Skip to content

Instantly share code, notes, and snippets.

View trptcolin's full-sized avatar

Colin Jones trptcolin

View GitHub Profile
(load "/Users/colin/lib/test-manager/load.scm")
(define (prime-factors product)
(list-factors product 2))
(define (list-factors product test-factor)
(if (< product 2)
(list)
(if (= 0 (modulo product test-factor))
(append (list test-factor) (list-factors (/ product test-factor) test-factor))
def printUpTo(limit: Int): Unit = {
for(i <- (0 to limit)) {
println("i = " + i)
}
}
(ns colin.problems-99)
(defn combinations [size, ls]
(filter
#(and (not (seq? (first %))) (= size (count %)))
(tree-seq
#(seq? (first %))
seq
(map
(fn [x]
module Kernel
def collate(*methods)
methods.map {|m| send(m)}
end
end
first, last = User.collate :first, :last # => [#<User id: 1 name: "Pat">, #<User id: 85 name: "Joe">]
avg, max, min = [4, 3, 1, 5, 2].collate :avg, :max, :min # => [3.0, 5, 1]
;;; Pythagorean Mean computation
;;; Using a lazy sequence to allow for computing the mean at a given point in
;;; an infinite sequence.
;;;
;;; Idea from Clojure mailing list:
;;; (http://groups.google.com/group/clojure/browse_thread/thread/c5b98ef74dc5809f)
;;;
;;; Reference:
;;; http://rosettacode.org/wiki/Averages/Pythagorean_means
(ns sieve
(:use clojure.test))
(defn square [x] (* x x))
(defn seq-contains? [ls x]
(boolean (some #{x} ls)))
(defn multiple-of? [product divisor]
(and (= 0 (mod product divisor))
(ns primeFactors)
(defn square [n] (* n n))
(defn divisible-by? [n candidate]
(= 0 (rem n candidate)))
(defn of [n]
(loop [factors [] n n candidate 2]
(cond
; Can someone explain this to me?
(use 'clojure.test)
(are [x y] (= x y)
'(4 9 16) (map (fn [x] (* x x)) '(2 3 4)))
;;; java.lang.Exception: Unsupported binding form: clojure.lang.LazySeq@7bed76a7
(macroexpand '(are [x y] (= x y)
[java] FAIL in (can-test-using-local-bindings) (test_clojure.clj:87)
[java] but got :pass
[java] expected: (= (quote (4 9 16)) (map (fn [x] (* x x)) (quote (2 3 4))))
[java] actual: (#<core$_EQ_ clojure.core$_EQ_@a947850> (4 9 16) (4 9 16))
;;; Removing ns info from macro expansion
(use 'clojure.walk)
(defn macroexpand-no-ns [form]
(prewalk (fn [x] (if (or (symbol? x) (keyword? x)) (symbol (name x)) x))
(macroexpand form)))
;;; Example