Skip to content

Instantly share code, notes, and snippets.

@henrik henrik/1-fizzbuzz.exs
Last active Nov 15, 2015

Embed
What would you like to do?
# 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

This comment has been minimized.

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

This comment has been minimized.

Copy link
Owner Author

henrik commented Nov 15, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.