Skip to content

Instantly share code, notes, and snippets.

View stathissideris's full-sized avatar

Stathis Sideris stathissideris

  • London
View GitHub Profile
(ns binary.core
(:require [org.clojars.smee.binary.core :as b]
[clojure.java.io :as io]
[clojure.core.match :as m])
(:import org.clojars.smee.binary.core.BinaryIO
java.io.DataInput))
(defn fixed-string [len]
(b/string "ISO-8859-1" :length len))
@stathissideris
stathissideris / instaquote.clj
Last active August 29, 2015 13:56
parse quoted strings with instaparse
;;see http://stackoverflow.com/questions/16215169/clojure-csv-with-escaped-comma
(require '[instaparse.core :as insta])
((insta/parser
"quoted-field = <'\\\"'> (#'[^\"\\\\]+' | escaped-char)* <'\\\"'>
<escaped-char> = #'\\\\.'") "\"hello\\\" world\"")
;;and to parse comments like /** this is a test */
;;http://stackoverflow.com/questions/11125459/java-regex-negative-lookahead
alias ipext='curl -s http://checkip.dyndns.org/ | grep -o [0-9][0-9]*.[0-9][0-9]*.[0-9][0-9]*.[0-9]*'
@stathissideris
stathissideris / safe_walk.clj
Last active August 29, 2015 13:57
safe walk
(defn prewalkseq
"Like prewalk but only for seqs and uses zippers."
[f s]
(loop [z (zip/seq-zip s)]
(if (zip/end? z)
(zip/root z)
(recur (zip/next (zip/replace z (f (zip/node z))))))))
;;replace zip/seq-zip with zip/vector-zip for vectors
@stathissideris
stathissideris / lazy-calc.clj
Last active August 29, 2015 13:57
Attempting to deal with the situation of lazy calculations that have to have access to the "state" of previous values
;;map lag with multiple accumulators
(defn map-lag
"fun is a function with arity equal to the number of accumulators
+1. It's expected to return a vector of the same length as its
arity.
accs is a vector of initial states for all the
accumulators.
coll is the collection to be processed."

Retrograde

The past is a grotesque animal

And in its eyes you see

How completely wrong you can be

-- Of Montreal

@stathissideris
stathissideris / reduce-kv.clj
Created April 24, 2014 13:27
reduce-kv to transform the keys of a map
> (reduce-kv (fn [m k v] (assoc m (str k) v)) {} {:a 1 :b 2})
{":a" 1, ":b" 2}
(require '[clojure.data.generators :as gen])
(defn weighted-interleave
"Given a map of infinite sequences to weights, it produces a new
infinite sequence where each element is selected randomly (according
to the weights) from one of the passed sequences. The sequences are
only advanced when the element is actually selected.
Example:
@stathissideris
stathissideris / datomic_clojure_collections.clj
Last active August 29, 2015 14:02 — forked from stuarthalloway/gist:2645453
Datomic queries against Clojure collections
;; Datomic example code
(use '[datomic.api :only (db q) :as d])
;; ?answer binds a scalar
(q '[:find ?answer :in ?answer]
42)
;; of course you can bind more than one of anything
(q '[:find ?last ?first :in ?last ?first]
"Doe" "John")
@stathissideris
stathissideris / coercion.clj
Created July 10, 2014 10:38
Coercion of nested EDN with schema
def edn-string
(schema/named schema/Any "Should be a valid EDN string"))
(defn saved-search-matcher [schema]
({schema/Num #(try (Double/parseDouble %) (catch Exception _ %))
edn-string #(try (read-string %) (catch Exception _ %))} schema))
(def search-criteria
(schema/both
edn-string