Class: Futuroscope::Map

Inherits:
Object
  • Object
show all
Defined in:
lib/futuroscope/map.rb

Overview

A futuroscope map behaves like a regular map but performs all operations using futures so they’re effectively parallel.

Instance Method Summary collapse

Constructor Details

#initialize(items) ⇒ Map

Initializes a map with a set of items.

items - Items in which to perform the mapping



12
13
14
# File 'lib/futuroscope/map.rb', line 12

def initialize(items)
  @items = items
end

Instance Method Details

#map(&block) ⇒ Object

Maps each item with a future.

block - A block that will be executed passing each element as a parameter

Returns an array of futures that behave like the original objects.



21
22
23
24
25
26
27
# File 'lib/futuroscope/map.rb', line 21

def map(&block)
  @items.map do |item|
    Future.new do
      block.call(item)
    end
  end
end