Skip to content

Instantly share code, notes, and snippets.

@osfameron
osfameron / psplit.clj
Created November 26, 2017 16:27
parallel split and large file editor
(import '[java.io RandomAccessFile])
(require '[clojure.math.numeric-tower :refer [ceil]])
(require '[clojure.string :as str])
(defrecord Piece [file start end])
(defn handle
([file]
(RandomAccessFile. file "r"))
@osfameron
osfameron / tramp.clj
Last active November 5, 2017 09:18
tramp-> sketch
;; Example
(defn make-query-request [{parent-id :parent-id}]
{:table-name "my-table"
:query {:parent-id parent-id}})
(defn make-get-request [id]
{:table-name "my-table"
:get {:s id}})
@osfameron
osfameron / xml.clj
Last active November 4, 2017 12:36
XML Literal syntax macro sketch in Clojure
(ns scratch.xml
(:require [clojure.data.xml :as xml])
(:gen-class))
(declare xml-fn)
(defn resolve-namespace [ns n]
(some-> n
symbol
((ns-aliases ns) n)
@osfameron
osfameron / managed-effects.clj
Created July 29, 2017 07:00
Managed effects
(defn tag [k & tuple]
(concat [k] tuple))
(def effect (partial tag :effect))
(defn tagged [k v]
(when (and (sequential? v) (= k (first v)))
(rest v)))
(defn run-effectfully [& args]
@osfameron
osfameron / pipeline.clj
Last active July 27, 2017 06:57
pipeline.clj
(ns scratch.pipe
(:gen-class))
;; # Stepping functions
(defn p-step
"Lift a function into a pipeline function that runs, then passes onto the
next function in the pipeline"
[f]
(fn [v p]
@osfameron
osfameron / neighbours.clj
Last active July 5, 2017 01:47
Neighbours in clojure - simple & overengineered solutions
(defn neighbours-simple [[x y]]
#{[(dec x) (dec y)] [x (dec y)] [(inc x) (dec y)]
[(dec x) y ] [(inc x) y]
[(dec x) (inc y)] [x (inc y)] [(inc x) (inc y)]})
(defn neighbours-zipped [[x y]]
(let [axis (juxt dec identity inc)]
(clojure.set/difference
(set (mapcat (partial map vector)
(repeat (axis x))
@osfameron
osfameron / sketch.clj
Last active May 19, 2017 20:04
Cloduino/Reagi sketch (untested)
;; in the library
;; ... [reagi :as r]
;; [cloduino :as c]
(defn digital-writer [board pin]
(c/pin-mode board pin OUTPUT)
(partial c/digital-write board pin))
(defn digital-reader [board pin]
(c/pin-mode board pin INPUT)
;; split-map sketch, after discussion with justin_smith
(defn split-map [fn m]
(->> m
(group-by fn)
(sort-by key)
(map (comp (partial into {}) val))))
(comment
(split-map (comp even? val) {:foo 1 :bar 2 :baz 3}))
@osfameron
osfameron / log.clj
Created February 14, 2017 23:10
Minimal logging library in Clojure (sketch)
;; # Minimal logging library
;;
;; Principles:
;;
;; - https://12factor.net/logs
;; - single appender, e.g. STDOUT
;;
;; - functions all the way down
;;
;; - just Clojure data-structures
@osfameron
osfameron / gist:265bb09b641194cd21c15670c90a526b
Last active February 5, 2017 17:23
recursive definition for recursive merge?
;;; this works
boot.user=> (def merge-2 (partial merge-with (partial merge-with merge)))
#'boot.user/merge-2
boot.user=> (merge-2 {:a {:b {:c 1}}} {:a {:b {:d 2}}})
{:a {:b {:c 1, :d 2}}}
;;; but this doesn't
boot.user=> (declare merge-rec)
#'boot.user/merge-rec
boot.user=> (def merge-rec (partial merge-with merge-rec))