Methods
elements elements! elementwise every every! ewise filter per threaded_map threaded_map_send to_elem
Classes and Modules
Module Enumerable::Filterable
Class Enumerable::Elementor
Class Enumerable::Enumerator
Public Instance methods
elements()

Alias for every

elements!()

Alias for every!

elementwise(count=1)

Returns an elementwise Functor designed to make R-like elementwise operations possible.

  [1,2].elementwise + 3          #=> [4,5]
  [1,2].elementwise + [4,5]      #=> [5,7]
  [1,2].elementwise + [[4,5],3]  #=> [[5,7],[4,5]
This method is also aliased as ewise
# File lib/more/facets/elementwise.rb, line 16
  def elementwise(count=1)
    @_elementwise_functor ||= []
    @_elementwise_functor[count] ||= Functor.new do |op,*args|
      if args.empty?
        r = self
        count.times do
          r = r.collect{ |a| a.send(op) }
        end
        r
      else
        r = args.collect do |arg|
          if Array === arg #arg.kind_of?(Enumerable)
            x = self
            count.times do
              ln = (arg.length > length ? length : arg.length )
              x = x.slice(0...ln).zip(arg[0...ln]).collect{ |a,b| a.send(op,b) }
              #slice(0...ln).zip(arg[0...1n]).collect{ |a,b| b ? a.send(op,b) : nil }
            end
            x
          else
            x = self
            count.times do
              x = x.collect{ |a| a.send(op,arg) }
            end
            x
          end
        end
        r.flatten! if args.length == 1
        r
      end
    end
  end
every()

Returns an elemental object. This allows you to map a method on to every element.

  r = [1,2,3].every + 3  #=> [4,5,6]
This method is also aliased as elements
# File lib/more/facets/elementor.rb, line 57
  def every
    @_every ||= to_elem
  end
every!()

In place version of every.

This method is also aliased as elements!
# File lib/more/facets/elementor.rb, line 63
  def every!
    raise NoMethodError unless respond_to?(:map!)
    @_every_inplace ||= to_elem(:map!)
  end
ewise(count=1)

Alias for elementwise

filter() {|output, *input| ...}

Without a block: wrap the Enumerable object in such a way that map, select and similar operations are performed "horizontally" across a series of blocks, instead of building an array of results at each step. This reduces memory usage, allows partial results to be provided early, and permits working with infinite series.

  (1..1_000_000_000).filter.select{ |i| i % 2 == 0 }.
                            map{ |i| i + 100 }.
                            take(10).to_a

With a block: the block acts as an arbitrary filter on the data. Unlike map, it can choose to drop elements from the result, and/or add additional ones. The first object passed to the block is the receiver of the output.

  (1..1_000_000_000).
    filter { |out,i| out << i if i % 2 == 0 }.  # like select
    filter { |out,i| out << i + 100 }.          # like map
    take(10).each { |i| puts i }

Use a method like to_a or to_h at the end of the chain if you want an Array or Hash built with the results, or each { … } if you just want to output each result and discard it.

# File lib/more/facets/filter.rb, line 30
  def filter(&blk)
    if block_given?
      Enumerator::Filter.new do |output|
        each do |*input|
          yield output, *input
        end
      end
    else
      Enumerator::Filter.new do |output|
        each do |*input|
          output.yield *input
        end
      end
    end
  end
per()
# File lib/more/facets/elementor.rb, line 46
  def per
    @__per__ ||= Functor.new do |op|
      Elementor.new(self, op)
    end
  end
threaded_map( {|| ...}

Like Enumerable#map but each iteration is processed via a separate thread.

CREDIT Sean O‘Halpin

# File lib/more/facets/thread.rb, line 37
  def threaded_map #:yield:
    map{ |e| Thread.new(e){ |t| yield(t) } }.map{ |t| t.value }
  end
threaded_map_send(meth, *args, &block)

Like Enumerable#map_send but each iteration is processed via a separate thread.

CREDIT Sean O‘Halpin

# File lib/more/facets/thread.rb, line 46
  def threaded_map_send(meth, *args, &block)
    map{ |e| Thread.new(e){ |t| t.send(meth, *args, &block) } }.map{ |t| t.value }
  end
to_elem(meth=nil)

Create Elementor.

# File lib/more/facets/elementor.rb, line 42
  def to_elem(meth=nil)
    Elementor.new(self, meth || :map)
  end