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

abtin
page 20: … protoc command… I had to additionally run the following go get commands in order to be able to compile protobuf code using go...
New
jeremyhuiskamp
Title: Web Development with Clojure, Third Edition, vB17.0 (p9) The create table guestbook syntax suggested doesn’t seem to be accepted ...
New
patoncrispy
I’m new to Rust and am using this book to learn more as well as to feed my interest in game dev. I’ve just finished the flappy dragon exa...
New
fynn
This is as much a suggestion as a question, as a note for others. Locally the SGP30 wasn’t available, so I ordered a SGP40. On page 53, ...
New
jskubick
I’m under the impression that when the reader gets to page 136 (“View Data with the Database Inspector”), the code SHOULD be able to buil...
New
jskubick
I found an issue in Chapter 7 regarding android:backgroundTint vs app:backgroundTint. How to replicate: load chapter-7 from zipfile i...
New
dsmith42
Hey there, I’m enjoying this book and have learned a few things alredayd. However, in Chapter 4 I believe we are meant to see the “&gt;...
New
taguniversalmachine
Hi, I am getting an error I cannot figure out on my test. I have what I think is the exact code from the book, other than I changed “us...
New
davetron5000
Hello faithful readers! If you have tried to follow along in the book, you are asked to start up the dev environment via dx/build and ar...
New
dachristenson
@mfazio23 Android Studio will not accept anything I do when trying to use the Transformations class, as described on pp. 140-141. Googl...
New

Other popular topics Top

Devtalk
Hello Devtalk World! Please let us know a little about who you are and where you’re from :nerd_face:
New
Devtalk
Reading something? Working on something? Planning something? Changing jobs even!? If you’re up for sharing, please let us know what you’...
1016 16828 371
New
AstonJ
What chair do you have while working… and why? Is there a ‘best’ type of chair or working position for developers?
New
New
New
PragmaticBookshelf
Rust is an exciting new programming language combining the power of C with memory safety, fearless concurrency, and productivity boosters...
New
Exadra37
I am asking for any distro that only has the bare-bones to be able to get a shell in the server and then just install the packages as we ...
New
DevotionGeo
The V Programming Language Simple language for building maintainable programs V is already mentioned couple of times in the forum, but I...
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
DevotionGeo
I have always used antique keyboards like Cherry MX 1800 or Cherry MX 8100 and almost always have modified the switches in some way, like...
New

Latest in PragProg

View all threads ❯