Family Day update

This commit is contained in:
Ingy döt Net 2020-02-17 23:21:07 -08:00
parent aac6731f2c
commit 9ad63ea473
2442 changed files with 39761 additions and 8255 deletions

View file

@ -1,52 +1,13 @@
'''Fizz buzz'''
from itertools import cycle, izip, count, islice
from itertools import count, cycle, islice
fizzes = cycle([""] * 2 + ["Fizz"])
buzzes = cycle([""] * 4 + ["Buzz"])
both = (f + b for f, b in izip(fizzes, buzzes))
# if the string is "", yield the number
# otherwise yield the string
fizzbuzz = (word or n for word, n in izip(both, count(1)))
# fizzBuzz :: () -> Generator [String]
def fizzBuzz():
'''A non-finite stream of fizzbuzz terms.'''
return map(
lambda f, b, n: (f + b) or n,
cycle([''] * 2 + ['Fizz']),
cycle([''] * 4 + ['Buzz']),
map(str, count(1))
)
# main :: IO ()
def main():
'''Display of first 100 terms of the fizzbuzz series.
'''
print(unlines(
take(100)(
fizzBuzz()
)
))
# GENERIC -------------------------------------------------
# take :: Int -> [a] -> [a]
# take :: Int -> String -> String
def take(n):
'''The prefix of xs of length n,
or xs itself if n > length xs.
'''
return lambda xs: (
xs[0:n]
if isinstance(xs, (list, tuple))
else list(islice(xs, n))
)
# unlines :: [String] -> String
def unlines(xs):
'''A single string formed by the intercalation
of a list of strings with the newline character.
'''
return '\n'.join(xs)
if __name__ == '__main__':
main()
# print the first 100
for i in islice(fizzbuzz, 100):
print i