Sha256: ecf9b885769dbce3f6ef51695cb21d6dd11088b223cfe7b8578bcd810c8150b5

Contents?: true

Size: 1.04 KB

Versions: 4

Compression:

Stored size: 1.04 KB

Contents

require 'concurrent/executors'
require 'concurrent/promise'

module Functions
  def returns(value)
    -> { value }
  end

  def identity
    -> (a) { a }
  end

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

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

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

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

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

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

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

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

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

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
totally_lazy-0.1.11 lib/functions.rb
totally_lazy-0.1.10 lib/functions.rb
totally_lazy-0.1.9 lib/functions.rb
totally_lazy-0.1.0 lib/functions.rb