Skip to content

Instantly share code, notes, and snippets.

Created March 16, 2014 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/9589183 to your computer and use it in GitHub Desktop.
Save anonymous/9589183 to your computer and use it in GitHub Desktop.
Stu:conditioning ruby's scheme shell
##########################################################################
# Title : Elmhurst Ruby FizzBuzz
# Author : Stuart Gerstein <stu[no- s. pam]@rubyprogrammer.net>
# Date : 2014-03-16
# Requires : Ruby >= 2.x
# Category : Hacking
##########################################################################
# Description
# o This is a toy example of legal ruby without the excessive if/else
# /case/when/unless etc
# o No regex, gem or os level support was part of the challenge
# o The talk involved explanation of context switching and abstraction
# o This was a fun one. BTW if your reading this you spend too much time
# on the internet!
# NOTES:
# This header must remain with the document which is under the
# BSD license. Please share it with new users as well as seasoned
# Also adding to this doc is encouraged but without removing license
#
# Please set one of your server nodes to run protein folding@home
# research and distributed computing tools.
#
# It is essential to help out humanity by donating your unused cpu
# cycles to help out research for cures for Alzheimer's, Lou Gehrig's
# Parkinson's disease as well as cancer.
# To know more on the project as well as setup please visit:
#
# http://folding.stanford.edu/English/HomePage
#
##########################################################################
%w:fizzbuzz fizz buzz:.each do |meth|
define_method meth do
meth
end
end
#alternative to above:
def fizzbuzz() __method__ end
def fizz() __method__ end
def buzz() __method__ end
def buzzword expr
->(y){ ->(x){(x%y).zero?}}.(expr)
end
#Styles inside predicates below:
#Strunk and white would be proud!
def fizzbuzz? n
(buzzword 15).(n)
end
def fizz? n
(buzzword 3)[n]
end
def buzz? n
((buzzword 5).call n)
end
def progn
(1..100).map do |n| # no need for if or when
((fizzbuzz? n) && fizzbuzz) || #This is in fact
((fizz? n) && fizz) || # a 1-liner
((buzz? n) && buzz) || (n)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment