Skip to content

Instantly share code, notes, and snippets.

@kirked
kirked / mk-deps.clj
Last active July 23, 2024 12:55
Make deps.edn from project.clj - babashka script
#!/usr/bin/env bb
(require '[babashka.fs :as fs])
(require '[clojure.pprint :refer [pprint] :as pprint])
(defn warn
[& args]
(binding [*out* *err*]
(apply println args)))
@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 / 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 / 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,
@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 / 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 / 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 / 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 / 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 / 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