Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What if Maybe was lazy? #8

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

What if Maybe was lazy? #8

wants to merge 4 commits into from

Conversation

rap1ds
Copy link
Owner

@rap1ds rap1ds commented Aug 21, 2014

This is an opt-in implementation for laziness. By default Maybes are not lazy, but if you call lazy or initialize Maybe with a block, it becomes lazy.

From README:

Laziness

Ruby 2.0 introduced lazy enumerables. By calling lazy on any enumerable, you get the lazy version of it. Same goes with Maybe.

called = false
m = Maybe(2).lazy.map do |value|
  called = true;
  value * value;
end

puts called # => false
puts m.get # => 4 # Map is called now
puts called # => true

You can also initialize Maybe lazily by giving it a block.

initialization_block_called = false
map_called = false

m = Maybe do
  initialization_block_called = true
  do_some_expensive_calculation              # returns 1234567890
end.map do |value|
  map_called = true;
  "the value of expensive calculation: #{value}";
end

puts initialization_block_called # => false
puts map_called # => false
puts m.get # => "the value of expensive calculation: 1234567890 # Map is called now
puts initialization_block_called # => true
puts map_called # => true

Note that if you initialize a maybe non-lazily and inspect it, you see from the class that it is a Some:

Maybe("I'm not lazy")               => #<Some:0x007ff7ac8697b8 @value=2>

However, if you initialize Maybe lazily, we do not know the type before the lazy block is evaluated. Thus, you see a different output when printing the value

Maybe { "I'm lazy" }               => #<Maybe:0x0000010107a600 @lazy=#<Enumerator::Lazy: #<Enumerator: #<Enumerator::Generator:0x0000010107a768>:each>>>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant