Skip to content

Instantly share code, notes, and snippets.

@ravinggenius
Last active August 29, 2015 14:02
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 ravinggenius/5503a7ca7b458730bf86 to your computer and use it in GitHub Desktop.
Save ravinggenius/5503a7ca7b458730bf86 to your computer and use it in GitHub Desktop.
class FunctionalArray
attr_reader :head
attr_reader :tail
def initialize(*items)
@head = items.first
tail = items[1..-1]
@tail = (tail.count > 0) ? self.class.new(items[1..-1]) : []
end
def count
1 + tail.count
end
def inject(initial, &block)
memo = block.call(initial, head)
if tail.count > 0
tail.inject(memo, &block)
else
memo
end
end
def map(&block)
reply = inject([]) do |memo, item|
[ *memo, block.call(item) ]
end
self.class.new(reply)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment