Skip to content

Instantly share code, notes, and snippets.

View angerman's full-sized avatar
🇸🇬

Moritz Angermann angerman

🇸🇬
View GitHub Profile
(defn lazy-combine-sorted [xs ys]
"lazily combines xs and ys and returns their union (assumes xs ys sorted)"
(lazy-seq
(if-let [[x & xs*] xs]
(if-let [[y & ys*] ys]
(cond (= x y) (cons x (lazy-combine-sorted xs* ys*))
(< x y) (cons x (lazy-combine-sorted xs* ys))
(> x y) (cons y (lazy-combine-sorted xs ys*)))
xs) ; no more ys
ys))) ; no more xs
(defn lazy-combine-sorted [xs ys]
"lazily combines xs and ys and returns their union (assumes xs ys sorted)"
(lazy-seq
(if-let [[x & xs*] xs]
(if-let [[y & ys*] ys]
(cond (= x y) (cons x (lazy-combine-sorted xs* ys*))
(< x y) (cons x (lazy-combine-sorted xs* ys))
(> x y) (cons y (lazy-combine-sorted xs ys*)))
xs) ; no more ys
ys))) ; no more xs
(defn compute-metrics [[header lines]]
(loop [[ln & lns*] lines
features nil
experiments 0]
(printf "%d, %d\n" experiments (count features))
(if (not ln)
(assoc header
:experiments experiments
:features features)
(recur lns*
(defn lazy-combine-sorted [xs ys]
"lazily combines xs and ys and returns their union (assumes xs ys sorted)"
(if (empty? xs) ys)
(if (empty? ys) xs)
(lazy-seq
(if-let [[x & xs*] xs]
(if-let [[y & ys*] ys]
(cond (= x y) (cons x (lazy-combine-sorted xs* ys*))
(< x y) (cons x (lazy-combine-sorted xs* ys))
(> x y) (cons y (lazy-combine-sorted xs ys*)))
(if (< a b) ;; either way
(recur a- b+ (conj acc a))
(recur a+ b- (conj acc b))))))))
(defn combine [A B]
(combine-sorted (sort A) (sort B)))
(defn lazy-combine-sorted [xs ys]
"lazily combines xs and ys and returns their union (assumes xs ys sorted)"
(if (empty? xs) ys)
(if (empty? ys) xs)
(lazy-seq
(if-let [[x & xs*] xs]
(if-let [[y & ys*] ys]
(cond (= x y) (cons x (lazy-combine-sorted xs* ys*))
(< x y) (cons x (lazy-combine-sorted xs* ys))
(> x y) (cons y (lazy-combine-sorted xs ys*)))
(defn lazy-combine-sorted [xs ys]
"lazily combines xs and ys and returns their union (assumes xs ys sorted)"
(lazy-seq
(if-let [[x & xs*] xs]
(if-let [[y & ys*] ys]
(cond (= x y) (cons x (lazy-combine-sorted xs* ys*))
(< x y) (cons x (lazy-combine-sorted xs* ys))
(> x y) (cons y (lazy-combine-sorted xs ys*)))
xs) ; no more ys
ys))) ; no more xs
;; LibSVM Model Parser
(use 'clojure.contrib.duck-streams)
(use 'clojure.contrib.seq-utils)
(require '[clojure.contrib.str-utils2 :as s])
(load "coll-util")
(def *root* "/Users/angerman/Downloads/libsvm-2.9-dense/tools/")
(ns clojure.contrib.duck-streams
(:import (java.io File RandomAccessFile)))
;; copied and adapted from c.c.ds
(defmulti #^{:arglist '([x])} random-access-reader class)
(defmethod random-access-reader File [#^File x]
(RandomAccessFile. x "r"))
(defmethod random-access-reader String [#^String x]
(random-access-reader (File. x)))
(in-ns 'clojure.contrib.duck-streams)
(import '(java.io File RandomAccessFile))
;; copied and adapted from c.c.ds
(defmulti #^{:arglist '([x])} random-access-reader class)
(defmethod random-access-reader File [#^File x]
(RandomAccessFile. x "r"))
(defmethod random-access-reader String [#^String x]
(random-access-reader (file-str x)))