Skip to content

Instantly share code, notes, and snippets.

View cgrand's full-sized avatar

Christophe Grand cgrand

View GitHub Profile
;; You are right that the f function passed to update gets called only once.
;; However the value passed to it may be composite.
;; multifocals
=> (defn swap [[a b]] [b a])
#'enliven.core.lenses/swap
=> (update {:a "A" :b "B" :c "C"}
(juxt :a :b) swap)
{:a "B", :b "A", :c "C"}
; the only reason for the protocol is to have keywords behave as lenses
; it could have been hard coded in fetch/putback
(defprotocol Lens
(-fetch [l x])
(-putback [l x v]))
(defn fetch [x l] (-fetch l x))
(defn putback [x l v] (-putback l x v))
(defn update [x l f & args]
@cgrand
cgrand / grammar.clj
Last active August 29, 2015 14:05
Parsley 2 VM, not yet incremental
(ns parsley2.core)
(defprotocol Asmable
(asm [x]))
(defrecord InlineAsm [ops]
Asmable
(asm [_] ops))
(extend-protocol Asmable
; helper functions are missing, be it for char ranges or for lookaheads
; however this snippet demoes:
; * error-correction (the change in correction between the 3rd and 4th case shows that parsnip looks for a minimal deletion)
; * lookahead
=> (let [alpha (fn [c] (<= (int \a) (int c) (int \z)))
p (parser :map
{:map ["{" (* :sym (? :ws) :sym) "}"]
:ws (+ " ")
:sym [(+ (->InlineAsm [:PRED alpha]))
(->InlineAsm [:PEEK (complement alpha)])]})]
@cgrand
cgrand / reader.clj
Last active August 29, 2015 14:06
readers from grammars
(defn lazy-reader [start prods]
(let [step (vm/stepper (asm/link (grammar start prods)))]
(fn self
([input] (self input (vector-tree-builder input)))
([input builder]
((fn f [state i]
(lazy-seq
(when (< i (count input))
(let [state (step state i (nth input i))]
(if (when-let [stacks (get state -2)]
@@ -271,4 +271,5 @@
name="%context.structEditingClojureSource.name"
description="%context.structEditingClojureSource.description"
+ parentId="ccw.ui.clojureEditorScope"
id="ccw.ui.clojureStructEditorScope">
</context>
@@ -576,4 +577,16 @@
<key
commandId="ccw.ui.edit.text.clojure.expand.selection.up"
+ contextId="ccw.ui.clojureStructEditorScope"
package base;
public class GCD {
public static long gcd(long a, long b) {
while (a != b) {
if (a < b) b -= a;
else a -= b;
}
return a;
@cgrand
cgrand / clojure_justice.patch
Last active August 29, 2015 14:06
jmh.output
From 0c89fecf532c7c73468164290af03bbed5559f3f Mon Sep 17 00:00:00 2001
From: Christophe Grand <christophe@cgrand.net>
Date: Tue, 16 Sep 2014 13:36:36 +0200
Subject: [PATCH] fix clojure benchmark
---
.../arithmetic/EuclidianGcdMicroBenchmark.java | 8 ++++++++
.../main/resources/snippets/clojure/arithmetic.clj | 23 +++++++++++++++++-----
2 files changed, 26 insertions(+), 5 deletions(-)
; discussed here https://groups.google.com/d/msg/clojure-dev/cWzMS_qqgcM/7IAhzMKzVigJ
; nested reduced
=> (transduce (comp (take 1)) conj [:a])
[:a]
=> (transduce (comp (take 1) (take 1)) conj [:a])
#<Reduced@65979031: [:a]>
=> (transduce (comp (take 1) (take 1) (take 1)) conj [:a])
#<Reduced@fcbc8d1: #<Reduced@60bea99a: [:a]>>
=> (transduce (comp (take 1) (take 1) (take 1) (take 1)) conj [:a])
(ns mutabots
"Reimplementation of transducers, in terms of processing functions instead
of reducing functions.
tl;dr: reducing-fn based transducers are a special case, influenced by reducers,
of processing-fn based transducers.
In Clojure 1.7.0-alpha2, transducers are expressed in terms of the existing
concept of reducing functions.
To sum it up, a transducer has currently the signature :