Created
August 5, 2010 18:21
-
-
Save bitemyapp/510140 to your computer and use it in GitHub Desktop.
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 pc.core | |
(str 1 2 nil 3) | |
(+ 1 3) | |
(Character/toUpperCase \s) | |
(def y (apply str (interleave "Attack at midnight" "The purple elephant chortled"))) | |
(apply str (take-nth 2 y)) | |
; (if part) (else part), () evaluates true | |
(if () "We are in clojure!" "We are in common Lisp!") | |
; zero is true too. that's uh, pretty absurd. | |
(if 0 "Zero is true" "Zero is false") | |
(true? 0) ;false | |
(zero? 0) ;true | |
(nil? nil) ;true | |
(nil? "nil") ;false | |
(def inventors {"Lisp" "McCarthy" "Clojure" "Hickey"}) | |
(inventors "Lisp") | |
(inventors "Foo") | |
(get inventors "Foo" "I dunno!") | |
:foo | |
(def inventors {:Lisp "McCarthy" :Clojure "Hickey"}) | |
(inventors :Clojure) | |
(defstruct book :title :author) | |
(def b (struct book "Anathem" "Neal Stephenson")) | |
b ;{:title, :author, etc} | |
(:title b) ; "Anathem" | |
(struct-map book :copyright 2008 :title "Anathem") ; author nil | |
'(1 2) | |
(str "Hello" " " "world") | |
(string? "Hello") | |
(keyword? :hello) | |
(symbol? 'hello) | |
(defn greeting | |
"Returns a greeting of the form 'hello, username'" | |
([] (greeting "world")) | |
([username] | |
(str "Hello, " username))) | |
(greeting "world") | |
(doc greeting) | |
(greeting) | |
(defn date [p1 p2 & chaperones] | |
(println p1 "and" p2 | |
"went out with" (count chaperones) "chaperones")) | |
(date "Romeo" "Juliet" "Friar Lawrence" "Nurse") | |
(def chap ["Friar Lawrence" "Nurse"]) | |
(date "Romeo" "Juliet" chap) ; 1 | |
(defn indexable-word? [word] | |
(> (count word) 2)) | |
(use '[clojure.contrib.str-utils :only (re-split)]) | |
(filter indexable-word? (re-split #"\W+" "A fine day it is")) | |
(filter (fn [w] (> (count w) 2)) (re-split #"\W+" "A fine day")) | |
(filter #(> (count %) 2) (re-split #"\W+" "A fine day it is")) | |
(defn indexable-words [text] | |
(let [indexable-word? (fn [w] (> (count w) 2))] | |
(filter indexable-word? (re-split #"\W+" text)))) | |
(indexable-words "a fine day it is") | |
(defn make-greeter [greeting-prefix] | |
(fn [username] (str greeting-prefix ", " username))) | |
(def hello-greeting (make-greeter "Hello")) | |
(hello-greeting "world") | |
((make-greeter "Howdy") "pardner") | |
(def foo 10) | |
foo | |
(var foo) | |
#'foo | |
(defn triple [number] (* 3 number)) | |
(triple 10) | |
(def b 10000000000000000000000000000000000000000000000000000) | |
(triple (* b b)) ; well now I'm just damned impressed. | |
; let binds top and right, prevents calculating more than once. | |
(defn square-corners [bottom left size] | |
(let [top (+ bottom size) | |
right (+ left size)] | |
[[bottom left] [top left] [top right] [bottom right]])) | |
(square-corners 10 10 50) | |
(defn greet-author-1 [author] | |
(println "Hello," (:first-name author))) | |
(def vv {:last-name "Vinge" :first-name "Vernor"}) | |
(greet-author-1 vv) | |
(defn greet-author-2 [{fname :first-name}] | |
(println "Hello," fname)) | |
(greet-author-2 vv) | |
(let [[x y] [1 2 3]] | |
[x y]) | |
(let [[_ _ z] [1 2 3]] | |
z) ;use _ to indicate that you don't care | |
(let [[x y :as coords] (range 1 7)] | |
(str "x: " x ", y:" y ", total " (count coords))) | |
(use '[clojure.contrib.str-utils :only (re-split str-join)]) | |
(defn ellipsize [words] | |
(let [[w1 w2 w3] (re-split #"\s+" words)] | |
(str-join " " [w1 w2 w3 "..."]))) | |
; attempt at cleaning it up | |
(def sentence "The quick brown fox jumps over the lazy dog") | |
(def sentlist (re-split #"\s+" sentence)) | |
(let [lst sentlist result []] | |
(def initial (str (str-join " " (take 3 sentlist)) "..."))) | |
; MUCH BETTER | |
(defn ellip-2 [sentence] | |
(let [word-seq (re-split #"\s+" words)] | |
(str (str-join " " (take 3 word-seq)) "..."))) | |
(defn ellip-2 [words] | |
(str (->> words (re-split #"\s+") (take 3) (str-join " ")) "...")) | |
(ellip-2 "YO THIS IS A TEST SENTENCE") ; "YO THIS IS..." | |
; delightful. | |
(str "a " "b ") | |
(subvec (vec sentlist) 0 3) ; vectorize first | |
(take 3 sentlist) | |
;(vec sentlist) | |
(nth sentlist 0) | |
(first (re-split #"\s+" sentence)) | |
(ellipsize sentence) ; "The quick brown ..." <--- fix? | |
(resolve 'foo) | |
java.io.File/separator ;"/" | |
(require 'clojure.contrib.math) | |
(use '[clojure.contrib.math :only (round)]) | |
(clojure.contrib.math/round 1.7) ; 2 | |
(round 1.7) ; ^^ | |
; idiomatic namespaces and library use | |
(ns examples.exploring | |
(:use clojure.contrib.str-utils) | |
(:import (java.io File))) | |
(find-doc "ns-") | |
(defn is-small? [number] | |
(if (< number 100) true (do | |
(println "Saw a big number" number) | |
false))) | |
(is-small? 50) | |
(is-small? 5000) | |
; result empty vector, x to 5, x isn't zero, | |
; conjoins x to result, decrements x until zero | |
; then returns the result vector | |
(loop [result [] x 5] | |
(if (zero? x) | |
result | |
(recur (conj result x) (dec x)))) | |
(defn countdown [result x] | |
(if (zero? x) | |
result | |
(recur (conj result x) (dec x)))) | |
(countdown [] 5) | |
(into [] (take 5 (iterate dec 5))) | |
(into [] (drop-last (reverse (range 6)))) | |
(vec (reverse (rest (range 6)))) | |
(defn indexed [coll] (map vector (iterate inc 0) coll)) | |
(indexed "abcde") | |
(defn index-filter [pred coll] | |
(when pred | |
(for [[idx elt] (indexed coll) :when (pred elt)] idx))) | |
(index-filter #{\a \b} "abcdbbb") | |
(defn index-of-any [pred coll] | |
(first (index-filter pred coll))) | |
(index-of-any #{\z \a} "zzabyycdxx") | |
(nth (index-filter #{:h} [:t :t :h :t :h :t :t :t :h :h]) 2) ; third instance of :h | |
(def stu {:name "Stu" :email "[email protected]"}) | |
(def serializable-stu (with-meta stu {:serialize true})) | |
(= stu serializable-stu) ;true | |
(identical? stu serializable-stu) ;false, checks for reference identity | |
(meta stu) | |
(meta serializable-stu) ; :serialize true | |
(comment ^serializable-stu) ; DEPRECATED | |
(def stu-with-address (assoc serializable-stu :state "NC")) | |
stu-with-address ; has email in the proper data | |
(type stu-with-address) ; persistent array map | |
(meta stu-with-address) | |
; #^stu-with-address ; not working :( | |
(use '[clojure.contrib.duck-streams :only (spit)]) | |
(spit "hello.out" "Hello, world") | |
(first '(1 2 3)) | |
(rest '(1 2 3)) | |
(cons 0 '(1 2 3)) | |
(take 20 (repeat 1)) | |
(take 10 (iterate inc 1)) | |
(import '(java.io File)) | |
(def fs (file-seq (File. "."))) | |
(nth fs 3) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment