Module: LazyList::ModuleFunctions

Included in:
LazyList, LazyList
Defined in:
lib/lazylist.rb

Overview

This module contains module functions, that are added to LazyList and it’s instances.

Instance Method Summary collapse

Instance Method Details

#delay(&block) ⇒ Object

Delay the value of block to a Promise.



58
59
60
# File 'lib/lazylist.rb', line 58

def delay(&block)
  MemoPromise.new(&block)
end

#force(obj) ⇒ Object

Force the delayed obj to evaluate to its value.



63
64
65
66
67
68
69
70
# File 'lib/lazylist.rb', line 63

def force(obj)
  case obj
  when Promise
    force obj.__value__
  else
    obj
  end
end

#mix(*lists) ⇒ Object

Returns a LazyList, that consists of the mixed elements of the lists _lists without any special order.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/lazylist.rb', line 74

def mix(*lists)
  return empty if lists.empty?
  first = lists.shift
  if lists.empty?
    first
  elsif first.empty?
    mix(*lists)
  else
    LazyList.new(first.head) do
      t = first.tail
      lists = lists.push t unless t.empty?
      mix(*lists)
    end
  end
end