Skip to content

Instantly share code, notes, and snippets.

@nipra
nipra / gist:1174053
Created August 26, 2011 18:24
Python **kwargs like functionality when doing function call
;; Just for fun!
;; If Clojure were like Python, I could have used (asdf **{:a 1 :b 2 :c 3}) when calling asdf
user> (defn asdf
[& {:keys [a b c]}]
[a b c])
#'user/asdf
user> (defmacro call**
[func kw-map]
`(~func ~@(mapcat identity kw-map)))
user> (iter {for x from 1 to 10}
{reduce x by conj initially [] into odds if (odd? x)}
{reduce x by conj initially [] into evens if (even? x)}
{sum x into sum-odds if (odd? x)}
{sum x into sum-evens if (even? x)}
{returning {:evens evens
:odds odds
:sum-odds sum-odds
:sum-evens sum-evens}})
{:evens [2 4 6 8 10], :odds [1 3 5 7 9], :sum-odds 25, :sum-evens 30}
@nipra
nipra / fnil_examples.clj
Created July 7, 2011 09:23
fnil use case
(defn plus
([x] ((fnil + 0) x))
([x y]
((fnil + 0 0) x y))
([x y z]
((fnil + 0 0 0) x y z))
([x y z & more]
(reduce plus (plus x y z) more)))
;; user> (plus nil)
user> (def *files* (file-seq (java.io.File. "/tmp")))
#'user/*files*
user> (def *f* (first *files*))
#'user/*f*
user> *f*
#<File /tmp>
user> (.isFile *f*)
false
user> (.isDirectory *f*)
true
(ns practical-clojure.chapter4
(:require [clojure.contrib [duck-streams :as ccds]]))
(def my-matcher (re-matcher #"[a-zA-Z]*" "test"))
(def my-matcher2 (re-matcher #"[a-z]" "test"))
;;; practical-clojure.chapter4> (find-all-matches #"\d" (apply str (range 10)))
;;; ["0" "1" "2" "3" "4" "5" "6" "7" "8" "9"]
(defn find-all-matches [re string]
;; First fetch CVS version of slime, git version of clojure, swank-clojure, clojure-contrib and clojure-mode
;; Create ~/bin/clojure script which starts clojure repl and adds clojure-contrib src dir and swank-clojure src dir to classpath. I used clj-env helper from clojure-contrib
(pushnew '("\.clj$" . clojure-mode) auto-mode-alist)
(require 'clojure-mode)
;;;; Slime configuration stuff
(setf slime-lisp-implementations
'((ecl("~/bin/ecl" "--heap-size" "1024000000") :coding-system utf-8-unix)
;; This buffer is for notes you don't want to save, and for Lisp evaluation.
;; If you want to create a file, visit that file with C-x C-f,
;; then enter the text in that file's own buffer.
(defn map-keys
"Applies f to all keys of m recursively."
[f m]
(cond
(map? m) (into {} (map (fn [[key value]]
[(f key) (map-keys f value)])
m))
(coll? m) (map (partial map-keys f) m)
:default m))
user> (re-seq (re-pattern #"\bfoo\b") "bar foo bar")
("foo")
user> (def *s "foo")
#'user/*s
user> (re-seq (re-pattern (format "\b%s\b" *s)) "bar foo bar")
nil
user> (re-seq (re-pattern (format "\\b%s\\b" *s)) "bar foo bar")
("foo")
user>
;;; http://clojure.googlegroups.com/web/tutorial.pdf
;;; Vector binding forms destructure sequential things (vectors, lists, seqs, strings, arrays,
;;; and anything that supports nth)
;;; Map binding forms destructure associative things (maps, vectors, strings and arrays;
;;; the latter three have integer keys)
user> (let [[a b c] "xyz"]
[a b c])