Skip to content

Instantly share code, notes, and snippets.

View vvvvalvalval's full-sized avatar

Valentin Waeselynck vvvvalvalval

View GitHub Profile
@vvvvalvalval
vvvvalvalval / async_promise_listener_hack.clj
Last active August 29, 2015 14:10
Hack for attaching asynchronous callbacks to Clojure promises
(require '[clojure.core.async :as a])
(defn make-promise-listener!
"Creates a function that attaches handlers to Clojure promises.
Behind the scences, fires a loop in another logical thread that scans every refresh-period ms the promises that have been registered for completion,
and executes the each handler on the corresponding value."
[{:keys [refresh-period]
:or {refresh-period 10}}]
(let [next-index! (let [a (atom -1)] #(swap! a inc)) ;; to get unique keys to put in the map.
state (atom {})] ;; map of dummy unique keys to pending promise-handler pairs
@vvvvalvalval
vvvvalvalval / monger-populate.clj
Last active August 29, 2015 14:15
'populate foreign keys' functionality with Monger, emulates a subset of Mongoose's populate
(require '[monger.collection :as mc])
(require '[monger.operators :as mop])
(defn populate "Populates the given docs sequence by looking up the 'foreign key' as an :_id in `foreign-coll`.
`foreign-path` can be either a single key or a sequence of keys (as in get-in)
Assumes the foreign keys are ObjectIds or coercable to objectIds.
Returns a seq of the docs where the foreign keys have been updated to be the foreign documents, in the same order.
"
@vvvvalvalval
vvvvalvalval / gist:937f69ef04872b649e82
Created May 25, 2015 12:31
Managing the lifecycle of stateful reagent components with single-item :key-ed seqs
;; Instead of this version, where you have to manage the lifecycle of my-stateful-component with React lifecycle methods
(defn parent-component []
[:div
;; ...
[my-stateful-component dynamic-id arg1 arg2 etc.]
;; ...
])
;; ... you can use this trick
@vvvvalvalval
vvvvalvalval / schema-helper-tx-fn.edn
Last active August 29, 2015 14:25
Datomic Schema definition: using transaction function to declare attributes without compromising data-orientation
;; defining helper function
[{:db/id #db/id[:db.part/user]
:db/doc "Helper function for defining entity fields schema attributes in a concise way."
:db/ident :utils/field
:db/fn #db/fn {:lang :clojure
:require [datomic.api :as d]
:params [_ ident type doc opts]
:code [(cond-> {:db/cardinality :db.cardinality/one
:db/fulltext true
:db/index true
@vvvvalvalval
vvvvalvalval / schema_cleaner.clj
Last active August 29, 2015 14:25
discarding unknown key in schema
(ns bs.utils.schema-cleaner "TL;DR look in the tests how the `cleaner` function is used."
(:require [schema.core :as s]
[schema.coerce :as sco]
[schema.utils :as scu]
)
(:use clojure.repl clojure.pprint))
(deftype ^:private GarbageType [])
(def ^:private garbage-const (GarbageType.))
(defmacro m:p "Makes a map from a list of symbols, using the symbol names to create keyword keys.
---
(let [a 1 b 2 c \"3\"]
(m:p a b c))
=> {:a 1, :b 2, :c \"3\"}
" [& syms]
(reduce (fn [m sym]
(assert (symbol? sym) "m:p only accepts symbols")
(assoc m (keyword (name sym)) sym))
{} syms))
@vvvvalvalval
vvvvalvalval / _forkable-stores.md
Last active February 28, 2016 10:19
Forkable stores

This Gist shows how you may want to implement a forkable Ring Session Store for development and testing.

(set! *warn-on-reflection* true)
;; 3 competing implementations which test if a string is uppercase (note that the StringUtils one has some caveats regarding its semantics)
(defn p1 [^String s]
(= s (str/upper-case s)))
(defn p2 [^String s]
(every? (fn [^Character c] (Character/isUpperCase c)) s))
(import org.apache.commons.lang3.StringUtils)
(defn p3 [^String s]
(StringUtils/isAllUpperCase s))
@vvvvalvalval
vvvvalvalval / git-sim-merge
Created November 18, 2016 09:25
utility for knowing what other branches will be merged if you merge a git branch into another
#!/usr/bin/env node
var br1 = process.argv[2];
var br2 = process.argv[3];
var exec = require('child_process').exec;
function setDiff(s1, s2){
return s1.filter(function(e){
return s2.indexOf(e) < 0;
@vvvvalvalval
vvvvalvalval / supdate.clj
Last active December 26, 2016 22:48
'supdate': Clojure's update with superpowers
(ns utils.supdate
"A macro for transforming maps (and other data structures) using a spec reflecting the
schema of the value to transform.")
;; ------------------------------------------------------------------------------
;; Example Usage
(comment
;;;; nominal cases
(supdate