Ad .sc sample

This commit is contained in:
Ted Nyman
2013-12-11 15:06:14 -08:00
parent 4533b9baaa
commit 4d40cab954
2 changed files with 205 additions and 26 deletions

View File

@@ -349,6 +349,7 @@
],
"Scala": [
".sbt",
".sc",
".script!"
],
"Scaml": [
@@ -503,8 +504,8 @@
".gemrc"
]
},
"tokens_total": 425921,
"languages_total": 496,
"tokens_total": 426327,
"languages_total": 498,
"tokens": {
"ABAP": {
"*/**": 1,
@@ -39899,29 +39900,29 @@
"exec": 2,
"scala": 2,
"#": 2,
"object": 2,
"object": 3,
"Beers": 1,
"extends": 1,
"Application": 1,
"{": 10,
"def": 7,
"{": 21,
"def": 10,
"bottles": 3,
"(": 34,
"(": 67,
"qty": 12,
"Int": 3,
"Int": 11,
"f": 4,
"String": 5,
")": 34,
"//": 4,
")": 67,
"//": 29,
"higher": 1,
"-": 4,
"-": 5,
"order": 1,
"functions": 2,
"match": 2,
"case": 5,
"+": 29,
"case": 8,
"+": 49,
"x": 3,
"}": 11,
"}": 22,
"beers": 3,
"sing": 3,
"implicit": 3,
@@ -39935,9 +39936,9 @@
".capitalize": 1,
"tail": 1,
"recursion": 1,
"val": 2,
"val": 6,
"headOfSong": 1,
"println": 2,
"println": 8,
"parameter": 1,
"name": 4,
"version": 1,
@@ -39964,10 +39965,10 @@
"<+=>": 1,
"baseDirectory": 1,
"map": 1,
"_": 1,
"_": 2,
"input": 1,
"add": 2,
"a": 2,
"a": 4,
"maven": 2,
"style": 2,
"repository": 2,
@@ -39978,8 +39979,8 @@
"of": 1,
"repositories": 1,
"define": 1,
"the": 4,
"to": 4,
"the": 5,
"to": 7,
"publish": 1,
"publishTo": 1,
"set": 2,
@@ -40017,7 +40018,7 @@
"false": 7,
"showSuccess": 1,
"timingFormat": 1,
"import": 1,
"import": 9,
"java.text.DateFormat": 1,
"DateFormat.getDateTimeInstance": 1,
"DateFormat.SHORT": 2,
@@ -40046,12 +40047,72 @@
"Credentials": 2,
"Path.userHome": 1,
"/": 2,
"math.random": 1,
"scala.language.postfixOps": 1,
"scala.util._": 1,
"scala.util.": 1,
"Try": 1,
"Success": 2,
"Failure": 2,
"scala.concurrent._": 1,
"duration._": 1,
"ExecutionContext.Implicits.global": 1,
"scala.concurrent.": 1,
"ExecutionContext": 1,
"CanAwait": 1,
"OnCompleteRunnable": 1,
"TimeoutException": 1,
"ExecutionException": 1,
"blocking": 3,
"node11": 1,
"Welcome": 1,
"Scala": 1,
"worksheet": 1,
"retry": 3,
"[": 11,
"T": 8,
"]": 11,
"n": 3,
"block": 8,
"Future": 5,
"ns": 1,
"Iterator": 2,
".iterator": 1,
"attempts": 1,
"ns.map": 1,
"failed": 2,
"Future.failed": 1,
"new": 1,
"Exception": 2,
"attempts.foldLeft": 1,
"fallbackTo": 1,
"scala.concurrent.Future": 1,
"scala.concurrent.Fut": 1,
"|": 19,
"ure": 1,
"rb": 3,
"i": 9,
"Thread.sleep": 2,
"*random.toInt": 1,
"i.toString": 5,
"ri": 2,
"onComplete": 1,
"s": 1,
"s.toString": 1,
"t": 1,
"t.toString": 1,
"r": 1,
"r.toString": 1,
"Unit": 1,
"toList": 1,
".foreach": 1,
"Iteration": 5,
"java.lang.Exception": 1,
"Hi": 10,
"HelloWorld": 1,
"main": 1,
"args": 1,
"Array": 1,
"[": 1,
"]": 1
"Array": 1
},
"Scaml": {
"%": 1,
@@ -41226,6 +41287,47 @@
"LazyFn": 2,
"LazyMemo": 1
},
"Stylus": {
"border": 6,
"-": 10,
"radius": 5,
"(": 1,
")": 1,
"webkit": 1,
"arguments": 3,
"moz": 1,
"a.button": 1,
"px": 5,
"fonts": 2,
"helvetica": 1,
"arial": 1,
"sans": 1,
"serif": 1,
"body": 1,
"{": 1,
"padding": 3,
";": 2,
"font": 1,
"px/1.4": 1,
"}": 1,
"form": 2,
"input": 2,
"[": 2,
"type": 2,
"text": 2,
"]": 2,
"solid": 1,
"#eee": 1,
"color": 2,
"#ddd": 1,
"textarea": 1,
"@extends": 2,
"foo": 2,
"#FFF": 1,
".bar": 1,
"background": 1,
"#000": 1
},
"SuperCollider": {
"BCR2000": 1,
"{": 14,
@@ -44542,7 +44644,7 @@
"Ruby": 3862,
"Rust": 3566,
"Sass": 56,
"Scala": 420,
"Scala": 750,
"Scaml": 4,
"Scheme": 3478,
"Scilab": 69,
@@ -44551,6 +44653,7 @@
"Slash": 187,
"Squirrel": 130,
"Standard ML": 243,
"Stylus": 76,
"SuperCollider": 268,
"Tea": 3,
"TeX": 1155,
@@ -44674,7 +44777,7 @@
"Ruby": 17,
"Rust": 1,
"Sass": 2,
"Scala": 3,
"Scala": 4,
"Scaml": 1,
"Scheme": 1,
"Scilab": 3,
@@ -44683,6 +44786,7 @@
"Slash": 1,
"Squirrel": 1,
"Standard ML": 2,
"Stylus": 1,
"SuperCollider": 2,
"Tea": 1,
"TeX": 1,
@@ -44704,5 +44808,5 @@
"Xtend": 2,
"YAML": 1
},
"md5": "4c42b3480fcf7bce2bf386e1982cc205"
"md5": "ba2022a3abc994a60ac4e0a820a0ad2d"
}

