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)) | |
| (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 some-name [f] ;; Takes a function that | |
| (fn [& fns] ;; Returns a function that takes a list of functions that returns | |
| (fn [& args] ;; A function that calls the original function with identity over applying each function to the args of this anonymous function #ohgod | |
| (f identity (map apply fns (repeat args)))))) |
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
| ;;; All Kinds of Destructuring ;;; | |
| (let [foo 1] foo) | |
| ; => 1 | |
| (let [[foo bar] [1 2 3]] [foo bar]) | |
| ; => [1 2] | |
| (let [[foo bar & baz] [1 2 3]] [foo bar baz]) | |
| ; => [1 2 (3)] |