38 ms·
My 2 cents: import itertools # this block is just for simple functional pipes class B: def __init__(self, f): self.f = f class Pipe (B): _
by tandav 6y ago
My 2 cents:
import itertools
# this block is just for simple functional pipes
class B:
def __init__(self, f): self.f = f
class Pipe (B): __ror__ = lambda self, x: self.f(x)
class Map (B): __ror__ = lambda self, x: map (self.f, x)
class MapValues(B): __ror__ = lambda self, it: it | Map(lambda kv: (kv[0], self.f(kv[1])))
# here the actual FizzBuzz
(
range(1, 100)
| Map(lambda i: (i, itertools.compress(('Fizz', 'Buzz'), (i % 3 == 0, i % 5 == 0))))
| MapValues(''.join)
| Map(lambda kv: kv[1] or kv[0])
| Pipe(list)
)
[1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz', 11, 'Fizz', 13, 14, 'FizzBuzz', 16, 17, ... ]
if you're interested in pipes like above - here my simple library for that (no install needed, just copy some lines)
https://github.com/tandav/pipe#fizzbuzz https://github.com/tandav/pipe#fizzbuzz