Skip to content
/ arrows Public

Extensions to the Clojure threading macros to help with manipulating nested data structures

License

Notifications You must be signed in to change notification settings

we-shop/arrows

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Arrows

An extension of the Clojure (and ClojureScript) threading macros to help with manipulating nested data structures.

[weshop/arrows "0.2.1"]

;; clojure, clojurescript >= 1.9.198:
(:require [weshop.arrows :refer [->% ->>%]])

;; clojurescript < 1.9.198
(:require-macros [weshop.arrows :refer [->% ->>%]])

Problem - can’t nest #()-style functions in Clojure:

This is a pain when you’re manipulating nested structures:

(let [table [{:a 1 :b [4 12]},
             {:a 3 :b [8 35]}]]
  (map #(-> %
            ;; not allowed - #() within #()
            (update :b #(map inc %))
            (assoc :c 8))
       table))

This library provides two macros: ->% and ->>%, each of which create an anonymous function that threads its argument through the provided forms in a similar way to -> and ->>:

(let [table [{:a 1 :b [4 12]},
             {:a 3 :b [8 35]}]]
  (map (->% (update :b #(map inc %))
            (assoc :c 8))
       table))

;; transforms into:

(let [table [{:a 1 :b [4 12]},
             {:a 3 :b [8 35]}]]
  (map (fn [m]
         (-> m
             (update :b #(map inc %))
             (assoc :c 8)))
       table))

;; and returns

;; [{:a 1, :b [5 13], :c 8},
;;  {:a 3, :b [9 36], :c 8}]

->>% behaves in a similar manner, except threads the argument as the last parameter to the forms, like ->>:

(let [results [[3 5 8 12], [6 45 23 18]]]
  (map (->>% (filter #(zero? (mod % 3)))
             (map #(* 2 %)))
       results))

;; -> [[6 24], [12 90 36]]

Bugs/features/suggestions/questions?

Please feel free to submit bug reports/patches etc through the GitHub repository in the usual way!

Thanks!

License

Copyright © 2018 WeShop

Distributed under the Eclipse Public License, the same as Clojure.

About

Extensions to the Clojure threading macros to help with manipulating nested data structures

Resources

License

Stars

Watchers

Forks

Packages