Genetic Algorithms in Elixir: Use the capture operator fully

&Enum.sum(&1) is more commonly written as &Enum.sum/1

This is used in a large number of examples.

1 Like

Thanks for pointing this out. I’m not sure why I had written it that way in the first place. I’ll comb over the examples again and focus on addressing that. I hope you enjoy the rest of the book!

2 Likes

While we’re talking about minor syntax stuff, how about:

[h1 ++ t2 | [h2 ++ t1 | acc]]

:arrow_down_small:

[h1 ++ t2, h2 ++ t1 | acc]
3 Likes

Appreciate this! There’s definitely strange syntactic stuff like that sprinkled throughout. If you or anybody else on this thread sees code like this throughout the book that can be expressed more succinctly/clearly/etc. please feel free to let me know.

3 Likes

Hi there! Not sure if this is the best thread for this, and it’s also very minor and a matter of personal preference :slightly_smiling_face:

The code:

|> Enum.sort_by(fitness_function, &>=/2)

is used a handful of times throughout the book. I find it a bit clearer to use:

|> Enum.sort_by(fitness_function, :desc)

but again, purely personal preference :smile:

3 Likes

I always forget about this feature!

The sort comparator shorthand is actually pretty new in Elixir, first implemented a year ago and released in v1.10.0, Jan 27 2020.

I don’t know if the book targets a specific version of Elixir, but it seems reasonable to me to avoid relying on language features only available in the last year or so.

1 Like

Finally found some time to start reading this book again, and found another minor style thing.

I would say this (page 47, 49, etc. in both B1.0 and B2.0):

def terminate?(population), do: hd(population).fitness == 42

is more readily understandable when written:

def terminate?([best | rest]), do: best.fitness == 42
1 Like