Skip to content

Instantly share code, notes, and snippets.

View enforser's full-sized avatar

Matthew Fors enforser

View GitHub Profile
@enforser
enforser / lazy-side-effects.clj
Last active August 1, 2023 18:31
Lazy side effects in clojure - overcoming chunking
;; Unlike fully lazy languages such as Haskell, Clojure seqs often implement chunking.
;; This means that when accessing the first element of a sequence, 32 members will be evaluated.
;; An example of this can be seen by implementing some side effect in a map.
(first (map (fn [x] (prn x) x) (range 5))) ;; prints 0 through 4, then returns 0
;; The above example will return 0 (as expected), but you'll notice it prints off all 5 elements
;; of (range 5).
;; In the next case we can see that the evaluation caused by the access of the first element stops after
;; the first 32 elements are evaluated.
@enforser
enforser / effective-repl-development.clj
Last active November 15, 2018 15:53
A collection of some examples of things I do to make REPL development more effective.
;; Demonstrates the usage of vars that are useful when working in the REPL.
;; Created: November 14, 2018
;;
;; My suggested method of reading this file is to copy it into some project and actually
;; go through the code snippets and evaluate in the REPL to play around with them.
;;
;; WARNING: Some of the examples will create a file called 'output.txt' from the directory
;; that your REPL is started in.
;;
;;
;; This document is meant to demonstrate a common misunderstanding in how
;; clojure.async's go blocks handle parallel processing.
(require '[clojure.core.async :as a])
;; non-blocking core async example.
(time (let [n 1000
vals-coll (atom [])]
(let [pipeline-chan (a/chan 1)]
(dotimes [i n] (a/go (a/>! pipeline-chan i)))
@enforser
enforser / logger.clj
Last active February 25, 2021 17:53
;; I use these when I want to throw a lot of `time` functions into code,
;; as it allows me to append a log to it. Using log-time-str I can
;; pass the str to whatever logging library I'm using at the time,
;; to achieve any benefits from that.
(defmacro log-time
"prints msg and the approximate number of milliseconds it took to
eval the given expression. Returns the result of the expr."
[msg expr]
`(let [start-time# (System/nanoTime)
### Keybase proof
I hereby claim:
* I am enforser on github.
* I am enforser (https://keybase.io/enforser) on keybase.
* I have a public key ASAA7YHJZ-i09gJFWLxJKP2fUwHfgfFpu0GbvgvXvGYCcAo
To claim this, I am signing this object: