Skip to content

Instantly share code, notes, and snippets.

View cgrand's full-sized avatar

Christophe Grand cgrand

View GitHub Profile

That's the kind of code that I'd like to make work across lumo & planck (and ideally any bootstrapped)

Is this unreasonable? Do you see any objection or technical issue with that?

  (def repl
    (letfn [(rep []
              (println (str (ns-name *ns*)) "=>")
              (read (fn [form ex]
 (if ex
@cgrand
cgrand / maze.clj
Created January 24, 2011 08:02
A maze generator (Wilson's algorithm) which can work with any topography (hextiles, torus variants, teapot etc.)
; 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]
@cgrand
cgrand / heredoc.clj
Last active March 6, 2021 17:16
An ugly hacky heredoc for Clojure
(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)"""
;; this file is a walkthrough of Moustache features, a web framework for Clojure
;; http://github.com/cgrand/moustache/tree/master
;; Moustache allows to declare routes, apply middlewares and dispatch on http methods.
;; Moustache is compatible with all frameworks built on Ring, including Compojure
(ns demo
(:use net.cgrand.moustache)
(:use [ring.adapter.jetty :only [run-jetty]])) ;; hmmm Ring without servlets
@cgrand
cgrand / tarjan.clj
Last active November 18, 2020 03:08
Implementing Tarjan in Clojure, step by step
;; 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 July 11, 2020 20:26 — 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)
;; (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 October 14, 2020 12:18
Mixing macros and code in cljc and supporting clj, cljs and self-hosted cljs, see https://github.com/cgrand/macrovich
;; 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 June 25, 2013 15:44
Fork me and paste your bot. Don't forget to clearly wtate which function to call.
;; * paste your code
;; * add a comment at the top telling which function to call
@cgrand
cgrand / bots.clj
Created June 26, 2013 14:57
Fork & paste your bot code here!
;; INCLUDE THE NAME OF THE BOTT FN AT THE TOP
(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 {})