Skip to content

Instantly share code, notes, and snippets.

@Melipone
Melipone / remove-duplicates
Created October 14, 2011 14:53
remove-duplicates in clojure based on the distinct function
(defn remove-duplicates
"Returns a lazy sequence of the elements of coll with duplicates removed using a predicate"
[coll pred]
(let [step (fn step [xs seen]
(lazy-seq
((fn [[f :as xs] seen]
(when-let [s (seq xs)]
(if (some #(pred f %) seen)
(recur (rest s) seen)
(cons f (step (rest s) (conj seen f))))))
@Melipone
Melipone / viterbi.clj
Created April 4, 2012 14:26
Viterbi algorithm
(ns ident.viterbi
(:use [clojure.pprint]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Example
;; (def initpr (to-array [0.6 0.4]))
;; (def transpr (to-array-2d [[0.7 0.3][0.4 0.6]]))
;; (def emisspr (to-array-2d [[0.1 0.4 0.5][0.6 0.3 0.1]]))
;; (def hmm (make-hmm {:states ["rainy" "sunny"] :obs ["walk" "shop" "clean"] :init-probs initpr :emission-probs emisspr :state-transitions transpr}))
;; optimal state sequence and probability of sequence
@Melipone
Melipone / testdynamic.clj
Created May 1, 2013 17:54
Dynamic variables
(declare ^:dynamic myvar)
(def myvar nil)
(defn mycounter [x]
(inc x))
(defn testdynvar [x]
(myvar x))
@Melipone
Melipone / mymap. clj
Last active October 20, 2017 08:41
Strange hash behavior
(import [java.util HashMap])
(def mymap (new HashMap))
(. mymap put (hash "http://www.google.com/") "serp")
(def myhash (hash "http://www.google.com/"))
myhash
;;-87748806
(. mymap get myhash)
;;"serp"
(spit "mytest.fb" (into {} mymap))
(def mymap2 (java.util.HashMap. (read-string (slurp "mytest.fb"))))