Skip to content

Instantly share code, notes, and snippets.

View jneira's full-sized avatar
:octocat:

Javier Neira jneira

:octocat:
View GitHub Profile
;; jneira's solution to http://4clojure.com/problem/63
(fn [f c] (reduce (fn [m n] (merge-with into m {(f n) [n]})) {} c))
;; 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
;; 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))
@jneira
jneira / lisp.hs
Created May 1, 2011 10:26
Minimal Lisp in Haskell
{-# 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)
@jneira
jneira / gist:950743
Created May 1, 2011 18:57 — forked from slagyr/gist:950574
Game of Life in 8 lines of Clojure
(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)))))
;; 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)))))
;; 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))))
;; jneira's solution to http://4clojure.com/problem/81
(fn [f & r] (set (filter f (apply concat r))))
;; 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)))]
;; 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))))