Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Scala does have FJ style parallelism. It's call futures and it's part of the actors library</p> <pre><code>import scala.actors.Future import scala.actors.Futures._ def mergeSort[A &lt;% Ordered[A]](xs : List[A]) : List[A] = { // merge is not interesting, it's sequential. The complexity lies in keeping it tail recursive def merge[A &lt;% Ordered[A]](accum : List[A], left : List[A], right : List[A]) : List[A] = { (left, right) match { case (lhead::ltail, rhead::rtail) =&gt; if (lhead &lt;= rhead) merge(lhead :: accum, ltail, right) else merge(rhead :: accum, left, rtail) case (Nil, _) =&gt; accum reverse_::: right case _ =&gt; accum reverse_::: left } } // here's the parallel sort bit def sort[A &lt;% Ordered[A]](xs : List[A], length : Int) : List[A] = { if (length &lt;= 1) xs else { val leftLength = length / 2 val rightLength = length - leftLength val (left, right) = xs splitAt leftLength // fork val leftFork = future { sort(left, leftLength) } val rightFork = future { sort(right, rightLength) } // join val leftJoin = leftFork() val rightJoin = rightFork() // merge merge(Nil, leftJoin, rightJoin) } } sort(xs, xs.length) } </code></pre> <p>Now, to the heart of the question. If Scala didn't have futures could you write one yourself based on actors. Indeed. It would look more or less like this.</p> <pre><code>import scala.actors.Actor import scala.actors.Actor._ object MyFuture { def apply[T](x : =&gt; T) : MyFuture[T] = { val future = new MyFuture[T] val act = actor { react { case sender : Actor =&gt; sender ! (future, x) } } act ! self future } } class MyFuture[A] extends Function0[A] { me =&gt; lazy val result = receive { case (`me`, result) =&gt; result.asInstanceOf[A] } def apply() = result </code></pre> <p>} </p> <p>And you would use it like so </p> <pre><code>scala&gt; val x = MyFuture(28 * 1000) x: Foo.MyFuture[Int] = &lt;function&gt; scala&gt; x() res4: Int = 28000 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload