Skip to content

Instantly share code, notes, and snippets.

@kirked
kirked / Atom.scala
Last active July 4, 2022 10:06
Clojure atoms, in Scala
/*------------------------------------------------------------------------------
* MIT License
*
* Copyright (c) 2017 Doug Kirk
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
@kirked
kirked / class-utils.cljs
Last active July 2, 2023 21:31
HTML element class manipulations in Clojurescript (no jQuery)
(defn classes-of
"Get the classes of an element as a Clojure keyword vector."
[e]
(let [words (-> e (.getAttribute "class") (string/split " "))]
(mapv keyword words)))
(defn classes->str
"Change a Clojure keyword seq into an HTML class string."
[classes]
(->> classes (mapv name) (string/join " ")))
@kirked
kirked / dump.scala
Created August 4, 2016 20:56
hexdump -C compatible dump in Scala
def dump(s: java.io.InputStream): Unit = {
val buf = new StringBuilder(64)
val chars = new StringBuilder(32)
var offset = 0
def dumpLine = {
println("%08x %48s |%16s|".format(offset, buf, chars))
buf.setLength(0)
chars.setLength(0)
offset += 0x10
@kirked
kirked / ZipEnumerator.scala
Last active January 29, 2023 23:07
Create a zip file on-the-fly using Play framework (nonblocking, iteratee, stream, S3, future, low memory use)
/*------------------------------------------------------------------------------
* MIT License
*
* Copyright (c) 2016 Doug Kirk
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
@kirked
kirked / Find.scala
Last active June 2, 2017 05:48
The `Find` monad combines `Option` and `Try`
import scala.util.{Try, Success, Failure}
import scala.util.control.NonFatal
object Find {
def empty[A]: Find[A] = NotFound
def apply[A](value: => A): Find[A] =
try {
if (value == null) NotFound else Found(value)
}
@kirked
kirked / WorkerPool.scala
Created October 23, 2015 23:44
Akka worker pool; backpressure configurable via dispatcher configuration.
package actors
import akka.actor._
import akka.pattern.ask
import scala.collection.immutable.Queue
import scala.concurrent.duration._
import scala.util.{Success, Failure}
object WorkerPool {
val defaultTimeout = 30.seconds
@kirked
kirked / Generator.scala
Created September 21, 2015 19:18
Generator for test data
import scala.util.Random
trait Generator[A] extends Function0[A] {
val random = new Random
def apply: A
}
object Generator {
@kirked
kirked / JsonLens.scala
Last active June 8, 2022 07:48
A lens wrapper over spray-json
import scala.reflect.ClassTag
import scala.util.{Either, Left, Right}
import spray.json._
object JsonLens {
class Json(val value: Option[JsValue]) {
def /(name: String): Json = Json(value.flatMap(_.asJsObject.fields.get(name)))
def -(name: String): Json = Json(value.map(obj => JsObject(obj.asJsObject.fields - name)))
def apply(index: Int): Json = {
@kirked
kirked / codingame-asciiart.scala
Last active August 29, 2015 14:24
Straightforward implementation of CodingGame ASCII art problem in Scala
object Solution extends App {
def readLetters(width: Int, height: Int): Array[Array[String]] = {
val chars = for (i <- 0 until height) yield readLine.grouped(width).toArray
chars.toArray
}
def letterIndex(c: Char): Int = if (c < 'A' || c > 'Z') 26 else c - 'A'
val l = readInt