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 neighbors-of [cell] | |
(set (for [dx [-1 0 1] dy [-1 0 1] :when (not (= [dx dy] [0 0]))] | |
[(+ dx (first cell)) (+ dy (second cell))]))) | |
(defn alive? [[cell freqs] world] | |
(or (and (= 2 freqs) (contains? world cell)) (= 3 freqs))) | |
(defn tick [world] | |
(let [frequencies (frequencies (reduce #(concat %1 (neighbors-of %2)) [] world))] | |
(set (keys (filter #(alive? % world) frequencies))))) |
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
{-# LANGUAGE OverloadedStrings #-} | |
{- To Run: | |
Load in ghci | |
:set -XOverloadedStrings (for convenience) | |
Execute repl expr -} | |
import Control.Applicative | |
import Data.Attoparsec hiding (Result) | |
import Data.Attoparsec.Char8 (char8, isDigit_w8, isSpace_w8) |
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 seqex.match) | |
(defn cps [funct cont] | |
(fn [[f & r :as s]] | |
(if (funct f) | |
(cont r) | |
false))) | |
(defn literal [l cont] | |
(cps (partial = l) cont)) |
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
user=> (import 'System.Linq.Enumerable) | |
System.Linq.Enumerable | |
user=> (def r1 (Enumerable/Range 1 10)) | |
#'user/r1 | |
user=> (seq r1) | |
(1 2 3 4 5 6 7 8 9 10) | |
user=> (def r2 (. Enumerable (generic Repeat Int32) 3 4)) | |
#'user/r2 | |
user=> (seq r2) | |
(3 3 3 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
(defmacro check-let [bindings & body] | |
(assert (vector? bindings)) | |
(assert (even? (count bindings))) | |
(if (not-empty bindings) | |
(let [[binding [else & more]] (split-at 2 bindings)] | |
`(if-let [~@binding] (check-let [~@more] ~@body) ~else)) | |
`(do ~@body))) |
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
(defmacro when-lets [[symbol value & more :as bindings] & body] | |
(if (not-empty bindings) | |
`(let [tmp# ~value] (when tmp# (let [~symbol tmp#] (when-lets [~@more] ~@body)))) | |
`(do ~@body))) | |
(defmacro when-lets [bindings & body] | |
(assert (vector? bindings)) | |
(assert (even? (count bindings))) | |
(if (not-empty bindings) |
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
(defmacro check-let [bindings & body] | |
(assert (vector? bindings)) | |
(if (not-empty bindings) | |
(let [[binding [else & more]] (split-at 2 bindings)] | |
`(if-let [~@binding] (check-let [~@more] ~@body) ~else)) | |
`(do ~@body))) | |
(check-let (a 1 ::else) ... ) ;; assert vector exception | |
(check-let [a] ... ) ;; if-let binding count exception | |
(check-let [a 1 ::else] 5) ;; 5 |
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
(defmacro check-let [bindings & body] | |
(assert (vector? bindings)) | |
(if (not-empty bindings) | |
(let [[binding [else & more]] ((juxt take drop) 2 bindings)] | |
`(if-let [~@binding] (check-let [~@more] ~@body) ~else)) | |
`(do ~@body))) | |
(defmacro check-let [bindings & body] | |
(assert (vector? bindings)) | |
(if (not-empty bindings) |
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 partition-when [pred coll] | |
(lazy-seq | |
(when-let [s (seq coll)] | |
(let [[head tail] (split-with pred s)] | |
(cons head (partition-when (complement pred) tail)))))) |
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
(future-call (bound-fn [] (println 1))) | |
;;; | |
(alter-var-root (var *out*) (constantly *out*)) | |
(do (future (println 1)) | |
(future (println 2)) | |
(doto (Thread. (fn [] (println 1))) | |
(.start))) |
NewerOlder