Skip to content

Instantly share code, notes, and snippets.

@MayDaniel
MayDaniel / gist:950619
Created May 1, 2011 16:24 — forked from slagyr/gist:950574
Game of Life in 8 lines of Clojure
(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)))))
@MayDaniel
MayDaniel / lisp.hs
Created May 1, 2011 10:50
Minimal Lisp in Haskell
{-# 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)
@MayDaniel
MayDaniel / match.clj
Created April 28, 2011 18:52 — forked from pepijndevos/project.clj
seqex - a tiny pattern matcher in Clojure
(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))
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)
(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)))
(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)
(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
(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)
(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))))))
(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)))