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
case class Trie[V](value: Option[V], children: List[Option[Trie[V]]]) { | |
def insert(key: String, value: V): Trie[V] = Trie.insert(this, key, value, 0) | |
def delete(key: String): Trie[V] = Trie.delete(this, key, 0) | |
def search(key: String): Option[V] = Trie.search(this, key, 0) | |
} | |
object Trie { | |
def empty[V]: Trie[V] = new Trie[V](None, List.fill(26)(None)) |
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
sealed trait Ternary[+A] { | |
def insert[B >: A](key: String, value: B): Ternary[B] = Ternary.insert(this, key, value, 0) | |
def search(key: String): Option[A] = Ternary.search(this, key, 0) | |
def keys: List[String] = Ternary.keys(this) | |
def keysWithPrefix(prefix: String): List[String] = Ternary.keys(this, prefix) | |
} |
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 TrieApp extends App { | |
val trie = Trie[Int] | |
.insert("to", 7) | |
.insert("a", 15) | |
.insert("tea", 3) | |
.insert("ted", 4) | |
.insert("ten", 12) | |
.insert("i", 11) | |
.insert("in", 5) | |
.insert("inn", 9) |
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 TernaryApp extends App { | |
val ternary = Ternary[Boolean] | |
.insert("East Richmond", true) | |
.insert("East Eagle", true) | |
.insert("Richmond West", true) | |
.insert("Cheltenham", true) | |
.insert("Richmond VIC", true) | |
println(ternary.keys) | |
// List(Cheltenham, East Eagle, East Richmond, Richmond VIC, Richmond West) |
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 DockerClientApp extends App { | |
import DockerClientOps._ | |
val client:DockerClient = DefaultDockerClient.fromEnv().build() | |
val interpreter = spotifyInterpreter(client) | |
val program = for { | |
_ <- pull("busybox:latest") | |
cntrId <- run("busybox", "sh", "-c", "while :; do sleep 1; done") |
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
for { | |
_ <- pull("busybox:latest") | |
cntr <- run("busybox", "sh", "-c", "while :; do sleep 1; done") | |
res <- exec(cntr, "date") | |
_ <- kill(cntr) | |
_ <- remove(cntr) | |
} yield res | |
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
sealed trait DockerOperation[A] | |
case class Pull(image:String) extends DockerOperation[String] | |
case class Run(image:String, command:String*) extends DockerOperation[String] | |
case class Exec(containerId:String, command:String*) extends DockerOperation[String] | |
case class Kill(containerId:String) extends DockerOperation[Unit] | |
case class Remove(containerId:String) extends DockerOperation[Unit] |
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 pull(image:String): Free[DockerOperation, String] = Free.liftF(Pull(image)) | |
def run(image:String, command:String*): Free[DockerOperation, String] = Free.liftF(Run(image, command)) | |
def exec(containerId:String, command:String*): Free[DockerOperation, String] = Free.liftF(Exec(containerId, command)) | |
def kill(containerId:String): Free[DockerOperation, String] = Free.liftF(Kill(containerId)) | |
def remove(containerId:String): Free[DockerOperation, String] = Free.liftF(Remove(containerId)) |
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 spotifyInterpreter(client: DockerClient): DockerOperation ~> Try = new (DockerOperation ~> Try) { | |
override def apply[A](fa: DockerOperation[A]): Try[A] = fa match { | |
case Pull(image: String) => Try { | |
client.pull(image) | |
} | |
case Run(image: String, command@_*) => Try { | |
val config = ContainerConfig.builder().cmd(command:_*).image(image).build() | |
val container:ContainerCreation = client.createContainer(config) | |
client.startContainer(container.id()) |
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 interpreter = spotifyInterpreter(DefaultDockerClient.fromEnv().build()) | |
val program = for { | |
_ <- pull("busybox:latest") | |
cntr <- run("busybox", "sh", "-c", "while :; do sleep 1; done") | |
res <- exec(cntr, "date") | |
_ <- kill(cntr) | |
_ <- remove(cntr) | |
} yield res |
OlderNewer