Skip to content

Instantly share code, notes, and snippets.

@rik0
rik0 / module_clojure.xml
Created January 23, 2011 17:22
Clojure Buildfile
<?xml version="1.0" encoding="UTF-8"?>
<project name="module_clojure" default="compile.module.clojure">
<dirname property="module.hdsnetsym.basedir" file="${ant.file.module_clojure}"/>
<target name="compile.module.clojure" description="Compile clojure scripts of ...">
<java classname="clojure.lang.Compile">
<classpath>
<path location="${current_module.output.dir}"/>
@rik0
rik0 / lazy_set.py
Created February 6, 2011 10:02
Trace of lazy set implementation in Python
# Copyright (C) 2011 by Enrico Franchi
#
# This file is released under the terms of the MIT license
# http://www.opensource.org/licenses/mit-license.php
import itertools as it
def silence_generator_already_executing(generator):
try:
for element in generator:
(defn -initialize [this params]
(dosync
(alter (.state this) merge
(unpack-parameters
params
[:is-starting-node :connections-on-spawn :starting-nodes]
(array-map :is-starting-node false))))
true)
(defn -initialize [this params]
(dosync
(alter (.state this) assoc :starting-node false)
(alter (.state this) assoc :m-value (int *m*))
(alter (.state this) assoc :logger
(Logger/getLogger (->> this .getAddress .getName))))
(condp = (alength params)
0 true
1 (dosync
(alter (.state this) assoc :starting-node (boolean (aget params 0)))
(defmacro unpack-parameters [params keys defaults & body]
`(let [{:keys ~keys :or ~defaults}
(apply hash-map (interleave (map keyword '~keys) ~params))]
~@body))
@rik0
rik0 / let-array.clj
Created February 13, 2011 14:59
let-array clojure macro used to destructure a Java array.
(defn -initialize [this params]
(let-array [
{:keys [is-starting-node connections-on-spawn starting-nodes]
:or {is-starting-node false}
:as options} params]
(dosync (alter (.state this) merge options))
true))
@rik0
rik0 / gist:831622
Created February 17, 2011 12:30
mdef.clj
(defmacro mdef [keys values]
`(do
~@(loop [keys keys, values values, forms ()]
(if (seq keys)
(recur (rest keys) (rest values)
(cons `(def ~(first keys) ~(first values))
forms))
forms))))
(mdef [a b c] [1 2 3])
@rik0
rik0 / output.txt
Created February 22, 2011 09:00
cannibals.pl
initial_state(mc, mc(left, bank(3,3), bank(0,0))).
final_state(mc(right, bank(0,0), bank(3,3))).
move(mc(left, L, _R), Boat) :-
choose_passengers(L, Boat).
move(mc(right, _L, R), Boat) :-
choose_passengers(R, Boat).
choose_passengers(bank(C, _M), boat(2, 0)) :-
C > 1.
@rik0
rik0 / bench.clj
Created February 23, 2011 08:35
bench.clj
(ns lazier.bench
(:use clojure.core
[clojure.contrib.str-utils :only (re-sub str-join)]
clojure.contrib.pprint))
(defn slow-computation [& more]
(Thread/sleep 500)
(if (seq more) more 1))
(defmacro benchmark
@rik0
rik0 / first-grammar.clj
Created February 23, 2011 18:57
PAIP grammar interpreter
(ns org.enrico_franchi.paip.simplegrammar.grammar
(:use clojure.core))
(def *simple-grammar*
'((sentence -> (noun-phrase verb-phrase))
(noun-phrase -> (Article Noun))
(verb-phrase -> (Verb noun-phrase))
(Article -> the a)
(Noun -> ball man woman table)
(Verb -> hit took saw liked)))