Friday, May 7, 2010

Skills. This allows you to set a variable asyncly, and immediately get on with it, and not have to worry about race conditions.

Example:

# without Asyncf
x = (1..12402).inject(1,:*)
y = (1..18320).inject(1,:*)
puts x * y

# the obvious problem with that:
# x is computed first, y is computed after, then we x * y.
# that takes unnecessary time. y is independent of x, so why execute it after?
# threads can be difficult to manage though, because of race conditions.
# well, Asyncf makes it easy!

x = Asyncf.new { (1..12402).inject(1,:*) }
y = Asyncf.new { (1..18320).inject(1,:*) }
puts x * y

# here, x and y will be calculated in parallel,
# and x * y will block until both are solved. but you could even say this:

x = Asyncf.new { Math.sqrt(2**48) }
y = Asyncf.new { Math.sqrt(2**96) }
z = Asyncf.new { Math.sqrt(x**y)  }
puts x * y * z

# the order of execution: x & y → z → puts

This also serves as a preview of how awesome an async paradigm would be. In which, things are executed in an order automatically calculated by following dependencies.


Notes

  1. 101718 reblogged this from devyn
  2. devyn posted this