Skip to content

Instantly share code, notes, and snippets.

@henrik
Last active November 15, 2015 18:50
Show Gist options
  • Save henrik/6ca434c0c5cb66ed29d7 to your computer and use it in GitHub Desktop.
Save henrik/6ca434c0c5cb66ed29d7 to your computer and use it in GitHub Desktop.
# Shortest (91 chars):
for n<-1..?d,r=&(rem(n,&1)==0&&IO.write&2)do;f=r.(3,"Fizz");b=r.(5,"Buzz");f||b||r.(1,n)end
# If newlines are required (99 chars):
for n<-1..?d,r=&(rem(n,&1)==0&&IO.write&2),do: (f=r.(3,"Fizz");b=r.(5,"Buzz");f||b||r.(1,n);:io.nl)
# Other solutions:
for n<-1..?d,r=&(rem(n,&1)==0&&IO.puts&2),do: r.(15,"FizzBuzz")||r.(3,"Fizz")||r.(5,"Buzz")||r.(1,n)
for n<-1..?d,do: Enum.find [{15,"FizzBuzz"},{3,"Fizz"},{5,"Buzz"},{1,n}],fn({d,t})->rem(n,d)==0&&IO.puts(t)end
  • ?d is 100 (ASCII code for d)
  • for …,this,do abuses a for-comprehension filter. this runs on each iteration and would skip if returning a falsy value.
  • Since IO.write returns a truthy value, we can chain f||b||r.(1,n) to only output the bare number if Fizz or Buzz did not output.
@emson
Copy link

emson commented Nov 15, 2015

Great... thanks for the descriptions really helpful - also like the conditions of 1==1&&IO.write "match"

@henrik
Copy link
Author

henrik commented Nov 15, 2015

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