Skip to content

Instantly share code, notes, and snippets.

@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"))))
@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 / 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 / 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 / Chapter13.js
Created February 28, 2011 06:09
Exercises from Chapter 13 of Eloquent Javascript
// Ex 13.1 of Chapter 13 in Eloquent Javascript
// Write a function called registerEventHandler to wrap the
// incompatibilities of these two models. It takes three arguments: first
// a DOM node that the handler should be attached to, then the name of
// the event type, such as "click" or "keypress", and finally the handler
// function.
function registerEventHandler (node, eventType, handlerFN) {
if (node.attachEvent) { //IE
@Melipone
Melipone / Chapter12.js
Created February 28, 2011 06:06
Exercises from Chapter 12 of Eloquent Javascript
// Ex. 12.1
//Write a function asHTML which, when given a DOM node,
//produces a string representing the HTML text for that node
//and its children. You may ignore attributes, just show nodes
//as <nodename>. The escapeHTML function from chapter 10 is
//available to properly escape the content of text nodes.
function escapeHTML(text) {
var replacements = {"<": "&lt;", ">": "&gt;",
@Melipone
Melipone / dom3.html
Created February 20, 2011 04:08
Week 4 homework #3 on the Theory of the DOM
<script type="text/javascript">
function walkTheDOM(node, func) {
func(node);
node = node.firstChild;
while (node) {
walkTheDOM(node, func);
node = node.nextSibling;
}
}
function countTextNodes() {
@Melipone
Melipone / dom2.html
Created February 20, 2011 04:06
Week 4 homework #2 on the Theory of the DOM
<script type="text/javascript">
function walkTheDOM(node, func) {
func(node);
node = node.firstChild;
while (node) {
walkTheDOM(node, func);
node = node.nextSibling;
}
}
function countTextNodes() {
@Melipone
Melipone / dom1.html
Created February 20, 2011 04:03
Week 4 homework #1 on the theory of the DOM
<script type="text/javascript">
function walkTheDOM(node, func) {
func(node);
node = node.firstChild;
while (node) {
walkTheDOM(node, func);
node = node.nextSibling;
}
}
function countTextNodes() {
@Melipone
Melipone / Chapter6.js
Created February 16, 2011 02:38
Exercises in Chapter 6 of Eloquent Javascript
// coming soon
// Ex. 6.1
function forEach(array, action) {
for (var i = 0; i < array.length; i++)
action(array[i]);
}
function reduce(combine, base, array) {
forEach(array, function (element) {