lib/ribimaybe.rb in ribimaybe-0.0.13 vs lib/ribimaybe.rb in ribimaybe-0.1.0

- old
+ new

@@ -2,20 +2,18 @@ module Ribimaybe module Maybe include Contracts - # Hack to ensure constant is available to Ruby contracts. - # + # Ensure constants are available to Ruby contracts. module Nothing; end class Just; end module Nothing include Contracts # Nothing string representation. - # def self.to_s "Nothing" end alias_method :inspect, :to_s @@ -23,43 +21,38 @@ # Compares a Nothing to another Maybe. # # ==== Attributes # # * +other+ - The other Maybe value. - # Contract Or[Nothing, Just] => Bool def self.===(other) self == other end # No operation. Always returns the default value. - # Contract Any, Proc => Any def self.maybe(default, &_) default end # No operation. Always returns Nothing. - # Contract Proc => Nothing def self.map(&_) self end # No operation. Always returns Nothing. - # Contract Any => Nothing def self.apply(_) self end class << self alias_method :>>, :apply end # No operation. Always returns Nothing. - # Contract Proc => Nothing def self.bind(fn = nil, &_) self end @@ -74,11 +67,10 @@ def initialize(value) @value = value end # Just string representation. - # def to_s "Just(#{@value.inspect})" end alias_method :inspect, :to_s @@ -92,11 +84,10 @@ # ==== Examples # # Just(1) == Just(1) # => true # Just(1) == Just(2) # => false # Just(1) == Nothing # => false - # Contract Or[Nothing, Just] => Bool def ==(other) other.maybe(false) do |value| @value == value end @@ -112,11 +103,10 @@ # # ==== Examples # # Just(1).maybe(false) { |x| x == 1 } # => true # Just(1).maybe(42) { |x| x } # => 1 - # Contract Any, Proc => Any def maybe(_, &fn) fn.curry.(@value) end @@ -128,11 +118,10 @@ # # ==== Examples # # Just(1).map { |x| x + 1 } # => Just(2) # Just { |x, y| x + y }.map { |f| f.(1) } # => Just(#<Proc:...>) - # Contract Proc => Just def map(&fn) Just.new(fn.curry.(@value)) end @@ -146,11 +135,10 @@ # ==== Examples # # Just do |x| # x + x # end.apply(Just(1)) # => Just(2) - # Contract Or[Nothing, Just] => Or[Nothing, Just] def apply(value) value.map { |v| @value.curry.(v) } end @@ -163,13 +151,12 @@ # * +fn+ - Function to be applied inside our Just. # # ==== Examples # # Just(1).bind do |x| - # rturn(x + x) + # unit(x + x) # end # => Just(2) - # Contract Proc => Or[Nothing, Just] def bind(fn = nil, &block) (fn || block).curry.(@value) end @@ -187,16 +174,15 @@ # ==== Examples # # Maybe(nil) # => Nothing # Maybe(1) # => Just(1) # Maybe { |x| x } # => Just(#<Proc:0x007fdecc03a478@(irb):6>) - # Contract Any, Or[nil, Proc] => Or[Nothing, Just] def Maybe(value = nil, &fn) (value || fn) ? Just.new(value || fn.curry) : Nothing end - alias_method :Just, :Maybe - alias_method :pure, :Maybe - alias_method :rturn, :Maybe + alias_method :Just, :Maybe + alias_method :pure, :Maybe + alias_method :unit, :Maybe end end