joustava

joustava

Genetic Algorithms in Elixir: Code throws error (page 111)

Not sure if I’m doing something wrong here, but it seems that this piece of code will throw errors:

def fitness_function(chromosome) do
    target = 'ILoveGeneticAlgorithms'
    encrypted = 'LIjs`B`k`qlfDibjwlqmhv'
    cipher = fn word, key -> Enum.map(word, & rem(&1 ^^^ key, 32768)) end
    key =
      chromosome.genes
      |> Enum.map(&Integer.to_string/1)
      |> Enum.join("")
      |> String.to_integer(2)

      guess = List.to_string(cipher.(encrypted, key))
      String.jaro_distance(target, guess)
end

As

String.jaro_distance(target, guess)

Doesn’t expect ‘charlists’ but binaries

** (FunctionClauseError) no function clause matching in String.Unicode.graphemes/1

    The following arguments were given to String.Unicode.graphemes/1:

        # 1
        'ILoveGeneticAlgorithms'

    Attempted function clauses (showing 1 out of 1):

        def graphemes(binary) when is_binary(binary)

    (elixir 1.10.3) lib/elixir/unicode/unicode.ex:252: String.Unicode.graphemes/1
    (elixir 1.10.3) lib/string.ex:2488: String.jaro_distance/2
    (genetic 0.1.0) lib/genetic.ex:24: anonymous fn/2 in Genetic.evaluate/3
    (elixir 1.10.3) lib/enum.ex:1396: Enum."-map/2-lists^map/1-0-"/2
    (genetic 0.1.0) lib/genetic.ex:23: Genetic.evaluate/3
    (genetic 0.1.0) lib/genetic.ex:116: Genetic.evolve/6
    scripts/code_breaker.exs:39: (file)

I’m running this on:

⟩ elixir --version
Erlang/OTP 23 [erts-11.0.2] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe]

Elixir 1.10.3 (compiled with Erlang/OTP 23)

Not sure if something has changed recently regarding this issue.

Also, it seems the capture operator ‘&’ is in the wrong place in the book and should be moved before the call to ‘rem’ otherwise a

** (ArithmeticError) bad argument in arithmetic expression: rem(#Function<1.106933999/1 in CodeBreaker.fitness_function/1>, 32768)
    :erlang.rem(#Function<1.106933999/1 in CodeBreaker.fitness_function/1>, 32768)
    scripts/code_breaker.exs:16: anonymous fn/2 in CodeBreaker.fitness_function/1
    (genetic 0.1.0) lib/genetic.ex:24: anonymous fn/2 in Genetic.evaluate/3
    (elixir 1.10.3) lib/enum.ex:1396: Enum."-map/2-lists^map/1-0-"/2
    (genetic 0.1.0) lib/genetic.ex:23: Genetic.evaluate/3
    (genetic 0.1.0) lib/genetic.ex:116: Genetic.evolve/6
    scripts/code_breaker.exs:33: (file)

will occur.
I think on the next page in the iex example it is the correct way.

Popular Prag Prog topics Top

New
johnp
Hi Brian, Looks like the api for tinydb has changed a little. Noticed while working on chapter 7 that the .purge() call to the db throws...
New
sdmoralesma
Title: Web Development with Clojure, Third Edition - migrations/create not working: p159 When I execute the command: user=&gt; (create-...
New
lirux
Hi Jamis, I think there’s an issue with a test on chapter 6. I own the ebook, version P1.0 Feb. 2019. This test doesn’t pass for me: ...
New
curtosis
Running mix deps.get in the sensor_hub directory fails with the following error: ** (Mix) No SSH public keys found in ~/.ssh. An ssh aut...
New
oaklandgit
Hi, I completed chapter 6 but am getting the following error when running: thread 'main' panicked at 'Failed to load texture: IoError(O...
New
s2k
Hi all, currently I wonder how the Tailwind colours work (or don’t work). For example, in app/views/layouts/application.html.erb I have...
New
kolossal
Hi, I need some help, I’m new to rust and was learning through your book. but I got stuck at the last stage of distribution. Whenever I t...
New
Henrai
Hi, I’m working on the Chapter 8 of the book. After I add add the point_offset, I’m still able to see acne: In the image above, I re...
New
Keton
When running the program in chapter 8, “Implementing Combat”, the printout Health before attack was never printed so I assumed something ...
New

Other popular topics Top

PragmaticBookshelf
A PragProg Hero’s Journey with Brian P. Hogan @bphogan Have you ever worried that your only legacy will be in the form of legacy...
New
AstonJ
You might be thinking we should just ask who’s not using VSCode :joy: however there are some new additions in the space that might give V...
New
AstonJ
We have a thread about the keyboards we have, but what about nice keyboards we come across that we want? If you have seen any that look n...
New
AstonJ
I have seen the keycaps I want - they are due for a group-buy this week but won’t be delivered until October next year!!! :rofl: The Ser...
New
Margaret
Hello content creators! Happy new year. What tech topics do you think will be the focus of 2021? My vote for one topic is ethics in tech...
New
OvermindDL1
Woooooooo! This is such a huge release for it, and 2 years incoming! In short, the library is now using an updated hyper backend (not j...
New
gagan7995
API 4 Path: /user/following/ Method: GET Description: Returns the list of all names of people whom the user follows Response [ { ...
New
AstonJ
Saw this on TikTok of all places! :lol: Anyone heard of them before? Lite:
New
PragmaticBookshelf
Author Spotlight Jamis Buck @jamis This month, we have the pleasure of spotlighting author Jamis Buck, who has written Mazes for Prog...
New
PragmaticBookshelf
A Ruby-Centric Chat with Noel Rappin @noelrappin Once you start noodling around with Ruby you quickly figure out, as Noel Rappi...
New

Latest in PragProg

View all threads ❯