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.

First Post!

adkelley

adkelley

Strings in Elixir are inserted between double quotes. Therefore change: target = 'ILoveGeneticAlgorithms' to: target = "ILoveGeneticAlgorithms"

Where Next?

Popular Pragmatic Bookshelf topics Top

johnp
Running the examples in chapter 5 c under pytest 5.4.1 causes an AttributeError: ‘module’ object has no attribute ‘config’. In particula...
New
jeffmcompsci
Title: Design and Build Great Web APIs - typo “https://company-atk.herokuapp.com/2258ie4t68jv” (page 19, third bullet in URL list) Typo:...
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
digitalbias
Title: Build a Weather Station with Elixir and Nerves: Problem connecting to Postgres with Grafana on (page 64) If you follow the defau...
New
brunogirin
When I run the coverage example to report on missing lines, I get: pytest --cov=cards --report=term-missing ch7 ERROR: usage: pytest [op...
New
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
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
a.zampa
@mfazio23 I’m following the indications of the book and arriver ad chapter 10, but the app cannot be compiled due to an error in the Bas...
New
roadbike
From page 13: On Python 3.7, you can install the libraries with pip by running these commands inside a Python venv using Visual Studio ...
New

Other popular topics Top

wolf4earth
@AstonJ prompted me to open this topic after I mentioned in the lockdown thread how I started to do a lot more for my fitness. https://f...
New
Exadra37
I am thinking in building or buy a desktop computer for programing, both professionally and on my free time, and my choice of OS is Linux...
New
dasdom
No chair. I have a standing desk. This post was split into a dedicated thread from our thread about chairs :slight_smile:
New
Margaret
Hello everyone! This thread is to tell you about what authors from The Pragmatic Bookshelf are writing on Medium.
1142 25749 758
New
AstonJ
Saw this on TikTok of all places! :lol: Anyone heard of them before? Lite:
New
First poster: joeb
The File System Access API with Origin Private File System. WebKit supports new API that makes it possible for web apps to create, open,...
New
AstonJ
Was just curious to see if any were around, found this one: I got 51/100: Not sure if it was meant to buy I am sure at times the b...
New
PragmaticBookshelf
Author Spotlight Rebecca Skinner @RebeccaSkinner Welcome to our latest author spotlight, where we sit down with Rebecca Skinner, auth...
New
New
AstonJ
This is a very quick guide, you just need to: Download LM Studio: https://lmstudio.ai/ Click on search Type DeepSeek, then select the o...
New

Sub Categories: