Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Rubymotion Futures and Promises
Ruby
Branch: master
Failed to load latest commit information.
lib commit the gem files
spec
README.md fixed conflict issue in Readme file
futuristic-0.4.3.gem
futuristic.gemspec

README.md

Futures and Promises

This is a rubymotion implementation of the Futures and Promise pattern, on top of Grand Central Dispatch.

What are Futures and Promises?

In computer science, future, promise, and delay refer to constructs used for synchronizing in some concurrent programming languages. They describe an object that acts as a proxy for a result that is initially unknown, usually because the computation of its value is yet incomplete<source Wikipedia>.

Futures and Promises

are objects holding a value which may become available at some point. This value is usually the result of some other computation. Since this computation may fail with an exception, the Future may also hold an exception in case the computation throws one.

Usage:

in your Gem file

gem 'futuristic'

how to use Promises

def fibonacci(n)
  return n if n < 2
  fib1 = Dispatch::Promise.new { fibonacci(n-1) }
  fib2 = Dispatch::Promise.new { fibonacci(n-2) }
  fib1 + fib2
end

p fibonacci(10) # => 55

how to use Futures

# computation is started
future_data = Dispatch::Future.new do
    bundle  = NSBundle.mainBundle
    plist_path = bundle.pathForResource("map", ofType: "plist")
    @map_data = load_plist(File.read(plist_path))
end

# you can do something else
@table = create_table_named("Future Maps")

puts "Hello World"

@table.data = future_data.value # if the computation is done, results with be immediatelly returned, if not done yet it will wait.

Futures using callback

# computation is started
future_data = Dispatch::Future.new do
    bundle  = NSBundle.mainBundle
    plist_path = bundle.pathForResource("map", ofType: "plist")
    @map_data = load_plist(File.read(plist_path))
end

# you can do something else
@table = create_table_named("Future Maps")

future_data.when_done do |value|
    # when the computation is done, table data will be setted on the Future Queue
    # the call back is not executed on the MainQueue/Dispatch::Queue.main
    @table.data = future_data.value
end

Module Futuristic

class Request

  include Dispatch::Futuristic

  def long_taking_computation
    sleep 10
    42
  end
end

request = Request.new
computation = request.future.long_taking_computation

#you can do something else while the computation is been executed on a background queue
puts "Drink some Kölsch"

# now you need the result, if it's already finished
# you will ge the result, otherwise it will wait untill the computation finish
p computation.value  # =>  42

Todo

  • Parallel Enumerable
  • Actor models
  • documentation and examples
Something went wrong with that request. Please try again.