Skip to content

Instantly share code, notes, and snippets.

Avatar

Christophe Grand cgrand

View GitHub Profile
@cgrand
cgrand / tarjan.clj
Last active Nov 18, 2020
Implementing Tarjan in Clojure, step by step
View tarjan.clj
;; Now, replace the loop by more telling operations.
(defn tarjan
"Returns the strongly connected components of a graph specified by its nodes
and a successor function succs from node to nodes.
The used algorithm is Tarjan's one."
[nodes succs]
(letfn [(sc [env node]
; env is a map from nodes to stack length or nil, nil means the node is known to belong to another SCC
; there are two special keys: ::stack for the current stack and ::sccs for the current set of SCCs
@cgrand
cgrand / gist:564d6e8ad57299f64beb438e4a8b709f
Created Jul 11, 2020 — forked from KingCode/gist:773560f4ab5bf91e660a2a26e581b036
cond-let and cond-let| macros, to leverage bindings between test and result expressions, as well as earlier ones (for cond-let)
View gist:564d6e8ad57299f64beb438e4a8b709f
;; (cond-let
;; (odd? x) [x n] (inc x)
;; (< n 10) [y (inc n)] 10
;; :else n))
;; we want the above to yield
;; (let [x n]
;; (if (odd? x)
;; (inc x)
@cgrand
cgrand / core.cljc
Last active Oct 14, 2020
Mixing macros and code in cljc and supporting clj, cljs and self-hosted cljs, see https://github.com/cgrand/macrovich
View core.cljc
;; SEE: https://github.com/cgrand/macrovich
;; macros and code in a single cljc working across clj, cljs and self-hosted cljs
;; require clojurescript from master
(ns foo.core
#?(:cljs (:require-macros
[net.cgrand.meta-macros :refer [macros no-macros]]
[foo.core :refer [add]])
:clj (:require
@cgrand
cgrand / bot.clj
Created Jun 25, 2013
Fork me and paste your bot. Don't forget to clearly wtate which function to call.
View bot.clj
;; * paste your code
;; * add a comment at the top telling which function to call
@cgrand
cgrand / bots.clj
Created Jun 26, 2013
Fork & paste your bot code here!
View bots.clj
;; INCLUDE THE NAME OF THE BOTT FN AT THE TOP
View rollup.clj
(require '[net.cgrand.xforms :as x])
(defn rollup [dimensions valfn]
(let [[dim & dims] (reverse dimensions)]
(reduce
(fn [xform dim]
(comp
(x/by-key dim xform)
(x/transjuxt
{:detail (x/into {})
@cgrand
cgrand / join.clj
Created Oct 14, 2017
Join transducer
View join.clj
(defn join
"Joins pairs returned by nested transducers based on their key (first item).
Each nested transducer must returns pairs made of a strictly increasing key and a value.
Emits pairs made of the join key and a vector of the joined values (nil when none).
Values in the vector appears in the same order as the xform that produced them."
([] identity)
([xform] xform)
([xform1 xform2]
(fn [rf]
(let [vq1 (volatile! clojure.lang.PersistentQueue/EMPTY)
View mutabots.clj
(ns mutabots)
(defn map [f]
(fn [p1]
(fn
([] (p1))
([x] (p1 (f x))))))
(defn filter [pred]
(fn [p1]
@cgrand
cgrand / heredoc.clj
Last active Feb 12, 2020
An ugly hacky heredoc for Clojure
View heredoc.clj
(defn heredoc []
(let [delim (.readLine *in*)]
(->> (repeatedly #(.readLine *in*))
(take-while #(not= delim %))
(interpose \newline)
(apply str))))
; The following lines are read (by the reader) as:
; "Look )(\"\\T\na here doc!\n"
#=(heredoc)"""
@cgrand
cgrand / maze.clj
Created Jan 24, 2011
A maze generator (Wilson's algorithm) which can work with any topography (hextiles, torus variants, teapot etc.)
View maze.clj
; http://groups.google.com/group/clojure/browse_thread/thread/974e2c7f89e27231/5f4bff3e58dfa36f
; output images http://i.imgur.com/gSASS.png (square maze)
; http://i.imgur.com/uEqaq.png (hex maze)
;; generic Wilson's algorithm implementation
(defn maze
"Returns a random maze carved out of walls; walls is a set of
2-item sets #{a b} where a and b are locations.
The returned maze is a set of the remaining walls."
[walls]
You can’t perform that action at this time.