# Genetic Algorithms in Elixir: wrong syntax in ch. 4, termination criteria for one\_max ?

**URL:** <https://forum.devtalk.com/t/genetic-algorithms-in-elixir-wrong-syntax-in-ch-4-termination-criteria-for-one-max/100252>\
**Category:** PragProg Customers\
**Tags:** book-genetic-algorithms-in-elixir\
**Created:** [11 March 2023 17:27 UTC](https://forum.devtalk.com/t/genetic-algorithms-in-elixir-wrong-syntax-in-ch-4-termination-criteria-for-one-max/100252 "2023-03-11T17:27:52Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![inreoh](https://forum.devtalk.com/user_avatar/forum.devtalk.com/inreoh/32/4917_2.png) [@inreoh](https://forum.devtalk.com/u/inreoh)\
**Post date:** [11 March 2023 17:27 UTC](https://forum.devtalk.com/t/genetic-algorithms-in-elixir-wrong-syntax-in-ch-4-termination-criteria-for-one-max/100252/1 "2023-03-11T17:27:52Z")

</div>

### @seanmor5

In chapter 4 of p1.0 (page 89 of 262 in the epub version), the code for the avg termination criterion looks like this:

```auto
def terminate?(population) do
  avg = 
    population
    |> Enum.map(&(Enum.sum(&1) / length(&1)))
  avg == 21
end

```

For me, this causes a syntax error:

protocol Enumerable not implemented for %Types.Chromosome

Presumably because the call to fitness\_function() is missing. This version works for me:

```auto
avg = 
          population
          |> Enum.map(&OneMax.fitness_function/1)
          |> Enum.sum()
          |> (fn x -> x / length(population) end).()

```

although it’s not very elegant (esp. the last line looks ugly), so I’d much prefer something closer to the original version.
