class RFunc::Seq

Public Class Methods

new(seq=[]) click to toggle source
# File lib/rfunc/seq.rb, line 9
def initialize(seq=[])
  raise "RFunc::Seq must be initialized with an Array.  #{seq.class} is not an Array" if seq.class != Array

  @array = seq
end

Public Instance Methods

+(seq2) click to toggle source
# File lib/rfunc/seq.rb, line 23
def +(seq2)
  Seq.new(@array + seq2)
end
<<(el) click to toggle source
# File lib/rfunc/seq.rb, line 19
def <<(el)
  Seq.new(@array << el)
end
<=>(seq_or_array) click to toggle source
# File lib/rfunc/seq.rb, line 31
def <=>(seq_or_array)
  if seq_or_array.is_a?(Seq)
    @array <=> seq_or_array.members
  else
    @array <=> seq_or_array
  end
end
==(object) click to toggle source
# File lib/rfunc/seq.rb, line 27
def ==(object)
  object.class == self.class && object.members == @array
end
all() click to toggle source
# File lib/rfunc/seq.rb, line 15
def all
  Seq.new(@array)
end
append(el) click to toggle source
# File lib/rfunc/seq.rb, line 91
def append(el)
  Seq.new(@array.push(el))
end
collect() { |el| ... } click to toggle source
# File lib/rfunc/seq.rb, line 113
def collect(&block)
  fold(Seq.new([])) {|accum, el| (res = yield(el)) ? accum.append(res) : accum }
end
collect_first() { |el| ... } click to toggle source
# File lib/rfunc/seq.rb, line 117
def collect_first(&block)
  # more performant than it's prettier version
  result = nil
  find {|el| result = yield(el) }
  result ? RFunc::Some.new(result) : RFunc::None.new
end
concat(seq_or_array) click to toggle source
# File lib/rfunc/seq.rb, line 124
def concat(seq_or_array)
  if seq_or_array.is_a?(Seq)
    Seq.new(@array.concat(seq_or_array.members))
  else
    Seq.new(@array.concat(seq_or_array))
  end
end
filter() { |el| ... } click to toggle source
# File lib/rfunc/seq.rb, line 99
def filter(&block)
  fold(Seq.new) {|accum, el|
    if yield(el)
      accum.append(el)
    else
      accum
    end
  }
end
find() { |el| ... } click to toggle source
# File lib/rfunc/seq.rb, line 109
def find(&block)
  RFunc::Option.new(@array.find {|el| yield(el) })
end
first()
Alias for: head
first_option()
Alias for: head_option
flat_map() { |el| ... } click to toggle source
# File lib/rfunc/seq.rb, line 132
def flat_map(&block)
  fold(Seq.new) {|accum, el|
    accum.concat(yield(el))
  }
end
flatten() click to toggle source
# File lib/rfunc/seq.rb, line 138
def flatten
  Seq.new(@array.flatten)
end
fold(accum) { |a, el| ... } click to toggle source
# File lib/rfunc/seq.rb, line 73
def fold(accum, &block)
  @array.inject(accum) {|a, el| yield(a, el) }
end
Also aliased as: foldl
foldl(accum, &block)
Alias for: fold
foldr(accum) { |a, el| ... } click to toggle source
# File lib/rfunc/seq.rb, line 77
def foldr(accum, &block)
  @array.reverse.inject(accum) {|a, el| yield(a, el) }
end
head() click to toggle source
# File lib/rfunc/seq.rb, line 43
def head
  raise "RFunc::Seq #{@array} has no head" if @array.size == 0
  raw_head
end
Also aliased as: first
head_option() click to toggle source
# File lib/rfunc/seq.rb, line 50
def head_option
  (h_e = raw_head) ? Some.new(h_e) : None.new
end
Also aliased as: first_option
intersect(seq_or_array) click to toggle source
# File lib/rfunc/seq.rb, line 150
def intersect(seq_or_array)
  if seq_or_array.is_a?(Seq)
    Seq.new(@array & seq_or_array.members)
  else
    Seq.new(@array & seq_or_array)
  end
end
last_option() click to toggle source
# File lib/rfunc/seq.rb, line 64
def last_option
  l = last
  l ? Some.new(l) : None.new
end
map() { |v| ... } click to toggle source
# File lib/rfunc/seq.rb, line 69
def map(&block)
  Seq.new(@array.map{|v| yield(v) })
end
members() click to toggle source
# File lib/rfunc/seq.rb, line 39
def members
  @array
end
prepend(el) click to toggle source
# File lib/rfunc/seq.rb, line 87
def prepend(el)
  Seq.new(@array.unshift(el))
end
reverse() click to toggle source
# File lib/rfunc/seq.rb, line 95
def reverse
  Seq.new(@array.reverse)
end
slice(from, to) click to toggle source
# File lib/rfunc/seq.rb, line 83
def slice(from, to)
  Seq.new(@array.slice(from, to))
end
sort() { |x,y| ... } click to toggle source
# File lib/rfunc/seq.rb, line 170
def sort(&block)
  if block_given?
    Seq.new(@array.sort {|x,y| yield(x,y) })
  else
    Seq.new(@array.sort)
  end
end
sort_by() { |obj| ... } click to toggle source
# File lib/rfunc/seq.rb, line 166
def sort_by(&block)
  Seq.new(@array.sort_by {|obj| yield(obj) })
end
tail() click to toggle source
# File lib/rfunc/seq.rb, line 60
def tail
  @tail ||= (t_s = @array[1..-1]) ? Seq.new(t_s) : Seq.new
end
tail_option() click to toggle source
# File lib/rfunc/seq.rb, line 56
def tail_option
  (tail[0]) ? Some.new(tail) : None.new
end
take(n) click to toggle source
# File lib/rfunc/seq.rb, line 162
def take(n)
  Seq.new(@array.take(n))
end
take_while() { |r| ... } click to toggle source
# File lib/rfunc/seq.rb, line 158
def take_while(&block)
  Seq.new(@array.take_while {|r| yield(r) })
end

Private Instance Methods

raw_head() click to toggle source
# File lib/rfunc/seq.rb, line 179
def raw_head; @array[0] end