Experiments with concurrency.

Result values, both anonymous and named, are updated when complete. Even works with flatMap. Amazing!

In [1]:
import scala.concurrent._
Out[1]:
import scala.concurrent._
In [2]:
implicit val ec = ExecutionContext.global
Out[2]:
ec: ExecutionContextExecutor = scala.concurrent.impl.ExecutionContextImpl@400242ba
In [3]:
Future(println("Hello"))
Future(3)
Hello
In [4]:
Thread.sleep(1000)
In [5]:
Future{Thread.sleep(3000); 3}
In [6]:
Future{Thread.sleep(10000); 30}
In [7]:
Future{Thread.sleep(3000); 3 ; println("completed")}
In [8]:
val n = 3
val fut = Future{Thread.sleep(3000); n}
In [9]:
val fut = Future{Thread.sleep(3000); 3}
val fut1 = fut.flatMap{k => Future{Thread.sleep(5000); k * k}}
In [10]:
val futVec = (1 to 10).toVector.map(n => Future{Thread.sleep(1000 * n); n})
val allDone = Future.sequence(futVec)
In [11]:
import sys.process._
println("git log -1".!!)
commit 67cebb5501c7d1643042c9f3b27fd7a31d5b1b70
Author: Siddhartha Gadgil <siddhartha.gadgil@gmail.com>
Date:   Sat Sep 14 20:51:58 2019 +0530

    handles in almond

Out[11]:
import sys.process._
In [12]:
import $ivy.`io.monix::monix:3.0.0`
Out[12]:
import $ivy.$                      
In [13]:
import monix.eval._
Out[13]:
import monix.eval._
In [14]:
Task(3)
Out[14]:
res13: Task[Int] = Eval(
  ammonite.$sess.cmd13$Helper$$Lambda$3060/1851591291@681c652f
)
In [15]:
val task3 = Task.deferFuture(Future{Thread.sleep(6000); 3}(ec)).memoize
Out[15]:
task3: Task[Int] = Async(<function2>, false, true, true)
In [16]:
import monix.execution.Scheduler.Implicits.global
val fut3 = task3.runToFuture
In [17]:
task3
Out[17]:
res16: Task[Int] = Async(<function2>, false, true, true)
In [18]:
task3.runToFuture
In [19]:
val id = java.util.UUID.randomUUID().toString
kernel.publish.html("Starting", id)

for (i <- 1 to 10) {
  Thread.sleep((200.0 + 200.0 * scala.util.Random.nextGaussian).toLong max 0L)
  kernel.publish.updateHtml(s"Got item <b>#$i</b>", id)
}

kernel.publish.updateHtml("Got all items", id)
Got all items
Out[19]:
id: String = "1d50ca78-679b-4e7d-b690-e9ff66705b38"
In [20]:
for (i <- 1 to 10) {
  kernel.publish.html(s"Got item <b>#$i</b>")
  Thread.sleep((200.0 + 200.0 * scala.util.Random.nextGaussian).toLong max 0L)
}
Got item #1
Got item #2
Got item #3
Got item #4
Got item #5
Got item #6
Got item #7
Got item #8
Got item #9
Got item #10
In [21]:
import almond.display._
Html("<b>Bold stuff</b>")
Bold stuff
Out[21]:
import almond.display._
In [22]:
val handle = Html("<p> stuff comes here </p>")
SVG
In [23]:
handle.withContent("<b>Stuff becomes bold</b>").update()
In [24]:
val svgText = """<svg version="1.1"
     baseProfile="full"
     width="300" height="200"
     xmlns="http://www.w3.org/2000/svg">

  <rect width="100%" height="100%" fill="red" />

  <circle cx="150" cy="100" r="80" fill="green" />

  <text x="150" y="125" font-size="60" text-anchor="middle" fill="white">SVG</text>

</svg>"""
Out[24]:
svgText: String = """<svg version="1.1"
     baseProfile="full"
     width="300" height="200"
     xmlns="http://www.w3.org/2000/svg">

  <rect width="100%" height="100%" fill="red" />

  <circle cx="150" cy="100" r="80" fill="green" />

  <text x="150" y="125" font-size="60" text-anchor="middle" fill="white">SVG</text>

</svg>"""
In [25]:
Html(svgText)
SVG
In [26]:
Future{Thread.sleep(5000); handle.withContent(svgText).update() }(ec)
In [32]:
handle.data
Out[32]:
res31: Map[String, String] = Map("text/html" -> "<p> stuff comes here </p>")
In [33]:
handle.displayId
Out[33]:
res32: String = "4b61dd5f-e0eb-47fd-8bac-0d6395d88d32"
In [35]:
handle.url
Out[35]:
res34: Option[java.net.URL] = None
In [36]:
handle.contentOrUrl
Out[36]:
res35: Either[java.net.URL, String] = Right("<p> stuff comes here </p>")
In [37]:
handle.metadata
Out[37]:
res36: Map[String, String] = Map()
In [27]:
interp
Out[27]:
res26: ammonite.interp.api.InterpAPI = ammonite.interp.Interpreter$$anon$1@3af381b7
In [28]:
interp.repositories
Out[28]:
res27: util.Ref[List[coursierapi.Repository]] = Ref(ammonite.util.Ref$$anon$1$$Lambda$173/760846326@28ab3ce7)
In [29]:
Image
Out[29]:
res28: Image.type = almond.display.Image$@79edf603
In [30]:
Image.JPG
Out[30]:
res29: Image.JPG.type = JPG
In [ ]: