Skip to content

Instantly share code, notes, and snippets.

View alirezameskin's full-sized avatar

Alireza Meskin alirezameskin

View GitHub Profile
@alirezameskin
alirezameskin / keybase.me
Created January 20, 2021 09:46
keybase.md
### 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:
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
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] =
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
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)
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)
}
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
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())
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))
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]