Skip to content

Instantly share code, notes, and snippets.

View akirillov's full-sized avatar

Anton Kirillov akirillov

View GitHub Profile
@akirillov
akirillov / DockerShortcuts.md
Created June 29, 2018 18:51
Docker shortcuts

Remove all images:

docker images --format "{{.ID}}" | xargs docker rmi

Stop and remove all containers:

docker ps -a --format "{{.ID}}" | xargs docker kill
docker ps -a --format "{{.ID}}" | xargs docker rm
//Let's say we have a list of Ints and function f from Int to Option:
val list = (1 to 10).toList
//List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
val f = (x: Int) => if (x%2==0) Some(x*2) else None
//f: Int => Option[Int]
//Now I'm going to apply f to the elements of the list and filter out empty results:
list map f flatten
/*
for the test purposes function getValueAsync imitates some heavy lifting
and returns type constructor parametrized by T (same for getValueOpt)
in real life these could be different functions returning some type
constructors like Option, List and Future which values
should then be passed to a function which takes proper types
Apply type class from cats has methods with a flavor like that:
@akirillov
akirillov / StackableTraits.scala
Last active July 13, 2016 08:41
Example of stackable traits in Scala
type Payload = Array[Byte]
trait Driver {
def send(payload: Payload)
}
//a component which depends on Driver interface
class Sender(val driver: Driver){
def send(message: String) = driver.send(message.getBytes)
}
@akirillov
akirillov / TailrecVsForeach.scala
Last active July 6, 2016 15:34
Comparison of tail recursive and imperative way of `map` implementation
/*
Test: tail recursive map vs imperative map
*/
object TailrecTest extends App{
def measure[A](f: => A): (A, Long) = {
val start = System.currentTimeMillis()
(f, System.currentTimeMillis() - start)
}
/*
object RabbitToRabbitFlow extends App {
implicit val actorSystem = ActorSystem("rabbit-akka")
implicit val materializer = ActorMaterializer()
val config = ConfigFactory.load("application.conf")
val sourceConnection = Connection(config.getConfig("source"))
val targetConnection = Connection(config.getConfig("target"))
val rabbitConsumer = Source.fromPublisher(sourceConnection.consume(config.getString("source.queue_name")))
@akirillov
akirillov / folds.hs
Created November 5, 2015 21:53
Library functions implementation with fold (from LYAHFGG)
maximum' :: (Ord a) => [a] -> a
maximum' = foldr1 (\x acc -> if x > acc then x else acc)
reverse' :: [a] -> [a]
reverse' = foldl (\acc x -> x : acc) []
product' :: (Num a) => [a] -> a
product' = foldr1 (*)
filter' :: (a -> Bool) -> [a] -> [a]
@akirillov
akirillov / ad-hoc_polymorphism.scala
Last active October 20, 2015 17:57
Simple example of ad-hoc polymorphism in Scala
//single parameter function for different types
trait Size[A] {
def size(a: A): Int
}
def size[A: Size](a: A): Int = implicitly[Size[A]].size(a)
implicit object StringSize extends Size[String]{
override def size(a: String): Int = a.length
}
@akirillov
akirillov / cassandra.yaml
Last active August 29, 2015 14:25
Different combinations of listen/rpc addresses for Docker/EC2/hardware setups
# Docker ===========================
# MacOS specific command to connect from host machine: cqlsh `boot2docker ip`
# Cassandra 1.2.9
listen_address: 127.0.0.1
rpc_address: 0.0.0.0
# Cassandra 2.1
listen_address: 127.0.0.1
rpc_address: 0.0.0.0
@akirillov
akirillov / MonoidPML.scala
Last active August 29, 2015 14:19
Pimp my library with monoid operations on list
package io.datastrophic
trait Monoid[A]{
def append(a1: A, a2: A): A
def unit: A
}
object Monoid{
implicit val IntMonoid = new Monoid[Int]{
def append(a1: Int, a2: Int): Int = a1 + a2