Skip to content

Instantly share code, notes, and snippets.

@jkk
Created July 9, 2010 20:39
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jkk/470031 to your computer and use it in GitHub Desktop.
user=> (use 'clj-wiki.repl)
nil
user=> (examples contains?)
-------------------------
clojure.core/contains?
`contains?` is intended for use on key-based collections such as sets and maps:
(contains? {:foo 1 :bar 2} :foo)
=> true
(contains? #{:fred :ethel :lucy} :ethel)
=> true
When using `contains?` on vectors, be aware that it checks for the existence of the given _index_, not _value_:
; :b is not a valid index
(contains? [:a :b :c] :b)
=> false
; 1 is a valid index
(contains? [:a :b :c] 1)
=> true
If you want to check for the existence of a _value_ in a collection (in linear time), use [some](/clojure.core/some).
`contains?` does not work on lists in any meaningful way.
nil
;; Proof-of-concept "examples" function. Pulls from http://clojure-examples.appspot.com/
;;
;; Note: Doesn't pretty up the Markdown syntax much yet. It should remove link
;; markup and possibly indent code.
(ns clj-wiki.repl
(:use clojure.java.io
clojure.contrib.json)
(:import java.net.URLEncoder))
(defn examples [f]
(let [fns (:ns (meta f))
fname (str (:name (meta f)))
url (str "http://clojure-examples.appspot.com/"
fns "/" (URLEncoder/encode fname "UTF-8")
"?format=json")
raw-content (-> (:raw-content (read-json (slurp (reader url))))
(.replace "\r\n" "\n")
(.replaceAll "~{3,}((?:.|[\n])+?)~{3,}[\n]"
"$1")
(.replaceAll ">{3,}[\n]((?:.|[\n])+?)>{3,}[\n]?"
"=> $1")
(.replaceAll "\n{3,}", "\n\n")
(.trim))]
(println (str "-------------------------\n"
fns "/" fname "\n\n"
raw-content))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment