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
;; jneira's solution to http://4clojure.com/problem/63 | |
(fn [f c] (reduce (fn [m n] (merge-with into m {(f n) [n]})) {} c)) |
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
;; jneira's solution to http://4clojure.com/problem/67 | |
(fn primes [n] | |
(letfn | |
[(next-prime | |
[prs nxt] | |
(if (empty? (filter #(= 0 (mod nxt %)) prs)) | |
(conj prs nxt) | |
(recur prs (inc nxt))))] | |
(->> (iterate |
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
;; jneira's solution to http://4clojure.com/problem/67 | |
(defn primes [n] | |
(let | |
[next-prime | |
(fn [prs] | |
(letfn | |
[(aux [[f & r :as c] nxt] | |
(cond | |
(empty? c) (conj prs nxt) | |
(zero? (mod nxt f)) (recur prs (inc nxt)) |
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
{-# LANGUAGE OverloadedStrings #-} | |
{- To Run: | |
Load in ghci | |
:set -XOverloadedStrings (for convenience) | |
Execute repl expr -} | |
import Control.Applicative | |
import Data.Attoparsec hiding (Result) | |
import Data.Attoparsec.Char8 (char8, isDigit_w8, isSpace_w8) |
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 neighbors-of [cell] | |
(set (for [dx [-1 0 1] dy [-1 0 1] :when (not (= [dx dy] [0 0]))] | |
[(+ dx (first cell)) (+ dy (second cell))]))) | |
(defn alive? [[cell freqs] world] | |
(or (and (= 2 freqs) (contains? world cell)) (= 3 freqs))) | |
(defn tick [world] | |
(let [frequencies (frequencies (reduce #(concat %1 (neighbors-of %2)) [] world))] | |
(set (keys (filter #(alive? % world) frequencies))))) |
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
;; jneira's solution to http://4clojure.com/problem/73 | |
(fn winner [board] | |
(let [trasp (apply map vector board) | |
diag #(map-indexed (fn [x y] (y x)) %) | |
all (concat board trasp | |
(map diag [board (reverse board)]))] | |
(some #(or (:x %) (:o %)) | |
(filter #(= 1 (count %)) (map set all))))) |
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
;; jneira's solution to http://4clojure.com/problem/74 | |
(fn squares [s] | |
(let [nums (map read-string (.split s ",")) | |
sqrs (filter #(= % (Math/pow (Math/sqrt %) 2)) nums)] | |
(apply str (interpose "," sqrs)))) |
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
;; jneira's solution to http://4clojure.com/problem/81 | |
(fn [f & r] (set (filter f (apply concat r)))) |
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
;; jneira's solution to Power Set | |
;; https://4clojure.com/problem/85 | |
;; translated directly from algorithm described in wikipedia :-P | |
(fn P [S] | |
(if (empty? S) #{S} | |
(let [e (first S) | |
T (disj S e) | |
PT (P T) | |
F (fn [e T] (set (map #(conj % e) T)))] |
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
;; jneira's solution to Transitive Closure | |
;; https://4clojure.com/problem/84 | |
(fn [s] | |
(let [f #(for [[a b] % [c d] % | |
:when (= c b)] [a d])] | |
(->> s (iterate #(let [n (into % (f %))] | |
(when (not= % n) n))) | |
(take-while identity) | |
(last)))) |