Module: Functions

Defined in:
lib/functions.rb

Instance Method Summary collapse

Instance Method Details

#as_promiseObject



50
51
52
53
54
# File 'lib/functions.rb', line 50

def as_promise
  -> (fn) {
    Concurrent::Promise.new { fn.() }
  }
end

#callObject



17
18
19
# File 'lib/functions.rb', line 17

def call
  ->(fn) { fn.() }
end

#call_concurrently(sequence_of_fn) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/functions.rb', line 33

def call_concurrently(sequence_of_fn)
  pool = Concurrent::CachedThreadPool.new
  begin
    call_concurrently_with_pool(sequence_of_fn, pool)
  ensure
    pool.shutdown
  end
end

#call_concurrently_with_pool(sequence_of_fn, pool) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/functions.rb', line 42

def call_concurrently_with_pool(sequence_of_fn, pool)
  sequence_of_fn.
      map(as_promise).
      map(execute_with(pool)).
      realise.
      map(realise_promise)
end

#call_raises(e) ⇒ Object



13
14
15
# File 'lib/functions.rb', line 13

def call_raises(e)
  -> { raise e }
end

#defer_apply(fn, value) ⇒ Object



29
30
31
# File 'lib/functions.rb', line 29

def defer_apply(fn, value)
  ->() { fn.(value) }
end

#defer_return(fn) ⇒ Object



25
26
27
# File 'lib/functions.rb', line 25

def defer_return(fn)
  ->(value) { defer_apply(fn, value) }
end

#execute_with(pool) ⇒ Object



56
57
58
59
60
61
# File 'lib/functions.rb', line 56

def execute_with(pool)
  -> (promise) {
    pool.post { promise.execute }
    promise
  }
end

#flip(fn) ⇒ Object



21
22
23
# File 'lib/functions.rb', line 21

def flip(fn)
  ->(a, b) { fn.(b, a) }
end

#identityObject



9
10
11
# File 'lib/functions.rb', line 9

def identity
  -> (a) { a }
end

#realise_promiseObject



63
64
65
# File 'lib/functions.rb', line 63

def realise_promise
  ->(promise) { promise.value! }
end

#returns(value) ⇒ Object



5
6
7
# File 'lib/functions.rb', line 5

def returns(value)
  -> { value }
end