Created
November 22, 2013 01:30
-
-
Save whilo/7593262 to your computer and use it in GitHub Desktop.
Clojure like functional data functions for hy sketch.
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
(import [pysistence [make_list make_dict]]) | |
(defn atom [init-val]) | |
(defn swap! [atom fn]) | |
(defn massoc [as k v] | |
(kwapply (as.using) {k v})) | |
(defn assoc-in [as ks v] | |
(if (= (len ks) 1) | |
(massoc as (first ks) v) | |
(massoc as (first ks) | |
(assoc-in (get as (first ks)) (rest ks) v)))) | |
(defn get-in [as ks] | |
(if (= (len ks) 1) | |
(get as (first ks)) | |
(get-in (get as (first ks)) (rest ks)))) | |
(defn update-in [as ks f] | |
(let [[old (get-in as ks)] | |
[new (f old)]] | |
(assoc-in as ks new))) | |
;; (massoc (make-dict {:a 1}) :a 2) | |
;; (update-in (make-dict {:a (make-dict {:b 1})}) [:a :b] inc) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment