News for hackers. Stuff that matters.
Re:The most elegant implementation of FizzBuzz by schoen on Wednesday October 26, 2016 @12:50PM (#12798114)
This is very nice. Here's my Python port:

  from itertools import cycle, imap, count, islice
  from operator import add

  def fizzbuzz(n):
      fizzes = cycle(["", "", "Fizz"])
      buzzes = cycle(["", "", "", "", "Buzz"])
      words = imap(add, fizzes, buzzes)
      numbers = imap(str, count(1))
      _fizzbuzz = imap(max, words, numbers)
      return list(islice(_fizzbuzz, n))
Re:The most elegant implementation of FizzBuzz by jimmahoney on Thursday October 27, 2016 @02:32AM (#12803265)
Here's a different sort-of-functional python fizzbuzz.

While it doesn't use the spiffy lazy lists of itertools, and does have the typical "if x % n == 0" test, it does generalize easily to the "and print Baz every 37th number" case.

  def joinmap(func, elements, separator):
    return separator.join(map(func, elements))

  def fb_value(x, fb={3:'Fizz', 5:'Buzz'}):
    words = joinmap(lambda n: fb[n] if x % n == 0 else '', fb.keys(), '')
    return words if words else str(x)

  def fb_range(low=1, high=100):
    return joinmap(fb_value, range(low, 1 + high), '\n')

  print fb_range()
Re:The most elegant implementation of FizzBuzz by schoen on Thursday October 27, 2016 @01:07PM (#12806946)
That's excellent!

Treating a dictionary as an iterator automatically iterates over its .keys(), so you can remove the .keys() in fb.keys() and just map over fb itself.

>>> map(lambda x: x+x, {2: "who", 4: "do", 6: "we", 8: "appreciate"})

[16, 4, 8, 12]

A problem that this highlights is that the order in which the Fizz and Buzz and other strings get concatenated depends on the order of the underlying dictionary hash, which can be thought of as random. In Python 3 it even changes from run to run.

https://docs.python.org/3/whatsnew/3.3.html#summary-release-...

$ python3 -c 'print(list({"foo": "bar", "baz": "qux"}))'

['foo', 'baz']

$ python3 -c 'print(list({"foo": "bar", "baz": "qux"}))'

['baz', 'foo']

For predictable behavior across larger sets of divisors, and for consistent behavior on Python 3, you'd probably want to use collections.OrderedDict.

Re:The most elegant implementation of FizzBuzz by viebel on Wednesday October 26, 2016 @12:54PM (#12798147)
It's amazing Python code and clojure code are almost the same.

(Except infix/prefix)

Re:The most elegant implementation of FizzBuzz by schoen on Wednesday October 26, 2016 @12:56PM (#12798172)
People don't always write Python in a functional style, but I imagine that the itertools module was created out of admiration (or jealousy) for other languages that can do some of these things. :-)
Re:The most elegant implementation of FizzBuzz by viebel on Wednesday October 26, 2016 @01:04PM (#12798250)
The klipse plugin can also features python interactive snippets - using skulpt - http://www.skulpt.org/.

Take a look here: http://blog.klipse.tech/klipse/2016/09/09/klipse-languages.h...

But unfortunately, itertools is not (yet?) supported by skulpt :(