Future on Elixir (elixir-lang) – ExFuture

As part of some more trials on future, I’ve uploaded the above ExFuture module on the GitHub. The grammer is still immature, but trying to simplify it using macros. Internally, it uses spawn/receive process to asynchronously executing the specified code blocks or functions.

Basically, I’m trying to learn around the nice feature set of Scala/Akka.

Functional Patterns for the Asynchronous Web

The above introduction video talks about the usage of futures on scala, with an example of asynchronously hitting multiple external APIs. There’re various functional-style methods on futures, and nicely structured with Scala’s DSL framework. The above ExFuture, is just a trial to implement the similar type of functionalities.


defmodule ExFuture.HelperTest do
use ExUnit.Case
use ExFuture
test "future block" do
f = future do
3 * 3
end
assert 9 == value(f)
end
test "parallel map with future/value macro using collection" do
v = [1, 2, 3]
|> Enum.map(future(&(&1 * 2)))
|> Enum.map(&(value(&1)))
assert v == [2, 4, 6]
end
test "map on future for async chaining" do
i = 1
f1 = future(i * 2)
f2 = map(f1, &(&1 * 3))
f3 = map(f2, &(&1 * 4))
assert 24 == value(f3)
end
end

view raw

gistfile1.ex

hosted with ❤ by GitHub

Advertisement

Posted on December 28, 2013, in Conference, Elixir, Web. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: