Skip to content

Instantly share code, notes, and snippets.

@jredville
Last active August 29, 2015 13:56
Show Gist options
  • Save jredville/8968504 to your computer and use it in GitHub Desktop.
Save jredville/8968504 to your computer and use it in GitHub Desktop.
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
@jredville
Copy link
Author

dear tabs... wtf

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