Module: Lab42::Core::IteratorReimpl

Included in:
Array, Enumerable, Enumerator::Lazy
Defined in:
lib/lab42/core/fn/iterator_reimpl.rb

Class Method Summary collapse

Class Method Details

.behavior?(x) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/lab42/core/fn/iterator_reimpl.rb', line 41

def self.behavior? x
  Symbol === x || Proc === x || Method === x
end

.decompose_args(args, block) ⇒ Object

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
13
14
# File 'lib/lab42/core/fn/iterator_reimpl.rb', line 6

def self.decompose_args args, block
  raise ArgumentError, 'too many arguments' if args.size + ( block ? 1 : 0 ) > 2
  return [args,block] if block 
  b, a = args.partition{|x| behavior? x}
  behave = b.pop
  a = b if a.empty?
  raise ArgumentError, "No behavior specified" unless behave
  return [a,behave]
end

.included(into) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/lab42/core/fn/iterator_reimpl.rb', line 16

def self.included into
  _self = self
  into.module_eval do
    alias_method :__lab42_core_iterator_map__, :map
    def map behavior=nil, &blk
      __lab42_core_iterator_map__(&(behavior||blk))
    end
    alias_method :__lab42_core_iterator_select__, :select
    def select behavior=nil, &blk
      __lab42_core_iterator_select__(&(behavior||blk))
    end
    alias_method :filter, :select

    alias_method :__lab42_core_iterator_inject__, :inject
    define_method :inject  do |*args, &blk|
      args, behavior = _self.decompose_args args, blk
      __lab42_core_iterator_inject__( *args, &behavior )
    end
    alias_method :reduce, :inject

    def filter *args, &blk
    end
  end

  private
  def self.behavior? x
    Symbol === x || Proc === x || Method === x
  end
end