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 Partially Flatten a Sequence | |
;; https://4clojure.com/problem/93 | |
(fn f [c] | |
(if (coll? c) | |
(if (some coll? c) | |
(mapcat f c) [c]) | |
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 Read Roman numerals | |
;; https://4clojure.com/problem/92 | |
(fn rr [[f & [s & r :as nxt]]] | |
(condp #(or %1 %2) nil | |
({"IV" 4 "IX" 9 "XL" 40 "XC" 90 "CD" 400 "CM" 900} | |
(str f s)) :>> #(+ % (rr r)) | |
({"I" 1 "V" 5 "X" 10 "L" 50 "C" 100 "D" 500 "M" 1000} | |
(str f)) :>> #(+ % (rr nxt)) | |
0)) |
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 Graph Connectivity | |
;; https://4clojure.com/problem/91 | |
(fn [c] | |
(let [[f & r] (seq c) | |
h #(apply concat %)] | |
(= (set (h c)) | |
(reduce (fn [a x] | |
(into a (h (filter #(some a %) c)))) | |
(set f) 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 Beauty is Symmetry | |
;; https://4clojure.com/problem/96 | |
(fn s [[_ [x l r] | |
[y i d]]] | |
(and (= x y) | |
(or (= nil l i) | |
(and | |
(s [_ l d]) | |
(s [_ r i]))))) |
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
function aplaneitor(soa,acu) { | |
if (soa instanceof Array) { | |
for (var j=0, len = soa.length;j<len;j++) { | |
aplaneitor(soa[j],acu); | |
} | |
} | |
else { | |
acu.push(soa); | |
} | |
} |
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
function flatten (xs) { | |
return xs.reduce (function (acc,x){ | |
return acc.concat(Array.isArray(x)?flatten(x):x)},[]) | |
} | |
function phineas (arr,sep) { | |
return flatten(arr).join(sep) | |
} | |
var res=phineas(["hola", ["soy", ["juan", "fernandez"] ], "y", ["no", "tengo", ["dinero"] ] ],"*") | |
console.log(res) |
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
module Foldtree where | |
data Tree a = Leaf a | Trees [Tree a] deriving(Show) | |
cow = Trees [Leaf 1, Trees [ Leaf 2, Leaf 3], Leaf 4] | |
-- Using concatMap | |
aplaneitor1 :: Tree a -> [a] | |
aplaneitor1 (Leaf a) = [a] | |
aplaneitor1 (Trees trees) = trees >>= aplaneitor1 |
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
;; I cant resist paste my version writing bootom-up through interactive driven devlopment ;-) | |
;; the origin of the puzzle is http://swizec.com/blog/functional-isnt-always-better/swizec/2591 | |
(defn tails [xs] | |
(take-while seq (iterate rest xs))) | |
(defn pairs [xs] | |
(mapcat (fn [[x & y]] (map list (repeat x) y)) | |
(tails xs))) | |
;; some vesions in comments in the blog post |
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
// Compositional evaluator as visitor | |
import java.math.BigInteger; | |
// Syntax | |
interface Exp { | |
<X> X accept(Visitor<X> v); | |
} |
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 rand-tree) | |
(defn seed [] | |
(let [r #(when (zero? (rand-int 2)) seed)] | |
{:left (r) | |
:right (r)})) | |
(def seed? fn?) | |
(def leaf? nil?) | |
(def branch? map?) |