This file contains 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 counter (agent 0)) | |
(set-validator! counter (fn [n] (>= n @counter))) |
This file contains 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
(defn nums [] (iterate inc 2)) | |
(defn sieve [[p & xs]] | |
(remove #(= (rem % p) 0) xs)) | |
(defn primes [] | |
(map first (iterate sieve (nums)))) | |
;; user> (take 20 (primes)) | |
;; (2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71) |
This file contains 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 newton-raphson | |
(:use | |
[clojure.contrib.math :only (abs)] | |
[clojure.contrib.seq :only (find-first)])) | |
(defn newton-raphson [improve, init] | |
(iterate improve init)) | |
(defn within [eps xs] |
This file contains 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
interface Function1<A, R> { | |
R apply(A arg); | |
} | |
class Holder<T> { | |
public T value; | |
public Holder(T value) { | |
this.value = value; | |
} | |
} |
This file contains 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
(defn accumulator [n] | |
(let [i (atom n)] | |
(fn [x] (swap! i + x)))) | |
;; user> (doseq [i (range 1 6)] (-> i (* 2) acc println)) | |
;; 2 | |
;; 6 | |
;; 12 | |
;; 20 |
This file contains 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
object Accumulator { | |
def accumulator(n: Int): Int => Int = { | |
var i = n | |
(x: Int) => { i += x; i } | |
} | |
def main(args: Array[String]) { | |
val acc = accumulator(0) | |
(1 to 5).foreach { i => println(acc(i * 2)) } | |
} |
This file contains 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
;; hyone's solution to Longest Increasing Sub-Seq | |
;; https://4clojure.com/problem/53 | |
(fn longest-inc-seq [coll] | |
(reduce #(let [len-a (count %1) | |
len-b (count %2)] | |
(if (and (> len-b 1) (> len-b len-a)) %2 %1)) | |
[] | |
(reductions | |
(fn [xs y] |
This file contains 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
;; hyone's solution to Group a Sequence | |
;; https://4clojure.com/problem/63 | |
(fn my-group-by [pred coll] | |
(reduce | |
(fn [dict m] | |
(let [ret (pred m)] | |
(assoc dict ret (conj (or (dict ret) []) m)))) | |
{} coll)) |
This file contains 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
;; hyone's solution to Black Box Testing | |
;; https://4clojure.com/problem/65 | |
(defn seq-type [coll] | |
(let [base (empty coll)] | |
(cond | |
(= base {}) :map | |
(= base #{}) :set | |
(= base '()) (if (reversible? coll) :vector :list)))) |
This file contains 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
;; hyone's solution to Merge with a Function | |
;; https://4clojure.com/problem/69 | |
(fn my-merge-with [f & maps] | |
(reduce | |
(fn [a b] | |
(reduce | |
(fn [x [k v]] | |
(assoc x k (if (b k) (f v (b k)) v))) | |
b a)) |
OlderNewer