Stern-Brocot sequence: Difference between revisions
Content added Content deleted
Alextretyak (talk | contribs) (Added 11l) |
(→Python: Composing pure (curried) functions: Updated primitives.) |
||
Line 3,937: | Line 3,937: | ||
<lang python>'''Stern-Brocot sequence''' |
<lang python>'''Stern-Brocot sequence''' |
||
⚫ | |||
⚫ | |||
import operator |
import operator |
||
⚫ | |||
⚫ | |||
Line 3,949: | Line 3,949: | ||
def go(xs): |
def go(xs): |
||
x = xs[1] |
x = xs[1] |
||
return (tail(xs) + [x + head(xs), x]) |
return (x, tail(xs) + [x + head(xs), x]) |
||
return |
return chain([1], unfoldr(go)([1, 1])) |
||
iterate(go)([1, 1]) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
# main :: IO () |
# main :: IO () |
||
def main(): |
def main(): |
||
Line 3,998: | Line 3,995: | ||
# |
# ----------------- GENERIC ABSTRACTIONS ----------------- |
||
# compose (<<<) :: (b -> c) -> (a -> b) -> a -> c |
# compose (<<<) :: (b -> c) -> (a -> b) -> a -> c |
||
Line 4,027: | Line 4,023: | ||
'''True if p(x) holds for every x in xs''' |
'''True if p(x) holds for every x in xs''' |
||
return lambda xs: all(map(p, xs)) |
return lambda xs: all(map(p, xs)) |
||
⚫ | |||
⚫ | |||
'''A function f mapped over a |
|||
non finite stream of values.''' |
|||
⚫ | |||
⚫ | |||
v = next(g, None) |
|||
if None is not v: |
|||
⚫ | |||
else: |
|||
return |
|||
⚫ | |||
Line 4,053: | Line 4,035: | ||
'''The first element of a non-empty list.''' |
'''The first element of a non-empty list.''' |
||
return xs[0] |
return xs[0] |
||
# iterate :: (a -> a) -> a -> Gen [a] |
|||
def iterate(f): |
|||
'''An infinite list of repeated |
|||
applications of f to x.''' |
|||
def go(x): |
|||
⚫ | |||
while True: |
|||
yield v |
|||
v = f(v) |
|||
return lambda x: go(x) |
|||
Line 4,071: | Line 4,041: | ||
'''A sublist of xs from which all duplicates, |
'''A sublist of xs from which all duplicates, |
||
(as defined by the equality predicate p) |
(as defined by the equality predicate p) |
||
are excluded. |
are excluded. |
||
⚫ | |||
def go(xs): |
def go(xs): |
||
if not xs: |
if not xs: |
||
Line 4,082: | Line 4,053: | ||
)) |
)) |
||
) |
) |
||
return |
return go |
||
Line 4,100: | Line 4,071: | ||
return xs[1:] |
return xs[1:] |
||
else: |
else: |
||
islice(xs, 1) # First item dropped. |
|||
return xs |
return xs |
||
Line 4,114: | Line 4,085: | ||
else list(islice(xs, n)) |
else list(islice(xs, n)) |
||
) |
) |
||
⚫ | |||
⚫ | |||
'''A lazy (generator) list unfolded from a seed value |
|||
by repeated application of f until no residue remains. |
|||
Dual to fold/reduce. |
|||
f returns either None or just (value, residue). |
|||
For a strict output list, wrap the result with list() |
|||
''' |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
valueResidue = f(valueResidue[1]) |
|||
⚫ | |||