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
| (defn drain | |
| "Takes all values available on a chan then returns the number taken to the channel drain creates. Compatible with cljs too." | |
| [c] | |
| (go | |
| (loop [r 0] | |
| (alt! | |
| (timeout 100) r | |
| c ([v] (recur (inc r))))))) | |
| (defn drainf |
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
| ;; based on core.logic 0.8-alpha2 or core.logic master branch | |
| (ns sudoku | |
| (:refer-clojure :exclude [==]) | |
| (:use clojure.core.logic)) | |
| (defn get-square [rows x y] | |
| (for [x (range x (+ x 3)) | |
| y (range y (+ y 3))] | |
| (get-in rows [x y]))) |
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
| (use 'clojure.test) | |
| (defn fold-right [f z coll] | |
| (loop [[c & cs] coll rvsd '()] | |
| (if (nil? c) | |
| (loop [acc z [r & rs] rvsd] | |
| (if r (recur (f r acc) rs) acc)) | |
| (recur cs (cons c rvsd))))) | |
| (defn reduce-right [f coll] |
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
| #!/usr/bin/java -jar clojure-1.7.0-master-SNAPSHOT.jar | |
| (let [pom-uber-jar | |
| (str "http://thelibraryofcongress.s3.amazonaws.com/" | |
| "pomegranate-0.0.13-SNAPSHOT-jar-with-dependencies.jar") | |
| cl (java.net.URLClassLoader. (into-array [(java.net.URL. pom-uber-jar)])) | |
| cx (.getContextClassLoader (Thread/currentThread))] | |
| (push-thread-bindings {clojure.lang.Compiler/LOADER cl}) |
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
| (defn slide | |
| "Returns the final vector after dragging an indexed item to another index, which means it swaps incrementally with each index it passes through. v should be a vector but if it isn't it will be converted to one." | |
| [v o n] | |
| (let [v (vec v) ;; ensures v is a vector | |
| item (v o) | |
| removed (if (= o (count v)) | |
| (pop v) | |
| `[~@(concat | |
| (subvec v 0 o) | |
| (subvec v (inc o)))])] |
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
| ;; try this form-by-form at a REPL | |
| (require '[clojure.spec.alpha :as s]) | |
| ;; create an inline DSL to describe the FizzBuzz world | |
| (defmacro divides-by | |
| [nm n] | |
| `(s/def ~nm (s/and pos-int? #(zero? (mod % ~n))))) | |
| ;; specify FizzBuzz | |
| (divides-by ::fizz 3) |
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
| ;; I think it would be a mistake to introduce temporal coupling to prevent typos. | |
| ;; The example program below lets you identify "missing" keys specs at | |
| ;; the time and place of your choosing, and then handle them as you | |
| ;; deem appropriate, without imposing those decisions on other | |
| ;; users of spec. | |
| (require '[clojure.spec.alpha :as s] | |
| '[clojure.set :as set]) |
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
| ;; I think it would be a mistake to introduce temporal coupling to prevent typos. | |
| ;; The example program below lets you identify "missing" keys specs at | |
| ;; the time and place of your choosing, and then handle them as you | |
| ;; deem appropriate, without imposing those decisions on other | |
| ;; users of spec. | |
| (require '[clojure.spec.alpha :as s] | |
| '[clojure.set :as set]) |
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
| (def init {:word "XIMEDES" | |
| :attempts-left 10 | |
| :state :playing | |
| :guessed #{}}) | |
| (def model (atom init)) | |
| (defn show-won [] | |
| [:div "Jeej you won!, share it with your friends!"]) |
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
| #if !DEBUG | |
| public func print(_ items: Any..., separator: String = " ", terminator: String = "\n") { | |
| //does nothing if not in debug build | |
| } | |
| #else | |
| public func print(_ items: Any..., separator: String = " ", terminator: String = "\n") { | |
| debugPrint(items) | |
| } | |
| #endif |