
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

Some minor things in the paper edition that says “3 2020” on the title page verso, not mentioned in the book’s errata online:
p. 186 But...
New

Many tasks_proj/tests directories exist in chapters 2, 3, 5 that have tests that use the custom markers smoke and get, which are not decl...
New

Title: Hands-On Rust (Chapter 11: prefab)
Just played a couple of amulet-less games. With a bit of debugging, I believe that your can_p...
New

Title: Hands-on Rust: question about get_component (page 295)
(feel free to respond. “You dug you’re own hole… good luck”)
I have somet...
New

First, the code resources:
Page 237: rumbl_umbrella/apps/rumbl/mix.exs
Note: That this file is missing.
Page 238: rumbl_umbrella/app...
New

The generated iex result below should list products instead of product for the metadata. (page 67)
iex> product = %Product{}
%Pento....
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

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

Modern front-end development for Rails, second edition - Struggling to get the first chapter to work
After running /bin/setup, the first error was: The foreman' command exists in these Ruby versions: That was easy to fix: gem install fore...
New

Getting an error when installing the dependencies at the start of this chapter:
could not compile dependency :exla, "mix compile" failed...
New
Other popular topics

Bought the Moonlander mechanical keyboard. Cherry Brown MX switches. Arms and wrists have been hurting enough that it’s time I did someth...
New
New

Learn different ways of writing concurrent code in Elixir and increase your application's performance, without sacrificing scalability or...
New

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

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:
Peter Ullrich
@PJUllrich
Data is at the core of every business, but it is useless if nobody can access and analyze ...
New

Author Spotlight:
Bruce Tate
@redrapids
Programming languages always emerge out of need, and if that’s not always true, they’re defin...
New

A Brief Review of the Minisforum V3 AMD Tablet.
Update: I have created an awesome-minisforum-v3 GitHub repository to list information fo...
New

Node.js v22.14.0 has been released.
Link: Release 2025-02-11, Version 22.14.0 'Jod' (LTS), @aduh95 · nodejs/node · GitHub
New

Ask Me Anything with
Mark Volkmann
@mvolkmann
On February 24 and 25, we are giving you a chance to ask questions of PragProg author M...
New
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /ruby
- /wasm
- /erlang
- /phoenix
- /keyboards
- /rails
- /python
- /js
- /security
- /go
- /swift
- /vim
- /clojure
- /emacs
- /haskell
- /java
- /onivim
- /typescript
- /svelte
- /c-plus-plus
- /crystal
- /kotlin
- /tailwind
- /react
- /gleam
- /ocaml
- /elm
- /flutter
- /vscode
- /ash
- /opensuse
- /html
- /centos
- /php
- /deepseek
- /zig
- /scala
- /textmate
- /sublime-text
- /lisp
- /nixos
- /debian
- /react-native
- /agda
- /kubuntu
- /arch-linux
- /django
- /revery
- /ubuntu
- /manjaro
- /spring
- /nodejs
- /diversity
- /lua
- /deno
- /julia
- /c
- /slackware