Skip to content

Instantly share code, notes, and snippets.

@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
@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 / 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 / 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 / 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 / success.cljs
Last active June 2, 2017 21:16
re-frame success! interceptor
(ns interceptors.success
(:require [re-frame.core :as rf]
[clojure.string :as string]))
(defn success-or-failure
"Tests whether an event keyword is a success or failure callback,
and returns one of :success, :failure, or nil."
[event]
(cond
@kirked
kirked / scroll_container.cljs
Created June 19, 2017 22:45
Scroll an HTML container to its bottom over a period of time (dashboard use, no user input required)
(defn element-with-id
"Return the element with the given ID."
[id]
(js/document.getElementById id))
(defn resolve-element
"Given an ID or a CSS selector or an element, return the first matching document element."
[selector]
(cond
@kirked
kirked / debug-edn.clj
Last active May 22, 2018 16:01
Rudimentary EDN problem finder
(defn parse-string
"Parse a literal string, returning the [line col index] of the following character.
start-ix should point to the first character inside the literal string, not the opening quote."
[start-line start-col start-ix text]
(loop [line start-line
col start-col
ix start-ix]
(if (>= ix (.length text))
@kirked
kirked / luhn.scala
Last active April 5, 2019 18:35
Fast Luhn credit card check in Scala
final object Luhn {
/** An O(1) Map[Int, Int] of precomputed double values. */
private[this] final val doubles = Array(0, 2, 4, 6, 8, 1, 3, 5, 7, 9)
/**
* Validate a card using the Luhn algorithm with a single pass through the string,
* no other validation needed prior.
*
* Pure speed here (only 1 function call to get the byte array).
*/
@kirked
kirked / FixScalacOptionsInConsole.scala
Created August 10, 2019 16:04
SBT plugin - console - relax compiler options
import sbt._
object FixScalacOptionsInConsole extends AutoPlugin {
import Keys._
override def requires = plugins.JvmPlugin
override def trigger = allRequirements
override lazy val projectSettings = Seq(
Compile / console / scalacOptions ~= filter,