This file contains 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
(defmacro def+keep-meta [name a-var] | |
`(alter-meta! (def ~name ~a-var) merge (meta ~a-var))) |
This file contains 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 clj.core | |
(:use [criterium.core :only (bench with-progress-reporting)])) | |
(defmacro accumulate | |
"Captures symbol 'acc as accumulator." | |
[init-e next-e] | |
`(loop [c# (int 1e8) | |
~'acc ~init-e] | |
(if (zero? c#) | |
~'acc |
This file contains 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 clj.core | |
"Relying namespace." | |
(:require [clj.util :as u])) | |
(defn config-dir | |
"Call " | |
[conf] | |
(.getCanonicalFile (u/make-file))) |
This file contains 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
(import '(java.io ByteArrayInputStream ByteArrayOutputStream | |
ObjectInputStream ObjectOutputStream)) | |
(defmacro wtf | |
[] | |
(Boolean. false)) ;; note the lack of syntax-quote | |
--------- | |
user=> (wtf) |
This file contains 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 (= (% (*)) (+)) | |
(*) | |
(* ((% (+)) | |
[(% (+)) (- (% (*)) (*))]) | |
(% (*))))) | |
;; => 5040 | |
;; Almost there, just an "if" to get rid of... | |
(#((% (+(*))) %) ;; call arg 1 with all args |
This file contains 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
;; It all started here: http://clojure-log.n01se.net/date/2011-04-06.html#19:04 | |
(#((% (+(*))) %) ;; call arg 1 with all args | |
[;; data | |
(+ (*)(*)(*)(*)(*)(*)(*)) | |
;; main fn -- simulate 'if' with map lookup and closures | |
#(({(+) (% (+(*)(*)))} ;; if zero return 'then' clause | |
(% (+)) ;; dispatch on first arg | |
(% (+(*)(*)(*)))) ;; call 'else' clause (n not found in map) | |
%) |
This file contains 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 swap2! [a f & args] | |
(let [oldp (promise) | |
f* (fn [oldv] (deliver oldp oldv) (apply f oldv args))] | |
(swap! a f*) @oldp)) |
This file contains 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 make-human-move | |
"Block until human has moved, then return move." | |
[g] | |
(SwingUtilities/invokeLater #(update-ui-game-state g human-player-id)) | |
(reset! human-turn? true) | |
;; Block until human has moved | |
(.take human-moves)) |
This file contains 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
;; Changes: | |
;; - use + instead of reduce + | |
;; - only calculate (+ actual-n next-n) once per iteration | |
;; - No dangling closing parens | |
;; - dec instead of (- ... 1) (to taste) | |
;; - zero? instead of (= ... 0) | |
;; - reverse the if clauses to take care of the base case first | |
(defn fib-list | |
[actual-n next-n iterations-left full-list] |
This file contains 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 fib | |
([n] | |
(fib [0 1] (- n 2))) | |
([list-fib n] | |
(if (zero? n) | |
list-fib | |
(recur (conj list-fib (apply + (take-last 2 list-fib))) | |
(dec n))))) | |
(fib 10) |