kbsymanz

kbsymanz

Looking for constructive feedback on my first github Elixir project

I’ve been learning Elixir for about 10 months now in my spare time and I would welcome some constructive feedback on my Maze Generator library.

https://github.com/kbsymanz/maze_generator

Thanks!

Kurt Symanzik

Most Liked Responses

kokolegorille

kokolegorille

Hello and welcome,

Disclaimer… I did not read the book Mazes for programmers.

I do not like the use of Agent to store visited cells, just to manage a map. It is started/stopped in the same function.

It’s possible to do without processes. I translated your code to Rust, for fun (and learning).

pub struct RecursiveBacktrack {
    visited: HashMap<Coordinate, bool>
}

impl RecursiveBacktrack {
    pub fn new() -> Self {
        Self {
            visited: HashMap::new(),
        }
    }

    pub fn carve(&mut self, grid: &mut Grid) {
        let mut rng = thread_rng();

        let x: usize = rng.gen_range(0, grid.width);
        let y: usize = rng.gen_range(0, grid.height);

        let starting_coordinate = Coordinate(x, y);

        self.do_carve(grid, starting_coordinate, starting_coordinate)
    }

    fn do_carve(&mut self, grid: &mut Grid, current: Coordinate, last: Coordinate) {
        match self.visited.get(&current) {
            Some(_) => (),
            None => {
                self.visited.insert(current, true);
                grid.open_passage(current, last);

                let mut neighbors = grid.neighbors(&current);
                neighbors.shuffle(&mut thread_rng());

                for neighbor in neighbors {
                    self.do_carve(grid, neighbor, current);
                }
            },
        }
    }
}

and the result

+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |
+---+---+   +   +   +---+   +   +   +   +   +---+   +   +   +   +---+   +   +   +
|               |           |   |       |   |           |   |   |       |   |    
+---+   +---+---+   +---+   +---+   +---+   +   +---+---+   +   +   +---+   +   +
|       |           |   |       |   |       |   |           |   |   |       |    
+---+---+   +---+   +   +   +---+   +---+   +---+   +---+   +---+   +   +---+   +
|           |   |   |       |           |           |   |           |   |   |   |
+---+---+---+   +   +   +---+   +---+   +   +---+   +   +   +---+   +   +   +   +
|           |   |   |   |       |   |   |   |   |   |   |   |   |   |       |    
+---+---+---+   +   +   +---+   +---+   +   +   +---+   +   +---+   +---+---+---+
|   |           |   |       |           |   |           |                        
+---+   +---+---+   +---+   +   +---+---+   +---+---+   +---+   +---+---+   +---+
|               |       |   |   |               |   |       |   |           |    
+---+---+---+   +---+---+   +   +   +---+---+   +   +   +---+   +---+---+---+   +
|       |   |               |       |       |   |   |   |       |                
+---+   +   +---+---+   +---+---+---+   +---+   +   +---+   +   +   +---+---+---+
|                       |               |           |       |   |   |   |        
+---+---+---+---+---+   +   +---+---+---+   +---+---+   +---+   +   +   +   +---+
|           |       |   |               |   |           |   |       |   |   |    
+---+---+   +   +---+---+   +---+---+   +   +---+   +---+   +---+---+---+   +   +
|       |   |   |           |       |   |       |                           |   |
+---+   +---+   +   +   +   +---+   +   +---+   +---+   +---+---+---+   +   +   +
|   |       |   |   |   |   |   |           |       |   |               |   |   |
+---+---+   +   +   +---+   +---+   +---+   +---+---+   +   +   +---+---+   +   +
|       |   |   |                   |   |           |   |   |   |   |       |    
+---+   +   +   +---+   +---+   +---+---+   +---+   +   +   +   +   +   +   +---+
|   |   |           |   |   |   |           |   |   |   |   |   |   |   |   |    
+---+   +   +---+   +---+---+   +   +---+---+   +   +   +   +   +   +---+---+   +
|       |   |   |               |                   |   |   |   |               |
+---+---+   +   +---+---+---+   +---+   +   +---+---+   +---+   +   +   +---+   +
|       |   |               |       |   |   |   |               |   |   |        
+---+---+   +---+   +   +---+   +   +   +---+   +   +---+---+   +---+   +---+---+
|               |   |           |   |   |       |   |       |               |    
+---+   +---+   +---+   +---+---+---+   +---+---+   +   +---+   +---+---+   +   +
|       |   |       |   |                           |   |   |           |   |    
+---+---+   +---+   +---+   +   +---+---+---+---+---+   +   +---+---+---+   +---+
|   |           |           |   |                       |                        
+---+   +---+   +   +---+   +---+   +---+   +---+   +---+---+---+---+---+---+---+
|       |   |   |   |   |       |   |   |   |   |   |           |           |    
+---+   +   +   +   +   +   +   +   +   +   +   +   +   +---+   +   +---+   +   +

I don’t know if Agent are used to solve the maze, but You can generate without :slight_smile:

kokolegorille

kokolegorille

I did not benchmark, but I am sure calling a pure function will be faster than starting and passing messages to an Agent :slight_smile:

kbsymanz

kbsymanz

I refactored to use a map instead of an agent and it confirmed your suggestion. The result was over 3 times faster. Thanks!

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New

We're in Beta

About us Mission Statement