Skip to content

Instantly share code, notes, and snippets.

@to4iki
Created August 29, 2015 08:26
Show Gist options
  • Save to4iki/bc3f257112f55e9934e4 to your computer and use it in GitHub Desktop.
Save to4iki/bc3f257112f55e9934e4 to your computer and use it in GitHub Desktop.
proc
def calc(b)
b.call(3, 2)
end
#def calc(&block)
# block.call
#end
# calc { |a, b| p a+b }
add = ->(a,b) { a+b }
multi = ->(a,b) { a*b }
p calc add # 5
p calc multi # 6
p %w(foo bar).map(&:upcase)
p [1,2,3].map(&:to_s)
class Array
alias :head :first
def tail
drop 1
end
def reduce_left(proc, acc=0)
return acc if empty?
proc.call(head, tail.reduce_left(proc, acc))
end
end
p [1,2,3].head # 1
p [1,2,3].tail # [2,3]
p [1,2,3].reduce_left add # 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment