Skip to content

Instantly share code, notes, and snippets.

View izmailoff's full-sized avatar
🎯
Focusing

Aleksey Izmailov izmailoff

🎯
Focusing
View GitHub Profile
@izmailoff
izmailoff / TestingAkka.scala
Last active August 29, 2015 14:04
Akka children/grandchildren restart example
package io.github.izmailoff
import akka.actor._
import akka.actor.SupervisorStrategy.{Stop, Restart}
import scala.concurrent.duration._
class Supervisor extends Actor {
override def preStart =
println("STARTED SUPERVISOR")
@izmailoff
izmailoff / MongoMetaRecordExample.scala
Created August 1, 2014 10:17
Example of MongoRecord and MongoMetaRecord without using singleton meta object
// This is an example of how one can use MongoRecord and it's `meta` object MongoMetaRecord without
// using regular Scala singleton object.
//
// This way you can set Mongo DB connection identifier for each meta object and have multiple
// meta objects available. Usually you would not want to do that unless you use multiple databases
// in the same application. My motivation was that Spray unit tests would run concurrently with Akka
// ActorSystem and regular Lift dependency injection would not work because it's scope is ThreadLocal.
//
// Some more details here:
// https://groups.google.com/forum/#!topic/liftweb/5NfI9DxQ99Q
@izmailoff
izmailoff / LazyMap.scala
Created August 2, 2014 04:35
A class that wraps 2 maps in it and makes it look like a single map
// this is unfinished, but shows the idea
// It's an answer to:
// http://stackoverflow.com/questions/25028214/merging-scala-maps#comment38926797_25028214
package com.izmailoff
class LazyMap[A, B, C, D](x: Map[A, B], y: Map[A, C], f: (Option[B], Option[C]) => Option[D])
extends Map[A, D] {
def get(key: A): Option[D] =
@izmailoff
izmailoff / TraitLinearization.scala
Created August 3, 2014 13:41
Trait linearization examples
// This was run in Scala REPL:
scala> trait A {
| println("CREATED A")
| def n: String
| }
defined trait A
scala>
@izmailoff
izmailoff / SchemaMigration.scala
Last active January 21, 2022 18:22
Liquibase for Scala
// Liquibase SBT dependency is something like this:
// "org.liquibase" % "liquibase-core" % "3.0.5"
import java.sql.Connection
import liquibase.Liquibase
import liquibase.resource.FileSystemResourceAccessor
import liquibase.database.DatabaseFactory
import liquibase.database.jvm.JdbcConnection
import liquibase.resource.ClassLoaderResourceAccessor
import net.liftweb.common.Logger
@izmailoff
izmailoff / FormFieldsToJson.scala
Last active March 31, 2023 07:53
Convert `Content-Type: multipart/form-data` to JSON format because I can't read the former
object FormFieldsToJson extends App {
if(args.length != 1) {
println("Usage: FormDataParser <form data>")
System.exit(1)
}
val fieldDelimiter = "----"
val fieldNLines = 3
val data = args(0)
@izmailoff
izmailoff / check_api.sh
Last active August 8, 2016 10:15
Check webpage/API continuously for availability
#!/bin/bash
# Keep calling some API every second and check that it's working (status 200).
# If any other HTTP status is received print an error.
#
# Useful for observing server downtime and such.
while true
do
status="$(curl -sL -w '%{http_code}' https://api.com/something -o /dev/null)"
@izmailoff
izmailoff / genDownloadCommands.sh
Created February 4, 2016 06:38
Generates unix commands (wget, youtube-dl) to download files based on the links found on a web page.
#!/bin/sh
exec scala "$0" "$@"
!#
import scala.io.Source
val url = "https://work.caltech.edu/lectures.html"
val html = Source fromURL(url) mkString
val href = """<a href="\s*(.+?)\s*">""".r
val links = (for (m <- href findAllMatchIn html) yield m group 1) toList
import scala.concurrent._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
def someFunction(a: Int, b: Int): Future[Int] = Future(a * b)
val args = Map(
(true, true) -> List((0,0), (0,1), (1,0), (1,1)),
(true, false) -> List((0,0), (1,0)),
(false, true) -> List((0,0), (0,1)),
@izmailoff
izmailoff / Stats.scala
Created February 8, 2016 00:47
Basic statistics functions (not optimized)
object Stats {
type Observations = List[Double]
def mean(xs: Observations) = xs.sum / xs.size
def variance(xs: Observations) = {
val m = mean(xs)
xs.map{ x => math.pow(x - m, 2) }.sum / (xs.size - 1)
}