Skip to content

Instantly share code, notes, and snippets.

View stathissideris's full-sized avatar

Stathis Sideris stathissideris

  • London
View GitHub Profile
(defn lcs [xr yr]
((fn inner-lcs [x y]
(cond
(or (zero? (count x)) (zero? (count y))) nil
(= (first x) (first y)) (cons (first x) (inner-lcs (rest x) (rest y)))
:else (let [xx (inner-lcs (rest x) y)
yy (inner-lcs x (rest y))]
(if (> (count xx) (count yy)) xx yy)))) xr yr))
> (print "result:" (lcs "Stathis Georgiou Sideris" "Stathis Sideris") "\n")
@stathissideris
stathissideris / HashOfHashes.java
Created August 26, 2011 10:48
Hash of hashes example in Java
package test;
import java.util.HashMap;
public class HashOfHashes {
public static void main(String[] args) {
//deep's type is a HashMap with String keys. Its values are Hashes with String keys and values
//Notice how you have to say all that twice, just to be sure :-/
HashMap<String, HashMap<String, String>> deep = new HashMap<String, HashMap<String, String>>();
@stathissideris
stathissideris / gist:3705914
Created September 12, 2012 10:51 — forked from Chouser/gist:3687532
Lazy seq as event subscription mechanism
;; Here is a spike of a lightweight in-process pubsub mechanism that allows pure ;; functional consumers, both blocking and asynchronous.
;; This defines the event stream, in this case just a series of numbers,
;; a new one produced each second
(defn timer []
(lazy-seq
(do
(Thread/sleep 1000)
(cons (System/nanoTime) (timer)))))
(ns lcs.core)
;(def a [:a :b :b :c :d :e :f :z])
;(def b [:a :b :b :d :e :f :g :a])
(def a "abcabba")
(def b "cbabac")
(defn indexed [coll] (map vector (iterate inc 1) coll))
@stathissideris
stathissideris / ditaa.clj
Created December 19, 2012 02:42
What ditaa would look like in Clojure
(require '[clojure.string :as string])
(defn gget [grid [x y]]
(get-in grid [y x] \space))
(defn gassoc [grid [x y] v]
(assoc-in grid [y x] v))
(defn dimensions [grid]
[(apply max (map count grid)) (count grid)])
@stathissideris
stathissideris / channel-as-seq.clj
Last active October 20, 2020 19:14
clojure async channels as lazy (and blocking!) sequences
(defn seq!!
"Returns a (blocking!) lazy sequence read from a channel."
[c]
(lazy-seq
(when-let [v (<!! c)]
(cons v (seq!! c)))))
(comment
(def stream (chan))
(go (dotimes [x 16]
(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