This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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#))))] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
* 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) | |
;; ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)))))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)) |