Module: Erlang::Enumerable

Includes:
Enumerable
Included in:
List, Map, Tuple
Defined in:
lib/erlang/enumerable.rb

Overview

Helper module for Erlang sequential collections

Classes including Erlang::Enumerable must implement:

  • #each (just like ::Enumerable).
  • #select, which takes a block, and returns an instance of the same class with only the items for which the block returns a true value

Licensing

Portions taken and modified from https://github.com/hamstergem/hamster

Copyright (c) 2009-2014 Simon Harris

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Instance Method Summary collapse

Instance Method Details

#compactObject

Return a new collection with all nil elements removed.



47
48
49
# File 'lib/erlang/enumerable.rb', line 47

def compact
  return select { |item| !item.nil? }
end

#each_index(&block) ⇒ Object

Yield all integers from 0 up to, but not including, the number of items in this collection. For collections which provide indexed access, these are all the valid, non-negative indices into the collection.



71
72
73
74
75
# File 'lib/erlang/enumerable.rb', line 71

def each_index(&block)
  return enum_for(:each_index) unless block_given?
  0.upto(size-1, &block)
  return self
end

#grep(pattern, &block) ⇒ Object

Search the collection for elements which are #=== to item. Yield them to the optional code block if provided, and return them as a new collection.



53
54
55
56
57
# File 'lib/erlang/enumerable.rb', line 53

def grep(pattern, &block)
  result = select { |item| pattern === item }
  result = result.map(&block) if block_given?
  return result
end

#grep_v(pattern, &block) ⇒ Object

Search the collection for elements which are not #=== to item. Yield them to the optional code block if provided, and return them as a new collection.



62
63
64
65
66
# File 'lib/erlang/enumerable.rb', line 62

def grep_v(pattern, &block)
  result = select { |item| !(pattern === item) }
  result = result.map(&block) if block_given?
  return result
end

#group_by(&block) ⇒ Object

Groups the collection into sub-collections by the result of yielding them to the block. Returns a Map where the keys are return values from the block, and the values are sub-collections (of the same type as this one).



113
114
115
# File 'lib/erlang/enumerable.rb', line 113

def group_by(&block)
  return group_by_with(self.class.empty, &block)
end

#inspectObject

Convert this collection to a programmer-readable String representation.



131
132
133
134
135
# File 'lib/erlang/enumerable.rb', line 131

def inspect
  result = "#{self.class}["
  each_with_index { |obj, i| result << ', ' if i > 0; result << obj.inspect }
  return result << "]"
end

#join(separator = $,) ⇒ Object

Convert all the elements into strings and join them together, separated by separator. By default, the separator is $,, the global default string separator, which is normally nil.



120
121
122
123
124
125
126
127
128
# File 'lib/erlang/enumerable.rb', line 120

def join(separator = $,)
  result = ""
  if separator
    each_with_index { |obj, i| result << separator if i > 0; result << obj.to_s }
  else
    each { |obj| result << obj.to_s }
  end
  return Erlang.from(result)
end

#partitionObject

Return 2 collections, the first containing all the elements for which the block evaluates to true, the second containing the rest.



89
90
91
92
93
# File 'lib/erlang/enumerable.rb', line 89

def partition
  return enum_for(:partition) if not block_given?
  a,b = super
  return Erlang::Tuple[self.class.new(a), self.class.new(b)]
end

#productObject

Multiply all the items (presumably numeric) in this collection together.



78
79
80
# File 'lib/erlang/enumerable.rb', line 78

def product
  return reduce(1, &:*)
end

#rejectObject Also known as: delete_if

Return a new collection with all the elements for which the block returns false.



40
41
42
43
# File 'lib/erlang/enumerable.rb', line 40

def reject
  return enum_for(:reject) if not block_given?
  return select { |item| !yield(item) }
end

#sumObject

Add up all the items (presumably numeric) in this collection.



83
84
85
# File 'lib/erlang/enumerable.rb', line 83

def sum
  return reduce(0, &:+)
end