Class: Collins::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/collins/option.rb

Overview

Note:

This is pretty much a straight rip off of the scala version

Represents optional values. Instances of ‘Option` are either an instance of `Some` or `None`

Examples:

name = get_parameter("name")
upper = Option(name).map{|s| s.strip}.filter{|s|s.size > 0}.map{|s|s.upcase}
puts(upper.get_or_else(""))

Direct Known Subclasses

None, Some

Instance Method Summary collapse

Instance Method Details

#defined?Boolean

Returns True if the value is defined.

Returns:

  • (Boolean)

    True if the value is defined



34
35
36
# File 'lib/collins/option.rb', line 34

def defined?
  !empty?
end

#empty?Boolean

Returns True if the value is undefined.

Returns:

  • (Boolean)

    True if the value is undefined

Raises:

  • (NotImplementedError)


29
30
31
# File 'lib/collins/option.rb', line 29

def empty?
  raise NotImplementedError.new("empty? not implemented")
end

#exists?(&predicate) ⇒ Boolean

Return true if non-empty and predicate is true for the value

Returns:

  • (Boolean)

    test passed



90
91
92
# File 'lib/collins/option.rb', line 90

def exists? &predicate
  !empty? && predicate.call(get)
end

#filter {|predicate| ... } ⇒ Option<Object>

Convert to ‘None` if predicate fails

Returns this option if it is non-empty and applying the predicate to this options returns true. Otherwise return ‘None`.

Yield Parameters:

  • predicate (Object)

    The current value

Yield Returns:

  • (Boolean)

    result of testing value

Returns:

  • (Option<Object>)

    ‘None` if predicate fails, or already `None`



149
150
151
152
153
154
155
# File 'lib/collins/option.rb', line 149

def filter &predicate
  if empty? || predicate.call(get) then
    self
  else
    None.new
  end
end

#filter_not(&predicate) ⇒ Option<Object>

Inverse of ‘filter` operation.

Returns this option if it is non-empty and applying the predicate to this option returns false. Otherwise return ‘None`.

Returns:

See Also:



164
165
166
167
168
169
170
# File 'lib/collins/option.rb', line 164

def filter_not &predicate
  if empty? || !predicate.call(get) then
    self
  else
    None.new
  end
end

#flat_map(&block) ⇒ Option<Object>

Same as map, but flatten the results

This is useful when operating on an object that will return an ‘Option`.

Examples:

Option(15).flat_map {|i| Option(i).filter{|i2| i2 > 0}} == Some(15)

Returns:

  • (Option<Object>)

    Optional value

See Also:



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/collins/option.rb', line 128

def flat_map &block
  if empty? then
    None.new
  else
    res = block.call(get)
    if res.is_a?(Some) then
      res
    else
      Some.new(res)
    end
  end
end

#foreach(&f) ⇒ NilClass

Apply the block specified to the value if non-empty

Returns:

  • (NilClass)


96
97
98
99
100
101
# File 'lib/collins/option.rb', line 96

def foreach &f
  if self.defined? then
    f.call(get)
  end
  nil
end

#getObject

Returns Value, if defined.

Returns:

  • (Object)

    Value, if defined

Raises:

  • (NameError)

    if value is undefined



40
41
42
# File 'lib/collins/option.rb', line 40

def get
  raise NotImplementedError.new("get not implemented")
end

#get_or_else(*default) { ... } ⇒ Object

The value associated with this option, or the default

Examples:

# Raises an exception
Option(nil).get_or_else { raise Exception.new("Stuff") }
# Returns -1
Option("23").map {|i| i.to_i}.filter{|i| i > 25}.get_or_else -1

Parameters:

  • default (Object)

    A default value to use if the option value is undefined

Yields:

  • Provide a default with a block instead of a parameter

Returns:

  • (Object)

    If None, default, otherwise the value



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/collins/option.rb', line 55

def get_or_else *default
  if empty? then
    if block_given? then
      yield
    else
      default.first
    end
  else
    get
  end
end

#map {|block| ... } ⇒ Option<Object>

If the option value is defined, apply the specified block to that value

Examples:

Option("15").map{|i| i.to_i}.get == 15

Yield Parameters:

  • block (Object)

    The current value

Yield Returns:

  • (Object)

    The new value

Returns:

  • (Option<Object>)

    Optional value



111
112
113
114
115
116
117
# File 'lib/collins/option.rb', line 111

def map &block
  if empty? then
    None.new
  else
    Some.new(block.call(get))
  end
end

#or_else(*default) ⇒ Option<Object>

Return this ‘Option` if non-empty, otherwise return the result of evaluating the default

Examples:

Option(nil).or_else { "foo" } == Some("foo")

Returns:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/collins/option.rb', line 71

def or_else *default
  if empty? then
    res = if block_given? then
            yield
          else
            default.first
          end
    if res.is_a?(Option) then
      res
    else
      ::Collins::Option(res)
    end
  else
    self
  end
end