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
### Keybase proof | |
I hereby claim: | |
* I am alirezameskin on github. | |
* I am alireza_meskin (https://keybase.io/alireza_meskin) on keybase. | |
* I have a public key ASCewzjXVWJG2wlv1NZWq0vAs6EALAAxT3d89M6FsPoZ-go | |
To claim this, I am signing this object: |
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 partition[T <% Ordered[T]](elm: T, list: List[T]): (List[T], List[T]) = { | |
doPartition(elm, list, Nil, Nil) | |
def doPartition[T <% Ordered[T]](elm: T, list: List[T], smallerItems: List[T], largerItems: List[T]): (List[T], List[T]) = | |
list match { | |
case Nil => (smallerItems, largerItems) | |
case head :: tail => | |
if (head < elm) | |
doPartition(elm, tail, head :: smallerItems, largerItems) | |
else |
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 split[T <% Ordered[T]](list: List[T]): (List[T], List[T]) = | |
list match { | |
case Nil => (Nil, Nil) | |
case head :: Nil => (head :: Nil, Nil) | |
case first :: second :: tail => | |
val (tl1, tl2) = split(tail) | |
(first :: tl1, second :: tl2) | |
} | |
def merge[T <% Ordered[T]](list1: List[T], list2: List[T]): List[T] = |
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 insertElement[T <% Ordered[T]](elm: T, sorted: List[T]): List[T] = | |
sorted match { | |
case Nil => elm :: sorted | |
case head :: tail if head < elm => head :: insertElement(elm, tail) | |
case _ => elm :: sorted | |
} | |
def insertionSort[T <% Ordered[T]](list: List[T]): List[T] = | |
list match { | |
case Nil => list |
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 selectionSort[T <% Ordered[T]](data: List[T]): List[T] = | |
data match { | |
case Nil => Nil | |
case head :: Nil => List(head) | |
case head :: tail => | |
val smallest = tail.min | |
val smallIndex = tail.indexOf(smallest) | |
if (head <= smallest) | |
head :: selectionSort(tail) |
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 getLargest[T <% Ordered[T]](data: List[T]): (T, List[T]) = | |
data match { | |
case head :: Nil => (head, Nil) | |
case head :: tail => | |
val (large, remaining) = getLargest(tail) | |
if (large > head) | |
(large, head :: remaining) | |
else | |
(head, large :: remaining) | |
} |
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 |
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
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
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] |
NewerOlder