This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def printUpTo(limit: Int): Unit = { | |
| for(i <- (0 to limit)) { | |
| println("i = " + i) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (ns colin.problems-99) | |
| (defn combinations [size, ls] | |
| (filter | |
| #(and (not (seq? (first %))) (= size (count %))) | |
| (tree-seq | |
| #(seq? (first %)) | |
| seq | |
| (map | |
| (fn [x] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;;; 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ; 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [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)) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;;; 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 |