module Erlang::Enumerable

Helper module for Erlang sequential collections

Classes including `Erlang::Enumerable` must implement:

Licensing

Portions taken and modified from 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.

Public Instance Methods

compact() click to toggle source

Return a new collection with all `nil` elements removed.

# File lib/erlang/enumerable.rb, line 47
def compact
  return select { |item| !item.nil? }
end
delete_if()
Alias for: reject
each_index(&block) click to toggle source

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.

# 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) click to toggle source

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.

# 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) click to toggle source

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.

# 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) click to toggle source

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).

# File lib/erlang/enumerable.rb, line 113
def group_by(&block)
  return group_by_with(self.class.empty, &block)
end
inspect() click to toggle source

Convert this collection to a programmer-readable `String` representation.

# 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 = $,) click to toggle source

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`.

# 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
partition() click to toggle source

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

Calls superclass method
# 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
pretty_print(pp) click to toggle source

@private

# File lib/erlang/enumerable.rb, line 138
def pretty_print(pp)
  return pp.group(1, "#{self.class}[", "]") do
    pp.breakable ''
    pp.seplist(self) { |obj| obj.pretty_print(pp) }
  end
end
product() click to toggle source

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

# File lib/erlang/enumerable.rb, line 78
def product
  return reduce(1, &:*)
end
reject() { |item| ... } click to toggle source

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

# File lib/erlang/enumerable.rb, line 40
def reject
  return enum_for(:reject) if not block_given?
  return select { |item| !yield(item) }
end
Also aliased as: delete_if
sort_by(&block) click to toggle source

Rubinius implements Enumerable#sort_by using Enumerable#map Because we do our own, custom implementations of map, that doesn't work well @private

# File lib/erlang/enumerable.rb, line 154
def sort_by(&block)
  result = Erlang.from(to_a)
  return result.frozen? ? result.sort_by(&block) : result.sort_by!(&block)
end
sum() click to toggle source

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

# File lib/erlang/enumerable.rb, line 83
def sum
  return reduce(0, &:+)
end

Protected Instance Methods

group_by_with(empty_group, &block) click to toggle source

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. All the sub-collections are built up from `empty_group`, which should respond to `#add` by returning a new collection with an added element.

# File lib/erlang/enumerable.rb, line 100
def group_by_with(empty_group, &block)
  block ||= lambda { |item| item }
  return reduce(EmptyMap) do |map, item|
    key = block.call(item)
    group = map.get(key) || empty_group
    map.put(key, group.add(item))
  end
end