One-dimensional cellular automata: Difference between revisions

m
Line 3,031:
 
=={{header|Julia}}==
=== functional ===
Automaton implemented as an iterable.
<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₀::BitVectorVector{Bool} end
 
Base.iterate(a::Automaton, g = a.g₀) =
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)
39

edits