Rust Pass by Reference


fn square (x_value: &mut u32) -> () {
    *x_value = 10;
}

fn main() {
    let mut x : u32 = 5;

    square(&mut x);
    println!("x is {0}", x);
    
}
Continue reading →

Deploy Cryogen blog on Github


SOURCE_BRANCH = "dev"
MASTER_BRANCH = "master"

# checkout correct branch
git checkout $SOURCE_BRANCH


# build
lein run

# stash
git stash

# checkout master branch
git checkout $MASTER_BRANCH
git fetch --all
git stash pop
cp -r public/blog/* blog/
cp -r public/blog/404.html .
cp -r public/blog/cryogen.xml .
cp -r public/blog/feed.xml .
cp -r public/blog/index.html .
cp -r public/blog/sitemap.xml .

git commit -m "Publish"
git push
Continue reading →

Decimal to binary conversion in Clojure


(defn convert-to-binary [value bits]
  (letfn [(to-binary [value bits] 
            (-> value (Integer/toString 2)
                (Integer/parseInt) (->> (format (str "%0" bits "d")))))]
    (let [binary-value (to-binary value bits) result-size (count binary-value)
          default-value (apply str (repeat bits 1))]
      (if (not= result-size bits) default-value  binary-value))))
Continue reading →

Conway's game of life implementation in Rust using ChatGPT


use std::io;

const WIDTH: usize = 20;
const HEIGHT: usize = 20;

struct Grid {
    cells: [[u8; WIDTH]; HEIGHT],
}

impl Grid {
    fn new() -> Grid {
        Grid {
            cells: [[0; WIDTH]; HEIGHT],
        }
    }

    fn get_neighbors(&self, x: usize, y: usize) -> u8 {
        let mut count = 0;
        for i in x.saturating_sub(1)..=x+1 {
            for j in y.saturating_sub(1)..=y+1 {
                if i == x && j == y {
                    continue;
                }
                if i < WIDTH && j < HEIGHT && self.cells[i][j] == 1 {
                    count += 1;
                }
            }
        }
        count
    }

    fn next_gen(&self) -> Grid {
        let mut next = Grid::new();
        for i in 0..WIDTH {
            for j in 0..HEIGHT {
                let neighbors = self.get_neighbors(i, j);
                if self.cells[i][j] == 1 && (neighbors == 2 || neighbors == 3) {
                    next.cells[i][j] = 1;
                } else if self.cells[i][j] == 0 && neighbors == 3 {
                    next.cells[i][j] = 1;
                }
            }
        }
        next
    }

    fn display(&self) {
        for i in 0..HEIGHT {
            for j in 0..WIDTH {
                if self.cells[j][i] == 1 {
                    print!("*");
                } else {
                    print!(" ");
                }
            }
            println!("");
        }
    }
}

fn main() {
    let mut grid = Grid::new();

    // Initialize the grid with some cells
    grid.cells[10][10] = 1;
    grid.cells[10][11] = 1;
    grid.cells[10][12] = 1;

    loop {
        grid.display();
        grid = grid.next_gen();
        println!("");
        println!("");

        let mut buffer = String::new();
        io::stdin().read_line(&mut buffer).unwrap();
    }
}

Continue reading →

Simple GA in Clojure

In this blog post I’ll present a simple Genetic Algorithm implementation made in Clojure. The application is based on the one presented by Lee Spector in HERE. The task of the Genetic Algorithm is to evolve, starting from random generated individuals, to a sequence of bits that sums to a particular number.

(defn new-individual
    "Function used for creating a new individual"
    [genome-length]
    {:genome (vec (repeatedly genome-length #(rand-int 2))) :fitness 0}
    )

Continue reading →