Skip to content

Instantly share code, notes, and snippets.

View alirezameskin's full-sized avatar

Alireza Meskin alirezameskin

View GitHub Profile
@alirezameskin
alirezameskin / Trie.scala
Last active July 21, 2019 16:49
An immutable implementation of Trie (Prefix-tree) in Scala
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))
@alirezameskin
alirezameskin / Ternary.scala
Last active July 21, 2019 16:49
An immutable implementation of Ternary search tree in Scala
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)
}
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)
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)
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")
for {
_ <- pull("busybox:latest")
cntr <- run("busybox", "sh", "-c", "while :; do sleep 1; done")
res <- exec(cntr, "date")
_ <- kill(cntr)
_ <- remove(cntr)
} yield res
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]
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))
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())
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