One-dimensional cellular automata: Difference between revisions
→Python :: Composition of pure functions: Tidied, updated primitives.
(→{{header|Haskell}}: Minor tidying) |
(→Python :: Composition of pure functions: Tidied, updated primitives.) |
||
Line 3,971:
Interpreting the rule shown in the task description as Wolfram rule 104, and generalising enough to allow for other rules of this kind:
<lang python>
from itertools import
from functools import reduce
from random import randint
# ruleSample :: Int -> String▼
def ruleSample(intRule):▼
'''16 steps in the evolution of a specified Wolfram rule.'''▼
return 'Rule ' + str(intRule) + ':\n' + (▼
unlines(map(▼
showCells,▼
take(16)(▼
iterate(nextRowByRule(intRule))(▼
onePixelInLineOf(64) if bool(randint(0, 1)) else (▼
randomPixelsInLineOf(64)▼
)▼
)▼
)▼
))▼
)▼
Line 3,997 ⟶ 3,981:
def nextRowByRule(intRule):
'''A row of booleans derived by Wolfram rule n
from another boolean row of the same length.
'''
# step :: (Bool, Bool, Bool) -> Bool
def step(l, x, r):
Line 4,009 ⟶ 3,993:
xs, xs[1:], xs[2:]
)) + [False]
return
# intFromBools :: [Bool] -> Int▼
def intFromBools(xs):▼
'''Integer derived by binary interpretation▼
of a list of booleans.'''▼
def go(b, pn):▼
power, n = pn▼
return (2 * power, n + power if b else n)▼
return foldr(go)([1, 0])(xs)[1]▼
#
# main :: IO ()
def main():
Line 4,018 ⟶ 4,012:
print(
unlines(map(
▲ )
# ----------------------- DISPLAY ------------------------
▲ '''16 steps in the evolution of a specified Wolfram rule.'''
▲ return 'Rule ' + str(intRule) + ':\n' + (
▲ unlines(map(
▲ showCells,
▲ take(16)(
▲ iterate(nextRowByRule(intRule))(
bool(randint(0, 1))
)
Line 4,029 ⟶ 4,042:
return Just((x // 2, bool(x % 2))) if x else Nothing()
return unfoldl(go)(n)
▲# intFromBools :: [Bool] -> Int
▲def intFromBools(xs):
▲ '''Integer derived by binary interpretation
▲ of a list of booleans.'''
▲ def go(b, pn):
▲ power, n = pn
▲ return (2 * power, n + power if b else n)
▲ return foldr(go)([1, 0])(xs)[1]
Line 4,072 ⟶ 4,075:
#
# Just :: a -> Maybe a
Line 4,089 ⟶ 4,092:
def foldr(f):
'''Right to left reduction of a list,
using
starting with an initial accumulator value.
def
return lambda acc: lambda
)▼
# iterate :: (a -> a) -> a -> Gen [a]
def iterate(f):
'''An infinite list of repeated
applications of f to x.
'''
def go(x):
v = x
Line 4,106 ⟶ 4,112:
yield v
v = f(v)
return
Line 4,113 ⟶ 4,119:
def take(n):
'''The prefix of xs of length n,
or xs itself if n > length xs.
'''
return lambda xs: (▼
def
if isinstance(xs, (list, tuple))
▲ )
else list(islice(xs, n))
# unfoldl :: (b -> Maybe (b, a)) -> b -> [a]
def unfoldl(f):
Line 4,131 ⟶ 4,138:
and the residual b is used as the argument for the next
application of f.
When f returns Nothing, the completed list is returned.
'''
def go(v):
xs = []
while True:
mb = f(
if mb.get('Nothing'):
return xs
else:
xs.insert(0,
return xs
return
# unlines :: [String] -> String
def unlines(xs):
'''A single
'''
return '\n'.join(xs)
|