Last active
October 16, 2024 20:01
-
-
Save borkdude/5f9a4ae710217e893a9462ff90b6cac3 to your computer and use it in GitHub Desktop.
Vanilla Clojure vs. Specter vs. transducers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns specter-idea | |
(:require [com.rpl.specter :as specter :refer [setval ALL | |
NONE multi-path | |
selected? | |
pred | |
]])) | |
(def data ;; only x and y are allowed | |
[{:name "x" :rels ["x" "y"]} ;=> fine, keep as is | |
{:name "y" :rels ["x" "y" "z"]} ;=> keep only allowed rels: {:name "y" :rels ["x" "y"]} | |
{:name "y" :rels ["z"]} ;=> no allowed rels, remove map completely | |
{:name "z" :rels ["x" "y"]}] ;=> disallowed name, remove completely | |
) | |
(def disallowed? (complement #{"x" "y"})) | |
;; Vanilla Clojure | |
(keep | |
(fn [m] | |
(when-not (disallowed? (:name m)) | |
(when-let [new-rels (seq (remove disallowed? (:rels m)))] | |
(assoc m :rels new-rels)))) | |
data) | |
;;=> ({:name "x", :rels ("x" "y")} {:name "y", :rels ("x" "y")}) | |
;; Note that it doesn't maintain 'types', vector is converted to seq | |
;; Util | |
(defn SOME? [x] | |
(not (identical? NONE x))) | |
;; Specter | |
(setval | |
[ALL | |
(multi-path | |
(selected? :name disallowed?) | |
[SOME? :rels ALL disallowed?] | |
(selected? SOME? :rels empty?))] | |
NONE | |
data) | |
;;=> [{:name "x", :rels ["x" "y"]} {:name "y", :rels ["x" "y"]}] | |
;; Specter solution maintains types, nice if you need it | |
;; Vanilla Clojure which preserves types | |
(vec | |
(keep | |
(fn [m] | |
(if-not (disallowed? (:name m)) | |
(if-let [new-rels (seq (remove disallowed? (:rels m)))] | |
(assoc m :rels (vec new-rels))))) | |
data)) | |
;; Vanilla Clojure transducer version, also preserves types | |
(into [] | |
(comp | |
(remove #(-> % :name disallowed?)) | |
(map (fn [m] (update-in m [:rels] #(into [] (remove disallowed?) %)))) | |
(remove #(-> % :rels empty?))) | |
data) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Preserving the types is easy enough:
But I would probably do something like this with transducers (also maintains collection types):
Benchmarked it with criterium bench too, execution means:
seq rewrite: 1.685164 µs
transducers: 2.450658 µs
specter: 2.769364 µs