Skip to content

Instantly share code, notes, and snippets.

View ajantis's full-sized avatar
:shipit:

Dmitry Ivanov ajantis

:shipit:
View GitHub Profile
@ajantis
ajantis / base.sh
Last active September 30, 2017 18:09
Base shell script
#!/usr/bin/env bash
# Debug
#set -x
# Strict mode:
# - Abort on first (line) error in the script.
# - Fail on first error in the pipe.
# - Fail on unset variables.
set -euo pipefail
@ajantis
ajantis / gist:b0b90bb916560436405c
Created July 10, 2015 22:28
Shapeless generics
// See also https://github.com/davegurnell/shapeless-workshop-scalax-2014
scala> import shapeless._
scala> import derivation._
scala> case class Dude(num: Int, str: String)
defined class Dude
@ajantis
ajantis / gist:9359fc964a7ba53b40d6
Created July 4, 2015 10:38
Simple ad-hoc serialization/deserialization using Type Classes
// Assuming that persistence layer knows how to read and write ItemValue objects to DB
trait DbFormat[T] {
def read(value: ItemValue): T
def write(obj: T): ItemValue
}
trait PersistenceLayer {
def store(obj: T)[T : DbFormat] = {
val format = implicitly[DBFormat[T]]
val item: ItemValue = format.write(obj)
@ajantis
ajantis / spellchecker.hs
Created March 26, 2014 21:32
Haskell simple spellchecker
import Data.Maybe (isJust)
import System.IO
main :: IO ()
maybeContains :: Eq a => a -> [a] -> Maybe a
maybeContains a l = if (elem a l) then Just a else Nothing
readWords :: String -> IO [String]
readWords fileName = do
@ajantis
ajantis / gist:9533270
Last active August 29, 2015 13:57
AkkaFileProcessorExample
class FileProcessor extends Actor {
var lineCounter = 0
val processor = context.actorOf(Props(new LineProcessor).withRouter(RoundRobinRouter(10))
def receive = {
case ProcessFile(filePath) =>
val src = scala.io.Source.fromFile(file.getOrElse("data.txt"))
src.getLines.foreach { line =>
lineCounter += 1
@ajantis
ajantis / akka_actors_and_nested_futures
Last active January 4, 2016 20:10
Akka actor dispatcher and nested futures
import akka.actor._
import scala.concurrent.duration._
import scala.concurrent.Future
object MyApp extends App {
val actorSystem = ActorSystem()
val myActor = actorSystem.actorOf(Props[MyActor])
myActor ! "Hello!"
}
@ajantis
ajantis / listens
Created June 26, 2013 11:59
Simple command line util for Mac OS to check who is listening on a provided port. Usage: listens <port>
#!/bin/sh
EXPECTED_ARGS=1
E_BADARGS=65
if [ $# -ne $EXPECTED_ARGS ]
then
echo "Usage: `basename $0` {port}"
exit $E_BADARGS
fi