Skip to content

Instantly share code, notes, and snippets.

;; given this data structure
(def data
[{:type :root,
:content
[{:line-type :comment, :text "#+title: Sample Org File"}
{:line-type :comment, :text "#+author: Seylerius"}
{:line-type :blank, :text ""}
{:line-type :paragraph,
:text "This is an attempt to test Org-Mode processing."}
{:line-type :blank, :text ""}]}
@nathanmarz
nathanmarz / specter.zip.clj
Created February 16, 2016 16:36
Integrate zippers with Specter
(ns specter.zip
(:use [com.rpl.specter.macros :only [defpath]]
[com.rpl specter])
(:require [clojure [zip :as zip]]))
(def VECTOR-ZIP (view zip/vector-zip))
(def NEXT (view zip/next))
(def RIGHT (view zip/right))
(def LEFT (view zip/left))
(def DOWN (view zip/down))
@nathanmarz
nathanmarz / demo.clj
Created January 11, 2016 18:53
Recursive navigation with Specter
(use 'com.rpl.specter)
(use 'com.rpl.specter.macros)
(defprotocolpath TreeWalker [])
(extend-protocolpath TreeWalker
Object nil
clojure.lang.PersistentVector [ALL TreeWalker])
(select [TreeWalker number?] [:a 1 [2 [[[3]]] :e] [4 5 [6 7]]])
(def MOVIES
[{:name "Lethal Weapon", :director "Paul Verhoeven", :rating 7.6}
{:name "RoboCop", :director "George P. Cosmatos", :rating 7.5}
{:name "Bad Movie", :director "James Cameron", :rating 4.2}
{:name "Great Movie", :director "James Cameron", :rating 10.0}
{:name "Lethal Weapon 3", :director "Ted Kotcheff", :rating 6.6}
{:name "Rambo III", :director "John McTiernan", :rating 5.4}
{:name "The Terminator", :director "Peter MacDonald", :rating 8.1}
{:name "Titanic", :director "James Cameron", :rating 8.3}
@nathanmarz
nathanmarz / flow11.clj
Created September 28, 2015 05:09
specter data flow snippets
[{:a 2 :b 3} {:a 1} {:a 4}]
@nathanmarz
nathanmarz / bank1.clj
Last active September 28, 2015 16:20
functional navigational programming blog post
(defn pay-fee [world]
(transfer world
[:people ALL :money]
[:bank :funds]
1))
@nathanmarz
nathanmarz / late-parameterization-specter.clj
Last active September 11, 2015 22:58
Late bound parameterization with Specter
(def DATA {"a" [{"b" 1} {"b" 2}] "b" 11})
;; Selectors that require parameters can be precompiled *without* the parameters.
;; The parameters are supplied later.
(def path (comp-paths keypath ALL keypath))
;; These two selections are equivalent
(select (path "a" "b") DATA)
(select [(keypath "a") ALL (keypath "b")] DATA)
(-invoke [this p01 p02 p03 p04 p05 p06 p07 p08 p09 p10
p11 p12 p13 p14 p15 p16 p17 p18 p19 p20
rest]
(let [a (object-array
(concat
[p01 p02 p03 p04 p05 p06 p07 p08 p09 p10
p11 p12 p13 p14 p15 p16 p17 p18 p19 p20]
rest))]
(com.rpl.specter.impl/bind-params* this a 0))
)
@nathanmarz
nathanmarz / protocol-function-failures.clj
Created August 8, 2015 15:50
Protocol functions fail to find extensions is assigned to locals or other var
(defprotocol TestProtocol
(tester [o]))
(let [t tester]
(defn another-tester [o]
(t o)))
(def another-tester2 tester)
(extend-protocol TestProtocol
@nathanmarz
nathanmarz / protocol-metadata-test-case.clj
Created August 8, 2015 14:02
Protocol functions and metadata do not play well together
(defprotocol TestProtocol
(tester [o]))
(def tester-with-meta (with-meta tester {:a 1}))
(extend-protocol TestProtocol
String
(tester [o] (println "Strings work!")))
(tester-with-meta "A") ;; Error