This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
println(List(1,2,3).foldLeft(0)((b,a) => b+a)) | |
println(List(1,2,3).reduceLeft((b,a) => b+a)) | |
// reverse a string with foldleft | |
def reverseStr(s: String) = (s.toList.foldLeft(List[Char]()) {(xs, x) => {x :: xs}}).mkString | |
println(reverseStr("hejsan")) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// print mult table 1 to 12 as a square | |
for( a <- (1 to 12); b <- (1 to 12)){ | |
print("%4s".format(a*b)) | |
if (b == 12) println("") | |
} | |
println("") | |
// is sugar for | |
(1 to 12).foreach( a => (1 to 12).foreach( b => { | |
print("%4s".format(a*b)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Coder(words: List[String]) { | |
def ?? = error("not implemented" ) | |
private val mnemonics = Map( | |
'2' -> "ABC", '3' -> "DEF", '4' -> "GHI", '5' -> "JKL", | |
'6' -> "MNO", '7' -> "PQRS", '8' -> "TUV", '9' -> "WXYZ") | |
/** Invert the mnemonics map to give a map from chars 'A' ... 'Z' to '2' ... '9' */ | |
val charCode: Map[Char, Char] = | |
for( (k,v) <- mnemonics; letter <- v ) yield (letter -> k) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static final Pattern pattern = Pattern.compile("^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$"); | |
public static boolean isBase642(String base64) { | |
final Matcher m = pattern.matcher(base64); | |
return m.matches(); | |
} | |
// with guava | |
// this is not really correct as = should only be allowed in the end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import java.lang.{reflect => jreflect} | |
import scala.reflect.mirror._ | |
/** | |
* Scala counterpart of java.lang.reflect.InvocationHandler | |
*/ | |
trait InvocationHandler { | |
def invoke(proxy: AnyRef, method: Symbol, args: Array[AnyRef]): AnyRef |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scalaxb.DataRecord | |
val x = scalaxb.fromXML[cusin.bo.MobileSubscriptionServicesAndCustomer](xml) | |
// DataRecord(namespace,type,...) | |
x.Body.bodyoption match { | |
//case DataRecord(a,b,c) => print("\na="+ a + "\nb= " + b + "\nc=" + c) | |
case DataRecord(_,Some("subscription_insert"),_) => println("insert") | |
case DataRecord(_,Some("subscription_update"),_) => println("update") | |
case DataRecord(_,Some("subscription_delete"),_) => println("delete") | |
case DataRecord(a,b,c) => error("invalid choice " + a + b) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def enumResultSet[E,A](rs: ResultSet, iter: IterV[E, A], get: ResultSet => IO[E]): IO[IterV[E, A]] = { | |
def loop(i: IterV[E, A]): IO[IterV[E, A]] = | |
i.fold(done = (_, _) => i.pure[IO], | |
cont = k => next(rs) >>= (hasMore => | |
if (!hasMore) i.pure[IO] | |
else get(rs) >>= (t => loop(k(El(t)))))) | |
loop(iter) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
SCRIPT="$(cd "${0%/*}" 2>/dev/null; echo "$PWD"/"${0##*/}")" | |
DIR=`dirname "${SCRIPT}"}` | |
exec scala $0 $DIR $SCRIPT | |
::!# | |
import java.io.File | |
object App { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fs = (0 to 1000) map (i => Future(i, 10000)) | |
def get = Futures.fold(ArrayBuffer.empty[AnyRef], 10000)(fs) { | |
case (l, i) if i%2==0 => l += i.asInstanceOf[AnyRef] | |
case (l, _) => l | |
}.await.result.get.asInstanceOf[ArrayBuffer[Int]].sum | |
(0 to 1000) foreach (_ => assert(get == 250500)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cusin.filesplitter2 | |
import scalax.io._ | |
import Resource._ | |
import java.util.concurrent.TimeUnit | |
import com.programmera.timer._ | |
import akka.actor.Actor.actorOf | |
import akka.actor.Actor | |
import akka.actor.ActorRef | |
import akka.dispatch.Dispatchers |