Clojure and me has moved.
Showing posts with label utilities. Show all posts
Showing posts with label utilities. Show all posts

Monday, June 8, 2009

Linear Interpolation and sorted-map

This post has moved, go to its new location
Sorted collections (with subseq and rsubseq) can help when working with a partition of disjoint intervals, eg when you need to interpolate.
(defn interpolator 
"Takes a coll of 2D points (vectors) and returns
their linear interpolation function."
[points]
(let [m (into (sorted-map) points)]
(fn [x]
(let [[[x1 y1]] (rsubseq m <= x)
[[x2 y2]] (subseq m > x)]
(if x2
(+ y1 (* (- x x1) (/ (- y2 y1) (- x2 x1))))
y1)))))

;; => (map (interpolator [[0 0] [1 1] [3 2] [4 3]]) (range 0 9/2 1/2))
;; (0 1/2 1 5/4 3/2 7/4 2 5/2 3)

Friday, May 1, 2009

Functionally growing a tree (2): insertion points and zippers

This post has moved, go to its new location
I just uploaded a library that builds on zippers but shifts emphasis from nodes to insertion-points (interstitial positions before, between and after nodes).
It eases growing trees from an empty root.
  ; show-ip represents the currently edited structure with * denoting the insertion-point
(defn show-ip [ip] (-> ip (insert-left '*) first z/root))
(def e (-> [] z/vector-zip (insertion-point :append)))
(-> e show-ip) ; [*]
(-> e (insert-left 1) show-ip) ; [1 *]
(-> e (insert-left 1) (insert-right 2) show-ip) ; [1 * 2]
(-> e (insert-left 1) (insert-right 2) left show-ip) ; [* 1 2]
(-> e (insert-left [1 2]) show-ip) ; [[1 2] *]
(-> e (insert-left [1 2]) left show-ip) ; [* [1 2]]
(-> e (insert-left [1 2]) left right show-ip) ; [[1 2] *]
(-> e (insert-left [1 2]) left next show-ip) ; [[* 1 2]]
(-> e (insert-left [1 2]) left next next show-ip) ; [[1 * 2]]
(-> e (insert-left [1 2]) left next next next show-ip) ; [[1 2 *]]
(-> e (insert-left [1 2]) left next next next next show-ip) ; [[1 2] *]
(-> e (insert-left [1 2]) left next next next next prev show-ip) ; [[1 2 *]]

Wednesday, April 29, 2009

.toUpperCase

This post has moved, go to its new location
I recently learnt that when you want to convert the case of a technical identifier (a tagname, a HTTP header etc.) you must not use plain .toUpperCase or .toLowerCase but specify Locale/ENGLISH.

Friday, April 17, 2009

Mapping every second item

This post has moved, go to its new location
I wanted to apply a function to every second item in a coll. I was considering writing something using interleave, take-nth and map or a combination of mapcat and partition when I thought of this:
(map #(%1 %2) (cycle [f identity]) coll)
I really love clojure's map parallel processing. (I should ask if every? and some could be allowed to take several colls.)

Wednesday, March 25, 2009

mapsplice

This post has moved, go to its new location
I needed to update a seq as follows: apply a given function to items at specified indices and insert the result (a seq) in place.
My first laborious attempt involved extracting subseqs of untouched items, mapping the function on the remaining items then joining everything back. Bleh! (Really, I don't like indices but this time I haven't found a way to work around.)
I was sad when, suddenly, I found this shortest solution:
(defn mapsplice [f coll indices]
(let [need-splice (map (set indices) (iterate inc 0))]
(mapcat #(if %2 (f %1) [%1]) coll need-splice)))
;; (mapsplice #(repeat % %) (range 10) [7 3])
;; (0 1 2 3 3 3 4 5 6 7 7 7 7 7 7 7 8 9)

Tuesday, January 27, 2009

Living on the bleeding edge

This post has moved, go to its new location
You can try clojure libs straight from the cloud:
(add-classpath "http://clojure-contrib.svn.sourceforge.net/viewvc/clojure-contrib/trunk/src/") ; sourceforge
(add-classpath "http://github.com/Lau-of-DK/clojureql/raw/master/src/"); github
(require '[clojure.contrib.duck-streams :as duck])
(require '[dk.bestinclass.clojureql :as ql])

Disclaimer:

  • add-classpath is not meant to be used anywhere but at the repl,
  • you'd better trust what you download,
  • if you are unlucky you can get an inconsistent snapshot,
  • use at your own risk!

Tuesday, January 20, 2009

Bindings and send

This post has moved, go to its new location
This morning on #clojure erohtar asked:
what is the best way to bind vars to a value when sending to an agent?

I don't know if it's the best way but it's mine:
(defmacro capture-and-send
"Capture the current value of the specified vars and rebind
them on the agent thread before executing the action."
[vars agent action & args]
(let [locals (map #(gensym (name %)) vars)]
`(let [~@(interleave locals vars)
action# (fn [& args#]
(binding [~@(interleave vars locals)]
(apply ~action args#)))]
(send ~agent action# ~@args))))
;; usage:
(capture-and-send [*out*] a f b c)

I post it here because erohtar needed it, I needed it once so others may need it.

Thursday, January 8, 2009

try-or, or-try, try-else or else-try?

This post has moved, go to its new location
I can't decide which name is best for this macro:
(defmacro try-or
"Evaluates exprs one at a time, from left to right. If a form returns a
value, this value is returned. If a form throws an exception, the next
form is evaluated.
If the last form throws an exception, the exception isn't caught."
([] nil)
([form] form)
([form & forms]
`(try
~form
(catch Exception e#
(try-or ~@forms)))))

Monday, January 5, 2009

Recursive seqs

This post has moved, go to its new location
Recursively defined sequences are pretty but difficult to get right when you don't want to assign the sequence to a var. Here is a couple of macros to ease recursive definitions.
(defmacro rec-cat 
"Similar to lazy-cat but binds the resulting sequence using the supplied
binding-form, allowing recursive expressions. The first collection
expression must not be recursive and must return a non-nil seq."
[binding-form expr & rec-exprs]
`(let [rec-rest# (atom nil)
result# (lazy-cat ~expr (force @rec-rest#))
~binding-form result#]
(swap! rec-rest# (constantly (delay (lazy-cat ~@rec-exprs))))
result#))

(defmacro rec-cons
"Similar to lazy-cons but binds the resulting sequence using the supplied
binding-form, allowing recursive expressions. The first expression must
not be recursive and must return a non-nil seq."
[binding-form expr & rec-exprs]
`(let [rec-rest# (atom nil)
result# (lazy-cons ~expr (force @rec-rest#))
~binding-form result#]
(swap! rec-rest# (constantly (delay (lazy-cat ~@rec-exprs))))
result#))

Examples:

Natural numbers (just like (iterate inc 0)):
(rec-cons naturals 0 (map inc naturals))
fibonnaci sequence:
(rec-cat fibs [0 1] (map + fibs (rest fibs)))
Chouser's cute reduction:
(defn reduction
"Returns a lazy seq of the intermediate values of the reduction (as
per reduce) of coll by f, starting with init."
([f coll]
(if (seq coll)
(rec-cons reductions (first coll) (map f reductions (rest coll)))
(cons (f) nil)))
([f init coll]
(rec-cons reductions init (map f reductions coll))))

Tuesday, July 1, 2008

lazy butlast

This post has moved, go to its new location
Named but-last to avoid name clash with butlast which is a low-level function (it is used in the definition of defn).
(defn but-last
"Return a lazy sequence of all but the n last items in coll."
([coll] (but-last coll 1))
([coll n]
((fn this [s os]
(if os
(lazy-cons (first s) (this (rest s) (rest os))))) (seq coll) (drop n coll))))

Update: Rich Hickey improved this function and added it to boot.clj:
(defn drop-last
"Return a lazy seq of all but the last n (default 1) items in coll"
([s] (drop-last 1 s))
([n s] (map (fn [x _] x) (seq s) (drop n s))))
and shared with us, mere mortals, a piece of wisdom:
I constantly have to remind myself to try to use a higher-order fn, because lazy-cons is so easy
From now on, I'll try to question each lazy-cons.

Monday, June 23, 2008

Lazier than lazy

This post has moved, go to its new location
If you can't put off what you have to do, ask someone else to do it for you.
(defn off
"Computes a lazy-seq in another (dedicated) thread."
[s]
(let [ex (. java.util.concurrent.Executors newSingleThreadExecutor)]
((fn this [s]
(if s
(let [future-rest (java.util.concurrent.FutureTask. #(rest s))]
(.execute ex future-rest)
(lazy-cons (first s) (this (.get future-rest))))
(.shutdown ex))) s)))

Wednesday, May 28, 2008

Jumping to Javadocs from the REPL

This post has moved, go to its new location

(defn open-url [url]
(let [htmlpane (new javax.swing.JEditorPane url)]
(.setEditable htmlpane false)
(.addHyperlinkListener htmlpane
(proxy [javax.swing.event.HyperlinkListener] []
(hyperlinkUpdate [#^javax.swing.event.HyperlinkEvent e]
(when (= (.getEventType e) (. javax.swing.event.HyperlinkEvent$EventType ACTIVATED))
(if (instance? javax.swing.text.html.HTMLFrameHyperlinkEvent e)
(.. htmlpane getDocument (processHTMLFrameHyperlinkEvent e))
(try
(.setPage htmlpane (.getURL e))
(catch Throwable t
(.printStackTrace t))))))))
(doto (new javax.swing.JFrame)
(setContentPane (new javax.swing.JScrollPane htmlpane))
(setBounds 32 32 700 900)
(show))))

(defn javadoc [c]
(let [url (str "http://java.sun.com/javase/6/docs/api/"
(.. c getName (replace \. \/) (replace \$ \.)) ".html")]
(open-url url)))

; usage:
; (javadoc Throwable) opens a window displaying Throwable's javadoc
; hint: (javadoc (class some-object))