
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

page 37
ANTLRInputStream input = new ANTLRInputStream(is);
as of ANTLR 4 .8 should be:
CharStream stream = CharStreams.fromStream(i...
New

Python Testing With Pytest - Chapter 2, warnings for “unregistered custom marks”
While running the smoke tests in Chapter 2, I get these...
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

Hi! I know not the intentions behind this narrative when called, on page XI:
mount() |> handle_event() |> render()
but the correc...
New

Title: Intuitive Python: docker run… denied error (page 2)
Attempted to run the docker command in both CLI and Powershell
PS C:\Users\r...
New

In case this helps anyone, I’ve had issues setting up the rails source code. Here were the solutions:
In Gemfile, change
gem 'rails'
t...
New

When trying to generate the protobuf .go file, I receive this error:
Unknown flag: --go_opt
libprotoc 3.12.3
MacOS 11.3.1
Googling ...
New

Hi, I have just acquired Michael Fazio’s “Kotlin and Android Development” to learn about game programming for Android. I have a game in p...
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

Hello @herbert ! Trying to get the very first “Hello, Bracket Terminal!" example to run (p. 53). I develop on an Amazon EC2 instance runn...
New
Other popular topics

What chair do you have while working… and why?
Is there a ‘best’ type of chair or working position for developers?
New

I’ve been really enjoying obsidian.md:
It is very snappy (even though it is based on Electron). I love that it is all local by defaul...
New

Please tell us what is your preferred monitor setup for programming(not gaming) and why you have chosen it.
Does your monitor have eye p...
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

Seems like a lot of people caught it - just wondered whether any of you did?
As far as I know I didn’t, but it wouldn’t surprise me if I...
New

Intensively researching Erlang books and additional resources on it, I have found that the topic of using Regular Expressions is either c...
New

Rails 7 completely redefines what it means to produce fantastic user experiences and provides a way to achieve all the benefits of single...
New
New

Get the comprehensive, insider information you need for Rails 8 with the new edition of this award-winning classic.
Sam Ruby @rubys
...
New

Ok, well here are some thoughts and opinions on some of the ergonomic keyboards I have, I guess like mini review of each that I use enoug...
New
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /wasm
- /ruby
- /erlang
- /phoenix
- /keyboards
- /rails
- /js
- /python
- /security
- /go
- /swift
- /vim
- /clojure
- /emacs
- /haskell
- /java
- /onivim
- /typescript
- /svelte
- /crystal
- /kotlin
- /c-plus-plus
- /tailwind
- /gleam
- /react
- /ocaml
- /elm
- /flutter
- /vscode
- /ash
- /html
- /opensuse
- /centos
- /php
- /deepseek
- /zig
- /scala
- /sublime-text
- /textmate
- /lisp
- /nixos
- /debian
- /react-native
- /agda
- /kubuntu
- /arch-linux
- /django
- /revery
- /ubuntu
- /manjaro
- /spring
- /nodejs
- /diversity
- /lua
- /julia
- /c
- /slackware
- /markdown