Last active
August 29, 2015 13:56
-
-
Save jredville/8968504 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def reduce(coll, initial = nil, sym = nil, &blk) | |
if block_given? && sym.nil? | |
reduce_with_block coll, initial, &blk | |
else | |
reduce_without_block coll, initial, sym | |
end | |
end | |
def reduce_with_block(coll, initial, &blk) | |
memo = initial || coll.first | |
skipped = false | |
coll.each do |e| | |
if !initial && !skipped | |
skipped = true | |
next | |
end | |
memo = blk.call memo, e | |
end | |
memo | |
end | |
def reduce_without_block(coll, initial, sym) | |
if sym.nil? | |
sym, initial = initial, nil | |
end | |
reduce_with_block(coll, initial) {|memo, obj| memo.send(sym, obj) } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
dear tabs... wtf