Skip to content

Commit

Permalink
add several macros/functions
Browse files Browse the repository at this point in the history
  • Loading branch information
liquidz committed Sep 10, 2010
1 parent f9abb03 commit 40769ee
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/simply/core.clj
@@ -1,4 +1,6 @@
(ns simply.core)
(ns simply.core
(:use [simply.list :only [insert]])
)

; =!
(defmacro !
Expand Down Expand Up @@ -54,3 +56,23 @@
)
)
)

; =iff
(defn iff
([f f2] (fn [x] (if (f x) (f2 x) x)))
([x f f2] ((iff f f2) x))
)

(defn- base->>?* [val sex add-fn]
(reduce (fn [res x]
(if (and (list? x) (->> x first (= 'fn*) not))
(add-fn res x)
(list x res)
)
) val sex))
; =->*
(defmacro ->* [val & sex]
(base->>?* val sex (partial insert 1)))
; =->>*
(defmacro ->>* [val & sex]
(base->>?* val sex #(concat %2 (list %1))))
22 changes: 22 additions & 0 deletions src/simply/list.clj
@@ -1,4 +1,5 @@
(ns simply.list
(:use [simply core])
(:require [clojure.contrib.seq :as se])
)

Expand Down Expand Up @@ -33,6 +34,11 @@
(deep-map #(let [x (get smap %)] (if (nil? x) % x)) coll)
)

; =deep-replace-f
(defn deep-replace-f [f coll]
(deep-map #(let [res (f %)] (iff res nil? %)) coll)
)

; =find-index
(defn find-index
([conv pred coll]
Expand All @@ -43,3 +49,19 @@
(def find-first-index (partial find-index identity))
; =find-last-index
(def find-last-index (partial find-index reverse))

; map-map
(defn map-map [f m]
(apply hash-map
(reduce (fn [res [k v]]
(concat res [k (f k v)])
) () m)
)
)

; =insert
(defn insert [n val coll]
(let [[b a] (split-at n coll)]
(concat b (cons val a))
)
)

0 comments on commit 40769ee

Please sign in to comment.