
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: Web Development with Clojure, Third Edition, pg 116
Hi - I just started chapter 5 and I am stuck on page 116 while trying to star...
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

Hello! On page xix of the preface, it says there is a community forum "… for help if your’re stuck on one of the exercises in this book… ...
New

Title: Build a Weather Station with Elixir and Nerves: Problem connecting to Postgres with Grafana on (page 64)
If you follow the defau...
New

Is the book’s epub format available to read on Google Play Books?
New

It seems the second code snippet is missing the code to set the current_user:
current_user: Accounts.get_user_by_session_token(session["...
New

The allprojects block listed on page 245 produces the following error when syncing gradle:
“org.gradle.api.GradleScriptException: A prob...
New

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

Book: Programming Phoenix LiveView, page 142 (157/378), file lib/pento_web/live/product_live/form_component.ex, in the function below:
d...
New

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
Other popular topics

New

poll
poll
Be sure to check out @Dusty’s article posted here: An Introduction to Alternative Keyboard Layouts It’s one of the best write-...
New

There’s a whole world of custom keycaps out there that I didn’t know existed!
Check out all of our Keycaps threads here:
https://forum....
New

On modern versions of macOS, you simply can’t power on your computer, launch a text editor or eBook reader, and write or read, without a ...
New

Not sure if following fits exactly this thread, or if we should have a hobby thread…
For many years I’m designing and building model air...
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

This is going to be a long an frequently posted thread.
While talking to a friend of mine who has taken data structure and algorithm cou...
New

If you want a quick and easy way to block any website on your Mac using Little Snitch simply…
File > New Rule:
And select Deny, O...
New

Author Spotlight:
Karl Stolley
@karlstolley
Logic! Rhetoric! Prag! Wow, what a combination. In this spotlight, we sit down with Karl ...
New

This is cool!
DEEPSEEK-V3 ON M4 MAC: BLAZING FAST INFERENCE ON APPLE SILICON
We just witnessed something incredible: the largest open-s...
New
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /wasm
- /ruby
- /erlang
- /phoenix
- /keyboards
- /rails
- /js
- /python
- /security
- /go
- /swift
- /vim
- /clojure
- /java
- /haskell
- /emacs
- /svelte
- /onivim
- /typescript
- /crystal
- /c-plus-plus
- /tailwind
- /kotlin
- /gleam
- /react
- /flutter
- /elm
- /ocaml
- /ash
- /vscode
- /opensuse
- /centos
- /php
- /deepseek
- /scala
- /html
- /zig
- /debian
- /nixos
- /lisp
- /agda
- /sublime-text
- /react-native
- /textmate
- /kubuntu
- /arch-linux
- /revery
- /ubuntu
- /manjaro
- /spring
- /django
- /diversity
- /nodejs
- /lua
- /c
- /slackware
- /julia
- /neovim