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.

Thanks!

Kurt Symanzik

Most Liked

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!

kokolegorille

kokolegorille

Whenever You want speed, real speed, You can use an ETS table.

It is blazing fast.

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement