ideas-computer-jasper-jasperRosettaCandidates

grep in clojure from slide 16 of https://speakerd.s3.amazonaws.com/presentations/2471a370b3610130440476a0f7eede16/2013-05-17-ClojureOOP-Geecon.pdf

Clojure for OOP folks Stefan Tilkov

( ns sample.grep "A simple complete Clojure program." ( :use [clojure.contrib.io :only [read-lines]]) ( :gen-class )) ( defn numbered-lines [lines] ( map vector ( iterate inc 0) lines)) ( defn grep-in-file [pattern file] {file ( filter

  1. ( re-find pattern (second %)) (numbered-lines (read-lines file)))}) ( defn grep-in-files [pattern files] ( apply merge ( map
  2. (grep-in-file pattern %) files))) ( defn print-matches [matches] ( doseq [[fname submatches] matches, [line-no, match] submatches] (println ( str fname ":" line-no ":" match)))) ( defn -main [pattern & files] ( if ( or ( nil? pattern) ( empty? files)) (println "Usage: grep <pattern> <file...>" ) ( do (println ( format "grep started with pattern %s and file(s) %s" pattern ( apply str ( interpose ", " files)))) (print-matches (grep-in-files (re-pattern pattern) files)) (println "Done." ))))

( defn rand-seq [limit] ( repeatedly

  1. ( rand-int limit)))

( take 10 ( partition 2 (rand-seq 10)))

( defn circumference [vertices] ( reduce + ( map distance vertices ( drop 1 ( cycle vertices)))))

-- https://speakerd.s3.amazonaws.com/presentations/2471a370b3610130440476a0f7eede16/2013-05-17-ClojureOOP-Geecon.pdf slide 39

( defn all-members [projects] ( reduce conj #{} ( flatten ( map :team projects))))

-- slide 39-41 of: https://speakerd.s3.amazonaws.com/presentations/2471a370b3610130440476a0f7eede16/2013-05-17-ClojureOOP-Geecon.pdf

( defn make-fsm "creates an fsm with initial state s0, a reset event, and a map of transitions. [state-transitions] must be a map of state->[[f1 f2 ...] {e0->s0, e1->s2, ...}]" [s0 reset-event state-transitions ] ( let [s ( atom s0)] ( fn [evt] ( if (

evt reset-event) ( do ( println "Reset event, returning to " s0) ( swap! s ( fn [_] s0))) ( let [[actions transitions] (state-transitions @s)] ( if-let [new-state (transitions evt)] ( do ( println "Event" evt "causes transition from" @s "to" new-state) ( doseq [f actions] (f)) ( swap! s ( fn [_] new-state))) ( println "Unexpected/unhandled event" evt "in state" @s)))))))

---

perl -e 'while ($s = <STDIN>) {$s =~ /(\s*)/; $whitespaceCount += length($1)}; print $whitespaceCount ' < /tmp/t.txt