Skip to content

Instantly share code, notes, and snippets.

View oluies's full-sized avatar

Örjan Angré (Lundberg) oluies

  • Sweden
  • 19:39 (UTC +02:00)
  • X @oluies
View GitHub Profile
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"))
@oluies
oluies / mul12.scala
Created May 28, 2011 17:34
print mult table 1 to 12 as a square
// 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))
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)
@oluies
oluies / gist:1208120
Created September 10, 2011 08:37
validate base64string
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
@oluies
oluies / Proxy.scala
Created October 3, 2011 07:08 — forked from gkossakowski/Proxy.scala
ScalaProxy example
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
@oluies
oluies / gist:1263253
Created October 5, 2011 00:23
scalaxb xsd:Choice match
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)
}
@oluies
oluies / enumResultSet.scala
Created October 8, 2011 16:27
enumResultSet
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)
}
@oluies
oluies / gist:1272665
Created October 8, 2011 18:29
shell scala script
#!/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 {
@oluies
oluies / gist:1273632
Created October 9, 2011 12:31
Futures.fold
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))
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