This file contains 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
trait JavaJsonizer { def toJson: String } | |
class SimpleJavaJsonizer extends JavaJsonizer { | |
def toJson: String = """{"type":"Int", "pattern":"hello*"}""" | |
} | |
case class YourBuilder(underlying: JavaJsonizer, name: Option[String]) { | |
def build(newName: String): YourBuilder = copy(name = Some(newName)) | |
} |
This file contains 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
post { | |
entity(as[Foo]) { rq: Foo => | |
complete("aa") | |
} ~ getRqEnt { | |
val foo = Foo("boo") | |
val ett = Await.result(Marshal(foo).to[RequestEntity], 100 millis) | |
//val recEtt = {"a" : "thx1138"} | |
str => { | |
println(ett) | |
println(str) |
This file contains 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
val l = (1 to 3).map(x => ((x-1)*10 until x*10) toList) toList | |
l.foldRight(List.empty[Int]) { case (sl,acc) => sl.foldRight(acc) { case (v, l) => v :: l } } |
This file contains 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
from collections import deque | |
class LFUCache(object): | |
def __init__(self, capacity): | |
self.value = {} | |
self.resque = {} | |
self.capacity = capacity | |
self.evictionQueue = deque() | |
This file contains 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 isSubseq[T](s: Seq[T], ss: Seq[T]): Boolean = { | |
(ss /: s) { | |
case (Seq(), _) => Seq() | |
case (remaining, x) if(remaining.head == x) => remaining.tail | |
case _ => ss | |
} isEmpty | |
} |
This file contains 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 scala.util.{Try, Success, Failure} | |
def sequence[T](s: Seq[Try[T]]): Try[Seq[T]] = | |
(Try(Seq.empty[T]) /: s) { | |
case (Success(s), Success(v)) => Success(v +: s) | |
case (Failure(cause), _) => Failure(cause) | |
case (_, Failure(cause)) => Failure(cause) | |
} map (_.reverse) | |
This file contains 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 maxSumSubseq(s: Seq[Int]): Int = { | |
(Option.empty[Int] /: s) { | |
case (Some(acc), v) if(v < acc+v) => Some(acc+v) | |
case (_, v) => Some(v) | |
} max | |
} | |
def maxSumSubseqIdxs(s: Seq[Int]): ((Int, Int), Int) = { | |
val sums = (Option.empty[((Int, Int), Int)] /: s.zipWithIndex) { | |
case (Some(((i, _), acc)), (v, j)) if(v < acc+v) => |
This file contains 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
object SimulationCLI extends App { | |
type CommandAction = (Seq[String], ElevatorControlSystemAPI) => Option[ElevatorControlSystemAPI] | |
val commands: Map[String, CommandAction] = | |
Map( | |
"state" -> { (args, ecs) => | |
ecs.status.toSeq.sortBy(_._1) foreach { | |
case (elevatorId, ElevatorState(movement, floor)) => | |
val movementStr = movement map { |
This file contains 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
abstract class FuncStackable { | |
def f(x: Int): Int | |
} | |
class Fidentity extends FuncStackable { | |
override def f(x: Int): Int = x | |
} | |
trait NInput extends FuncStackable { | |
val n: Int |
This file contains 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
object Solution extends App { | |
case class Trie[KE, V](v: Option[V], children: Map[KE, Trie[KE,V]]) { | |
def insert(key: Seq[KE], v: V): Trie[KE, V] = | |
if(key.isEmpty) copy(v = Some(v)) | |
else { | |
val ke = key.head | |
val newChild = children.getOrElse(ke, Trie.empty).insert(key.tail, v) | |
copy(children = children + (ke -> newChild)) | |
} |
OlderNewer