Skip to content

Instantly share code, notes, and snippets.

@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 / Tree.scala
Created September 1, 2017 19:55
Basic tree data structure in Scala
sealed trait Tree[+A] {
def value: A
def children: Vector[Tree[A]]
def get: Option[A]
def isEmpty: Boolean
final def nonEmpty: Boolean = !isEmpty
def isLeaf: Boolean
@kirked
kirked / config.scala
Created October 11, 2019 14:44
Scala syntax augmentation for Typesafe config
import com.typesafe.config.{Config, ConfigException, ConfigFactory}
import java.time.{Duration => JDuration}
import scala.collection.JavaConverters._
import scala.reflect.ClassTag
import scala.util.Try
object config {
class ConfigOps(config: Config) {
def booleanOption(path: String): Option[Boolean] =
if (config.hasPath(path)) Some(config.getBoolean(path)) else None
@kirked
kirked / TemporalCache.scala
Created June 19, 2017 22:36
Temporal Cache
package actors
import akka.actor.{Actor, ActorLogging, Props}
import java.time.Instant
import scala.collection.immutable.TreeMap
import scala.concurrent.duration._
object TemporalCache extends ActorGenerator1[TemporalCache, FiniteDuration] {
def props(checkExpiration: FiniteDuration = 60.seconds) =
Props(new TemporalCache(checkExpiration))
@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 / Clojure Resources.md
Last active July 9, 2022 01:59
A coworker wrote this in a private repo, and I want to keep it as a reference…

Rich Hickey

Rich is the BDFL of Clojure. He's a great speaker with lots of ideas to offer concerning problem solving and separating time, state, value, and identity. His thoughts on these things influenced the design of Clojure and Datomic (an immutable database that supports time-travel).

All of his talks are good but here are a few to start with:

@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 / 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 / ZipToStreamFlow.scala
Last active March 6, 2020 21:28
Akka streams implementation of constant-space (per stream) zip file creation.
/*------------------------------------------------------------------------------
* 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 / 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,