Skip to content

Instantly share code, notes, and snippets.

View arturopala's full-sized avatar
🎯
Focusing

Artur Opala arturopala

🎯
Focusing
View GitHub Profile
import java.util.{Timer, TimerTask}
import java.util.Date
import scala.concurrent._
import scala.concurrent.duration.FiniteDuration
import scala.util.Try
import scala.util.Success
import scala.util.Failure
object Schedule {
@arturopala
arturopala / AnyWordSpecCompat.scala
Last active April 27, 2020 07:17
Scala trait adding ScalaTest's AnyWordSpec compatibility to MUnit
import scala.reflect.ClassTag
trait AnyWordSpecCompat extends munit.FunSuite {
implicit class NameExt[T](name: String) {
def in(body: => T): Unit = test(name)(body)
def should(body: => T): Unit = {
val pos0 = munitTestsBuffer.length
@arturopala
arturopala / ActionChain.scala
Last active April 22, 2020 14:11
Type safe action chain ADT
sealed trait Direction
sealed trait East extends Direction
case object East extends East
sealed trait West extends Direction
case object West extends West
sealed trait North extends Direction
case object North extends North
sealed trait South extends Direction
case object South extends South
@arturopala
arturopala / GetRates.scala
Last active March 14, 2023 09:33
Get an official GBP-PLN currency exchange rates on the pay-dates using NBP API. Runnable using scala-cli command.
//> using scala "2.13.8"
//> using lib "com.typesafe.play::play-ahc-ws-standalone:2.1.3"
//> using lib "com.typesafe.play::play-ws-standalone-json:2.1.3"
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import akka.actor.ActorSystem
import play.api.libs.ws._
import play.api.libs.ws.ahc._
@arturopala
arturopala / ISO31661Alpha3.scala
Created April 16, 2020 14:02
ISO 31661 Alpha 3 country code to name dictionary for Scala
/**
* ISO 3166-1 alpha-3 codes are three-letter country codes defined in ISO 3166-1,
* part of the ISO 3166 standard published by the International Organization for Standardization (ISO),
* to represent countries, dependent territories, and special areas of geographical interest.
*/
object ISO31661Alpha3 {
def apply(code: String): Option[String] = code2Country.get(code)
final private val code2Country = Map(
@arturopala
arturopala / Buffer.scala
Last active April 22, 2020 08:17
Buffer
import scala.reflect.ClassTag
/** Mutable, indexed buffer abstraction.
*
* @groupprio Abstract 0
* @groupprio Properties 1
* @groupprio Update 2
* @groupprio Append 3
* @groupprio Insert 4
* @groupprio Shift 5
import java.nio.charset.Charset
import org.scalatest.{Matchers, WordSpec}
import scala.util.Random
class CRC5Spec extends WordSpec with Matchers {
/**
* Implements a "normal" MSB-first byte-width CRC5 function using a lookup table.
@arturopala
arturopala / FutureOfEitherT.scala
Last active May 5, 2017 09:16
Future of Etither monad transformer
object FutureOfEitherT {
type Result[T] = Either[Throwable, T]
implicit class FutureOps[T](val f: Future[T]) extends AnyVal {
implicit def orFail: FutureOfEither[T] =
FutureOfEither(f map Right.apply recover { case NonFatal(e) => Left(e) })
}
@arturopala
arturopala / LZW.scala
Created October 22, 2014 15:37
Scala LZW compress and decompress
import scala.collection.mutable.{ ListBuffer, HashMap }
object LZW {
private val initialDictionaryCompress: Seq[(String, Int)] = (0 until 256) map (i => ("" + i.toChar, i))
private val initialDictionaryDecompress: Seq[(Int, String)] = (0 until 256) map (i => (i, "" + i.toChar))
def compress(uncompressed: String): Seq[Int] = {
val dictionary = HashMap(initialDictionaryCompress: _*)
val result = new ListBuffer[Int]()
@arturopala
arturopala / FrogStorkGameExample
Last active August 29, 2015 14:03
FrogStorkGame example of Scala language dealing with complexity
object Example3 extends App {
import games.board.xgame._
val game = new XGame() with reporting.ConsoleReporter
implicit val board = game.board(5, 5)
import Commands._
import BluePlayer.{ Frog => BlueFrog, Stork => BlueStork }
import RedPlayer.{ Frog => RedFrog, Stork => RedStork }