Skip to content

Instantly share code, notes, and snippets.

@rlm
rlm / gist:746185
Created December 18, 2010 05:19
curry.clj
(ns sunil.curry)
(defn partial+
"Takes a function f and fewer than the normal arguments to f, and
returns a fn that takes a variable number of additional args. When
called, the returned function calls f with args + additional args.
differs from the core version in that it works on just one argument."
{:added "1.0"}
([f] f)
([f arg1]
@sunilnandihalli
sunilnandihalli / curry.clj
Created December 17, 2010 20:33
a macro to create fixed-arity curryable function in clojure
(defmacro def-curry-fn [name args & body]
{:pre [(not-any? #{'&} args)]}
(if (empty? args)
`(defn ~name ~args ~@body)
(let [rec-funcs (reduce (fn [l v]
`(letfn [(helper#
([] helper#)
([x#] (let [~v x#] ~l))
([x# & rest#] (let [~v x#]
(apply (helper# x#) rest#))))]
(defmacro check-let [b-vec & body]
(if-not (empty? b-vec)
(let [v (take 3 b-vec)
r (apply vector (drop 3 b-vec))]
`(if-let [~@(take 2 v)]
(check-let ~r ~@body)
~(nth v 2)))
`(do ~@body)))
;; turns this:
;; Simple example for generating Pascal's triangle
;; using chouser's finger-tree
(use 'clojure.data.finger-tree
'clojure.pprint)
(def pascal
(iterate #(into (double-list)
(map +
(conjr % 0)
(defn with-default-args [f & default-args]
(fn [& args]
(apply f (->> default-args
(drop (count args))
(concat args)))))
(comment ; usage
(def f (with-default-args + 1 2))
@RJ
RJ / irccloud_faq.txt
Created October 17, 2010 19:13
Rough cut of FAQ for IRCCloud.com
* Which browsers does it work in?
** We've tested in modern versions of Firefox, Chrome and Safari. It currently doesn't work in IE.
We plan to improve browser support in future.
* Does IRCCloud act as a BNC/bouncer and stay connected when I go offline?
** Yes it does - you will stay connected to IRC even if you shutdown your computer or log out of IRCCloud.com
This means when you come back, you'll be able to see what happened on IRC whilst you were away.
* What will it cost?
** Free currently, will roll out monthly subscription as the beta progresses.
(defprotocol Spinnable
(spin [this] "Return a seq walking the opposite direction as this"))
(defn iter-bi [x f b]
(reify
Spinnable
(spin [_] (iter-bi x b f))
clojure.lang.ISeq
(first [_] x)
(more [_] (iter-bi (f x) f b))
(defn itunes [command]
(let [mgr (javax.script.ScriptEngineManager.)
engine (.getEngineByName mgr "AppleScript")]
(.eval engine (str "tell application \"iTunes\" to " command))))
(itunes 'pause)
(itunes 'play)
(itunes 'stop)
;; ...
(defn some-name [f] ;; Takes a function that
(fn [& fns] ;; Returns a function that takes a list of functions that returns
(fn [& args] ;; A function that calls the original function with identity over applying each function to the args of this anonymous function #ohgod
(f identity (map apply fns (repeat args))))))
(defprotocol Pair (a [_]) (b [_]))
(defmethod print-method user.Pair [o w]
(.write w (str "#<Pair[" (.getName (class o)) "] " (a o) ", " (b o) ">")))
(deftype PairType [a b]
Pair
(a [_] a)
(b [_] b))