75
samples/Scala/node11.sc Normal file
View File

@@ -0,0 +1,75 @@
import math.random
import scala.language.postfixOps
import scala.util._
import scala.util.{Try, Success, Failure}
import scala.concurrent._
import duration._
import ExecutionContext.Implicits.global
import scala.concurrent.{ ExecutionContext, CanAwait, OnCompleteRunnable, TimeoutException, ExecutionException, blocking }
/* This worksheet demonstrates some of the code snippets from
* Week3, Lecture 4, "Composing Futures".
*/
object node11 {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
/**
* Retry successfully completing block at most noTimes
* and give up after that
*/
def retry[T](n: Int)(block: =>Future[T]): Future[T] = {
val ns: Iterator[Int] = (1 to n).iterator
val attempts: Iterator[()=>Future[T]] = ns.map(_ => ()=>block)
val failed: Future[T] = Future.failed(new Exception)
attempts.foldLeft(failed)((a, block) => a fallbackTo { block() })
} //> retry: [T](n: Int)(block: => scala.concurrent.Future[T])scala.concurrent.Fut
//| ure[T]
def rb(i: Int) = {
blocking{Thread.sleep(100*random.toInt)}
println("Hi " ++ i.toString)
i + 10
} //> rb: (i: Int)Int
def block(i: Int) = {
println("Iteration: " + i.toString)
val ri = retry(i)( Future {rb(i)} )
ri onComplete {
case Success(s) => println(s.toString ++ " = 10 + " ++ i.toString)
case Failure(t:Exception) => println(t.toString ++ " " ++ i.toString)
case r => println(r.toString ++ " " ++ i.toString)
}
} //> block: (i: Int)Unit
/* Multiple executions of a block of commands where
* each block contains one collectCoins and
* one buyTreasure. If either call fails, the whole iteration does not fail,
* because we are catching exceptions (with flatMap) in this implementation.
* Note that these blocks execute synchrounsly.
*/
(0 to 4 toList).foreach(i =>block(i)) //> Iteration: 0
//| Iteration: 1
//| java.lang.Exception 0
//| Hi 1
//| Iteration: 2
//| 11 = 10 + 1
//| Hi 2
//| Iteration: 3
//| Hi 3
//| Hi 2
//| Iteration: 4
//| 12 = 10 + 2
blocking{Thread.sleep(3000)} //> Hi 4
//| Hi 3
//| 13 = 10 + 3
//| Hi 3
//| 14 = 10 + 4
//| Hi 4
//| Hi 4
//| Hi 4-
}