Skip to content

Instantly share code, notes, and snippets.

View cgrand's full-sized avatar

Christophe Grand cgrand

View GitHub Profile
@cgrand
cgrand / xfor.clj
Created August 26, 2015 04:09
xfor: transducing for
(defmacro xfor
"Like for except the first expression is omitted. Returns a transducer."
[[binding & seq-exprs] body]
(let [rf (gensym 'rf)
acc (gensym 'acc)
body
(reduce (fn [body [expr binding]]
(case binding
:let `(let ~expr ~body)
:when `(if ~expr ~body ~acc)
@cgrand
cgrand / ccw.md
Last active August 29, 2015 13:56
Small annoyances of the editor

Defects

  • selection is not always maintained after autoshift of indentation
  • autoshit is sometimes off by -1
  • split does not preserve linebreaks
  • structural commands still work when in comments

Enhacement

  • Adds a "Source" context menu to enable discovery of commands
  • typing a semicolon should add a linebreak
@cgrand
cgrand / payload.json
Last active August 29, 2015 13:57 — forked from mamund/uber-doc.xml
{"user":
{"actions": {},
"properties":
{"givenName": "mike",
"faimlyName": "amundsen",
"email": "m...@example.org"}}}
=> (-> (javax.xml.parsers.DocumentBuilderFactory/newInstance)
.newDocumentBuilder
(.parse (java.io.ByteArrayInputStream.
(.getBytes
"<data>
<data url=\"http://example.org/\">hint</data>
3.14
</data>")))
.getDocumentElement
.getTextContent)
@cgrand
cgrand / sjacket.markdown
Last active August 29, 2015 13:58
Notes on sjacket

Returning to sjaket after 18 months, here are my first reactions:

  • The code could be worse given it was a talk-driven development and that Mr. Edmund Jackson forced me to drink too many beers.
  • Today I tried implementing selections so that code can work on more than one expression:
    • they are several kind of selections:
      • raw selections
      • structuraly valid selections
      • structuraly valid antiselections (selections which when suppressed yield a valid structure, eg (a |b) (c| d))
    • is a position a collapsed selection?
    • insertion points are still missing when dealing with zippers: you can't be around nodes
  • how to insert spaces?
;; bifocal is the lens library which I may spin off the enliven codebase
;; a lens is the generalization of associative access/update
;; fetch/putback are the pendant of get/assoc
;; symbols, strings, numbers, keywords, regex are lenses on their own
=> (fetch {:a 1 :b 2} :a)
1
=> (putback {:a 1 :b 2} :a 6)
@cgrand
cgrand / seqexp.clj
Created June 6, 2014 12:50
Here is what I hacked on my flight back from Helsinki
; regexps on seqables, implemented using a Pike and Janson vm.
; linear with the size of the input, supports greedy and reluctant operators.
=> (exec (as :whole
(as :pre
(* odd?) #(== 3 %))
(* (constantly true))
#(== 7 %))
[1 3 3 7])
{:pre (1 3 3), :whole (1 3 3 7)}
@cgrand
cgrand / State of Enliven.md
Created June 10, 2014 20:50
State of Enliven

After an initial "sprint" (a block of days I've been able to allocate to Enliven dev), I've only been able to work on it on and off hence the slow pace of development.

Locally I've been refactoring and learning from the initial code burst. I've been experimenting too, especially with several flavors of encoding composition:

  • pre-rendering constant parts as bytes
  • pre-rendering constant parts as gzipped bytes (moderate success: it works but needs work to, at least, trigger this only on constant strings above a given threshold)
  • pre-rendering constant parts as direct bytebuffers and rendering the whole template as an array of bytebuffers to leverage vectored IO (aka gather IO)

However the current goal is to add dynamic (reactive) templating.

From an abstract point of view I try to make Enliven's core a meta templating system. (Currently there's only static html and static text.)

@cgrand
cgrand / registers.clj
Last active August 29, 2015 14:03
seqexp with custom registers
; (as of https://github.com/cgrand/seqexp/commit/0defb92c1f28fdb5ae37fd7d7325b29fc7518d0f)
; (spelling of 'occurrence' is fixed by the next commit...)
; plain old last subgroup behaviour
=> (exec (*' (+? odd?) (as :threes (+ 3)))
[3 3 3 7 7 3 3 2])
{:threes (3 3), :match (3 3 3 7 7 3 3), :rest (2)}
; but now we can ask for all the occurrences for a given group
=> (exec (*' (+? odd?) (as :threes (+ 3)))
@cgrand
cgrand / square-peg.clj
Created July 2, 2014 09:30
Fitting a square peg into a round hole with clojure multimethods
; by lambdanext
(defmulti fits? (fn [hole peg] [(:type hole) (:type peg)]))
(defmethod fits? [:circle :circle] [{hole-diameter :diameter} {peg-diameter :diameter}]
(<= peg-diameter hole-diameter))
(defmethod fits? [:circle :square] [{hole-diameter :diameter} {peg-length :length}]
(<= (* peg-length (Math/sqrt 2)) hole-diameter))