Skip to content

Instantly share code, notes, and snippets.

View eneroth's full-sized avatar

Henrik Eneroth eneroth

View GitHub Profile
@eneroth
eneroth / rama_electric.clj
Last active September 8, 2023 15:39
Rama + Electric
;; ## General helpers
(ns app.helpers.rama
(:require
[missionary.core :as m]
[taoensso.timbre :as timbre])
(:import
[com.rpl.rama Depot PState Path ProxyState$Callback RamaModule]
[com.rpl.rama.test InProcessCluster LaunchConfig]
[hyperfiddle.electric Failure Pending]))
(ns example
(:require [com.rpl.specter :as sp]))
;; Find paths
;; #######################################
(defn find-when
"Given a nested data structure and a predicate, return a vector of
paths to all locations where the predicate is true."
[data pred]
@eneroth
eneroth / args-parse.clj
Last active November 19, 2019 14:27
Trivial K/V parser for a string of args
(require '[clojure.string :as string])
(def test-str "name\\=name=value\\=value;name=value;name=value\\;;name=value;")
(defn key-part [s]
(second (re-find #"(.*[^\\])=" s)))
(defn value-part [s]
(second (re-find #"[^\\]=(.*)" s)))
@eneroth
eneroth / poc_util_app.clj
Last active October 16, 2019 17:23
Custom add-watch function for Fulcro 3. Triggers a function based on a query.
(ns poc.util.app
(:require [com.fulcrologic.fulcro.algorithms.denormalize :as denormalize]))
(defn add-query-watch
"Takes at minimum an app, a registry-key, a query, a starting-entity, and a function.
Optionally takes an an options map as last argument.
Attaches a watch to the app state, using registry-key as the key for the watch.
When the queried state changes, triggers function f.
@eneroth
eneroth / cf_dsl.clj
Last active March 15, 2022 03:48
Minimal DSL for CloudFormation templates
;; DSL
;; ############################
(ns example.lib.cloudformation
(:require [camel-snake-kebab.core :refer [->PascalCase]]))
(defmacro defresource
"Defines a resource. The CF name of the resource will be the same
as the name given to it, except Pascal-cased, since that seems
to be the style favoured by CF. Optionally, a literal name
in the form of a keyword can be given as second argument to
@eneroth
eneroth / flip_attrs.clj
Last active February 27, 2019 18:32
Flips attributes around
(defn flip-attrs [attributes]
(let [one (clojure.string/split attributes #"&")
two (map #(clojure.string/split % #"=") one)
three (map reverse two)
four (map #(interpose "=" %) three)
five (interpose "&" four)
six (flatten five)]
(apply str six)))
(flip-attrs "tool=whip&clothing=hat&sidearm=pistol")
@eneroth
eneroth / deep_merge.clj
Last active November 20, 2018 04:24
Deep merge with vector, seq and set handling
(defn- deep-merge
"Takes a collection of maps and deep merges them. I.e...
[{:a {:b 1}}
{:a {:c 2}}]
... will be merged into ...
[{:a {:b 1
:c 2}}]
@eneroth
eneroth / is-balanced.clj
Created September 16, 2018 11:40
Uses reduce to check if brackets are balanced
(def brackets {\{ \}
\[ \]
\( \)})
(defn checker [stack char]
(if (some #{char} (keys brackets)) ;; Is the character an opening bracket?
(conj stack char)
(let [closing-char (get brackets (last stack))]
(if (= closing-char char)
@eneroth
eneroth / p_apply.clj
Last active October 10, 2018 17:23
Parallel application of a function over arguments, constrained by worker count
(defn- process-one
"Takes a channel and a function. Expects vectors of [index arguments]
on the channel. Takes from the channel, and applies the function
to the arguments part of the vector. Returns an output-channel,
on which the index and the result of the function application is returned."
[in-ch function]
(let [out-ch (chan)]
(go
(loop [item (<! in-ch)]
(if item
@eneroth
eneroth / strip_nil.clj
Last active August 2, 2018 14:06
Recursively strips values that are nil from a data structure. Will strip entire branches if they recursively evaluate to nil
(defn safe-seq
[thing]
(if (seqable? thing)
(seq thing)
true))
(defprotocol StripNil
(strip-nil [data]
"Recursively strips values that are nil from a datastructure.