Skip to content

Instantly share code, notes, and snippets.

@nipra
Created July 10, 2010 10:48
Show Gist options
  • Save nipra/470629 to your computer and use it in GitHub Desktop.
Save nipra/470629 to your computer and use it in GitHub Desktop.
(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]
(let [matcher (re-matcher re string)]
(loop [match (re-find matcher)
result []]
(if-not match
result
(recur (re-find matcher)
(conj result match))))))
(defstruct person :first-name :last-name)
(def person1 (struct-map person :first-name "Nikhil" :last-name "Prabhakar"))
(def person2 (struct-map person :first-name "Nikhilesh" :last-name "Prabhakar"))
(def get-first-name (accessor person :first-name))
(defn count-words [file]
(sort (fn [x y]
(> (second x) (second y)))
(apply merge-with + (map #(array-map (.toLowerCase %) 1)
(filter (fn [x]
(re-matches #"^[a-zA-Z]+$" x))
(.split #"\W+" (slurp file)))))))
(defn count-words-helper [string]
(apply merge-with + (map #(array-map (.toLowerCase %) 1)
(remove empty? (.split #"[^a-zA-Z]+" string)))))
(defn count-words* [file]
(let [file-string (slurp file)]
(sort (fn [x y]
(> (second x) (second y)))
(count-words-helper file-string))))
(defn count-words+ [file]
(let [rdr (ccds/reader file)
result1 (loop [line (.readLine rdr)
result (when line (count-words-helper line))]
(if-not line
result
(let [next-line (.readLine rdr)
next-result (when next-line (count-words-helper next-line))]
(recur next-line (merge-with + result next-result)))))]
(sort (fn [x y]
(> (second x) (second y)))
result1)))
(defn square [x]
(do (println (str "Processing: " x))
(* x x)))
(defn lazy-counter [base increment]
(lazy-seq
(cons base (lazy-counter (+ base increment) increment))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment