
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
Strings in Elixir are inserted between double quotes. Therefore change: target = 'ILoveGeneticAlgorithms'
to: target = "ILoveGeneticAlgorithms"
Popular Pragmatic Bookshelf topics

Title: Design and Build Great Web APIs - typo “https://company-atk.herokuapp.com/2258ie4t68jv” (page 19, third bullet in URL list)
Typo:...
New

your book suggests to use Image.toByteData() to convert image to bytes, however I get the following error: "the getter ‘toByteData’ isn’t...
New

The following is cross-posted from the original Ray Tracer Challenge forum, from a post by garfieldnate. I’m cross-posting it so that the...
New

Title: Web Development with Clojure, Third Edition - migrations/create not working: p159
When I execute the command:
user=> (create-...
New

This isn’t directly about the book contents so maybe not the right forum…but in some of the code apps (e.g. turbo/06) it sends a TURBO_ST...
New

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

In general, the book isn’t yet updated for Phoenix version 1.6. On page 18 of the book, the authors indicate that an auto generated of ro...
New

Modern Front-End Development for Rails - application does not start after run bin/setup (page xviii)
After some hassle, I was able to finally run bin/setup, now I have started the rails server but I get this error message right when I vis...
New

I got this error when executing the plot files on macOS Ventura 13.0.1 with Python 3.10.8 and matplotlib 3.6.1:
programming_ML/code/03_...
New

I’ve got to the end of Ch. 11, and the app runs, with all tabs displaying what they should – at first. After switching around between St...
New
Other popular topics

Algorithms and data structures are much more than abstract concepts. Mastering them enables you to write code that runs faster and more e...
New

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

I know that -t flag is used along with -i flag for getting an interactive shell. But I cannot digest what the man page for docker run com...
New

Curious to know which languages and frameworks you’re all thinking about learning next :upside_down_face:
Perhaps if there’s enough peop...
New

I ended up cancelling my Moonlander order as I think it’s just going to be a bit too bulky for me.
I think the Planck and the Preonic (o...
New

In case anyone else is wondering why Ruby 3 doesn’t show when you do asdf list-all ruby :man_facepalming: do this first:
asdf plugin-upd...
New

Continuing the discussion from Thinking about learning Crystal, let’s discuss - I was wondering which languages don’t GC - maybe we can c...
New

Think Again 50% Off Sale »
The theme of this sale is new perspectives on familiar topics.
Enter coupon code ThinkAgain2021 at checkout t...
New

Inside our android webview app, we are trying to paste the copied content from another app eg (notes) using navigator.clipboard.readtext ...
New

Hair Salon Games for Girls Fun
Girls Hair Saloon game is mainly developed for kids. This game allows users to select virtual avatars to ...
New
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /ruby
- /wasm
- /erlang
- /phoenix
- /keyboards
- /rails
- /python
- /js
- /security
- /go
- /swift
- /vim
- /clojure
- /haskell
- /emacs
- /java
- /svelte
- /onivim
- /typescript
- /kotlin
- /c-plus-plus
- /crystal
- /tailwind
- /react
- /gleam
- /ocaml
- /elm
- /flutter
- /vscode
- /ash
- /opensuse
- /html
- /centos
- /php
- /deepseek
- /zig
- /scala
- /sublime-text
- /lisp
- /textmate
- /nixos
- /debian
- /react-native
- /agda
- /kubuntu
- /arch-linux
- /django
- /ubuntu
- /revery
- /deno
- /manjaro
- /spring
- /nodejs
- /diversity
- /lua
- /julia
- /slackware
- /c