Skip to content

Instantly share code, notes, and snippets.

@theladyjaye
Created August 3, 2012 14:13
Show Gist options
  • Save theladyjaye/3248004 to your computer and use it in GitHub Desktop.
Save theladyjaye/3248004 to your computer and use it in GitHub Desktop.
Functional-ish fizzbuzz in python
from itertools import cycle, izip
import operator import add
fizz = cycle(['', '', 'fizz'])
buzz = cycle(['','','','','buzz'])
r = izip(fizz, buzz)
index = 1
while index < 101:
print("{}: {}".format(index, add(*r.next())))
index = index + 1
@theladyjaye
Copy link
Author

from __future__ import print_function
from itertools import cycle, izip, count, imap
from operator import add 
fizz = cycle(['', '', 'fizz'])
buzz = cycle(['','','','','buzz'])
r = izip(fizz, buzz)
i = count(1)
text = imap(lambda x: add(*x), r)

map(print, imap(lambda x: "{}:{}".format(i.next(), x), text))

@ifnull
Copy link

ifnull commented Aug 3, 2012

<?php

    $start = 10;
    $count = 0;

    $ugh = array_fill(0, 15, '');
    $fizz = array('', '', 'fizz');
    $buzz = array('','','','','buzz');

    foreach ($ugh as &$val) {
        $val = current($fizz).current($buzz);
        if(next($fizz) === false){
            reset($fizz);
        }
        if(next($buzz) === false){
            reset($buzz);
        }
    }

    reset($ugh);

    $i = 1;
    $lemniscate = (boolean) $count;
    while ($i < $start+$count || !$lemniscate) {
        if($i >= $start){
            print(sprintf('%d: %s', $i, current($ugh))."\n");
        }
        if(next($ugh) === false){
            reset($ugh);
        }
        $i++;
    }

@mycoalshoe
Copy link

Public Sub fb()
For i = 1 To 100
Select Case 0
Case n Mod 15
i = "FizzBuzz"
Case n Mod 3
i = "Fizz"
Case n Mod 5
i = "Buzz"
Case Else
i = n
End Select
Debug.Print i
Next n
End Sub

@mycoalshoe
Copy link

oops wrong language :)

@theladyjaye
Copy link
Author

Here it is in Haskell

let fizz = cycle ['', '', 'fizz']
let buzz = cycle ['', '', '', '', 'buzz']
let fizzbuzz = zipWith (++) fizz buzz
putStr (unlines fizzbuzz)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment