Module: Enumerable

Included in:
LibXML::XML::XPath::Object, ObjectSpace, XRange
Defined in:
lib/rmtools/core/b.rb,
lib/rmtools/rand/enum.rb,
lib/rmtools/text/textilize.rb,
lib/rmtools/functional/fold.rb,
lib/rmtools/conversions/enum.rb,
lib/rmtools/enumerable/common.rb

Instance Method Summary collapse

Instance Method Details

#+(array) ⇒ Object



52
53
54
# File 'lib/rmtools/enumerable/common.rb', line 52

def +(array)
  to_a + array.to_a
end

#bObject



30
# File 'lib/rmtools/core/b.rb', line 30

def b; !empty? && self end

#dump(depth = 5) ⇒ Object



41
42
43
# File 'lib/rmtools/text/textilize.rb', line 41

def dump(depth=5)
  RMTools.dump_recurse self, 0, depth
end

#export(arr, index) ⇒ Object



12
13
14
# File 'lib/rmtools/enumerable/common.rb', line 12

def export(arr, index)
  arr[index] = self[index]
end

#foldl(o, m = nil) ⇒ Object Also known as: fold



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/rmtools/functional/fold.rb', line 4

def foldl(o, m=nil)
  case o
  when Proc
    block_given? ?
      reduce(m && yield(m)) {|m, i| m ? o.call(m, yield(i)) : yield(i)} : 
      reduce(m) {|m, i| m ? o.call(m, i) : i}
  when Symbol
    block_given? ?
      reduce(m && yield(m)) {|m, i| m ? m.__send__(o, yield(i)) : yield(i)} : 
      reduce(m) {|m, i| m ? m.__send__(o, i) : i}
  else TypeError! o, Proc, Symbol
  end
end

#foldr(o, m = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rmtools/functional/fold.rb', line 19

def foldr(o, m=nil)
  case o
  when Proc
  block_given? ?
    reverse.reduce(m && yield(m)) {|m, i| m ? o.call(yield(i), m) : yield(i)} : 
    reverse.reduce(m) {|m, i| m ? o.call(i, m) : i}
  when Symbol
  block_given? ?
    reverse.reduce(m && yield(m)) {|m, i| m ? yield(i).__send__(o, m) : yield(i)} : 
    reverse.reduce(m) {|m, i| m ? i.__send__(o, m) : i}
  else TypeError! o, Proc, Symbol
  end
end

#import(arr, index) ⇒ Object



8
9
10
# File 'lib/rmtools/enumerable/common.rb', line 8

def import(arr, index)
  self[index] = arr[index]
end

#map_hash(&block) ⇒ Object



44
45
46
# File 'lib/rmtools/enumerable/common.rb', line 44

def map_hash(&block)
  Hash[map(&block)]
end

#presentObject



48
49
50
# File 'lib/rmtools/enumerable/common.rb', line 48

def present
  to_a.present
end

#rand(*args) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rmtools/rand/enum.rb', line 4

def rand(*args)
  if args.empty?
    if block_given?
      h, ua = {}, to_a.uniq
      s = ua.size
      loop {
        i = Kernel.rand size
        if h[i]
          return if h.size == s
        elsif yield(e = ua[i])
          return e
        else h[i] = true
        end
      }
    else to_a[Kernel.rand(size)]
    end
  else
    Kernel.rand(*args)
  end
end

#randsample(qty = Kernel.rand(size)) ⇒ Object



25
26
27
28
29
# File 'lib/rmtools/rand/enum.rb', line 25

def randsample(qty=Kernel.rand(size))
  a, b = [], to_a.dup
  [qty, size].min.times {a << b.rand!}
  a
end

#recursive_find(&b) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/rmtools/enumerable/common.rb', line 22

def recursive_find(&b)
  res = nil
  to_a.each {|e|
    return e if b[e]
    if e.resto :recursive_find and res = e.recursive_find(&b)
      return res
    end
  }
  res
end

#recursive_select(&b) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/rmtools/enumerable/common.rb', line 33

def recursive_select(&b)
  res = []
  to_a.each {|e|
    res << e if b[e]
    if e.resto :recursive_find
      res.concat e.recursive_find_all(&b)
    end
  }
  res.uniq
end

#threadify(threads = 4, &block) ⇒ Object



56
57
58
# File 'lib/rmtools/enumerable/common.rb', line 56

def threadify(threads=4, &block)
  RMTools::threadify(self, threads, &block)
end

#to_traversableObject



4
5
6
# File 'lib/rmtools/enumerable/common.rb', line 4

def to_traversable
  RMTools::ValueTraversable(to_a)
end

#truth_mapObject



60
61
62
# File 'lib/rmtools/enumerable/common.rb', line 60

def truth_map
  to_a.map_hash {|i| [i, true]}
end

#urlencodeObject

TODO: deal with it



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rmtools/conversions/enum.rb', line 21

def urlencode
  map {|k, v| next if !v
    k, v = k.to_s, v.to_s
    if v =~ /&/
      v = v/'&'
      v.map {|val| "#{CGI.escape(k)}=#{CGI.escape(val)}"} * '&'
    elsif k =~ /&/
      k = k/'&'
      k.map {|key| "#{CGI.escape(key)}=#{CGI.escape(v)}"} * '&'
    else
      "#{CGI.escape(k)}=#{CGI.escape(v)}" 
    end
  } * '&'
end

#xprod(obj, inverse = false) ⇒ Object



16
17
18
19
20
# File 'lib/rmtools/enumerable/common.rb', line 16

def xprod(obj, inverse=false)
  obj = obj.to_a if obj.is_a? Set
  obj = [obj] unless obj.is_a? Array
  inverse ? obj.to_a.product(self) : product(obj)
end