Skip to content

Instantly share code, notes, and snippets.

@Chouser
Chouser / gist:9023572
Created February 15, 2014 18:56
Paths in a graph
(defn paths
"Returns a lazy sequence of path vectors from path [<start-node>] to
target in graph."
[graph target path]
(let [this-node (peek path)]
(if (= this-node target)
[path]
(->> (get graph this-node)
(filter #(not-any? (fn [edge] (= edge [this-node %]))
(partition 2 1 path)))
@Chouser
Chouser / types-fixed.ml
Last active August 29, 2015 14:14
Attempt at recursive map type in OCaml
module MakeVal (MapType : Map.S) =
struct
let compare = Pervasives.compare
type map_type = t MapType.t
and 'a with_meta = { value : 'a; meta : map_type }
and t =
| List of t list with_meta
| Vector of t list with_meta
| Map of map_type with_meta
| Int of int
@Chouser
Chouser / mapmap.ml
Last active August 29, 2015 14:14
Attempt at recursive map type in OCaml
module rec VariantMod
: sig
type t = Int of int | Map of t OrderedValMap.t
end = VariantMod
and OrderedVal
: sig
type t = VariantMod.t
val compare : t -> t -> int
end
= struct
let slurp filename =
let ic = open_in filename in
let rec loop out_str =
try
loop out_str ^ (input_line ic) ^ "\n"
with e ->
close_in ic;
out_str in
loop ""
@Chouser
Chouser / gist:20f114d9c53a647f2c12
Created February 25, 2015 06:27
Self-modifying forth
: site-counter ( literal-int -- int )
here cell - { addr } \ store in 'addr' the address of the literal int preceding this in the user's code
postpone dup \ compile code to ...dup the int at the top of the data stack
postpone 1+ \ ...increment it
addr postpone literal \ ...push the above 'addr' onto the data stack
postpone ! \ ...copy the incremented value INTO THE USERS CODE
; immediate \ do all this at compile time
\ Now for a "normal" word defintion
: bar ( -- )
@Chouser
Chouser / tag-fn.clj
Created March 3, 2015 01:56
Tag a function to make it available with multiple prefixes.
(defn tag [tags the-var]
(binding [*ns* nil]
(doseq [tag tags]
(intern (in-ns tag) (.sym the-var) @the-var))))
(tag '[A B C]
(defn foo [a b]
(+ a b)))
(A/foo 5 10)
Clojure 1.7.0-beta2
;; ns-unmap doesn't fully take effect on uses of the unmapped var in the same compilation unit:
user=> (let [] (ns-unmap 'user 'first) first)
#object[clojure.core$first__4107 0x4b8729ff "clojure.core$first__4107@4b8729ff"]
;; ...but has taken effect by the time the next top level form is compiled:
user=> first
CompilerException java.lang.RuntimeException: Unable to resolve symbol: first in this context, compiling:(NO_SOURCE_PATH:0:0)
(ns org.drewolson.test-dragon
(:use clojure.contrib.test-is org.drewolson.dragon)
(:gen-class))
(deftest foo
(is (= 1 1)))
(defn -main []
(run-tests 'org.drewolson.test-dragon)
(.flush *test-out*))
; updated for Clojure SVN 1309
(defn parse-grid [s]
(vec (map (fn [line] (vec (map #(Integer. %) (.split line ","))))
(.split s "\\s+"))))
(defn main [gridstr]
(let [costs (parse-grid gridstr)
size (count costs)]
(loop [min-sums (vec (replicate size (vec (replicate size nil))))
(ns org.drewolson.dragon
(:gen-class
:extends javax.swing.JFrame))
(defn -paint [this graphics]
(prn :paint-called)
(doto graphics
(.drawString "Hello, World" 20 20)))
(defn -main []