Skip to content

Instantly share code, notes, and snippets.

Christophe Grand cgrand

View GitHub Profile
@cgrand
cgrand / xfor.clj
Created Aug 26, 2015
xfor: transducing for
View xfor.clj
(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 Aug 29, 2015
Small annoyances of the editor
View ccw.md

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
View text-content.clj
=> (-> (javax.xml.parsers.DocumentBuilderFactory/newInstance)
.newDocumentBuilder
(.parse (java.io.ByteArrayInputStream.
(.getBytes
"<data>
<data url=\"http://example.org/\">hint</data>
3.14
</data>")))
.getDocumentElement
.getTextContent)
View payload.json
{"user":
{"actions": {},
"properties":
{"givenName": "mike",
"faimlyName": "amundsen",
"email": "m...@example.org"}}}
@cgrand
cgrand / sjacket.markdown
Last active Aug 29, 2015
Notes on sjacket
View sjacket.markdown

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?
View bifocal.clj
;; 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 Jun 6, 2014
Here is what I hacked on my flight back from Helsinki
View seqexp.clj
; 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)}
View State of Enliven.md

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 Aug 29, 2015
seqexp with custom registers
View registers.clj
; (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 Jul 2, 2014
Fitting a square peg into a round hole with clojure multimethods
View square-peg.clj
; 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))
You can’t perform that action at this time.