One-dimensional cellular automata: Difference between revisions

Content added Content deleted
Line 3,031: Line 3,031:


=={{header|Julia}}==
=={{header|Julia}}==
=== functional ===
Automaton implemented as an iterable.
<syntaxhighlight lang="julia">
<syntaxhighlight lang="julia">
automaton(g) =
struct Automaton g₀::BitVector end
for i ∈ 0:9
println(join(alive ? '#' : '_' for alive ∈ g))
g = ([false; g[1:end-1]] .+ g .+ [g[2:end]; false]) .== 2
end
automaton([c == '#' for c ∈ "_###_##_#_#_#_#__#__"])
</syntaxhighlight>
=== iterable struct ===
<syntaxhighlight lang="julia">
struct Automaton g₀::Vector{Bool} end


Base.iterate(a::Automaton, g = a.g₀) =
Base.iterate(a::Automaton, g = a.g₀) =
g, @. ([false; g[1:end-1]] + g + [g[2:end]; false]) == 2
g, ([false; g[1:end-1]] .+ g .+ [g[2:end]; false]) .== 2


Base.show(io::IO, a::Automaton) = for g in Iterators.take(a, 10)
Base.show(io::IO, a::Automaton) = for g in Iterators.take(a, 10)