Skip to content

Instantly share code, notes, and snippets.

Avatar
:octocat:

Javier Neira jneira

:octocat:
View GitHub Profile
@jneira
jneira / Evaluator.java
Created Oct 28, 2011 — forked from dvanhorn/Evaluator.java
Compositional interpreter for lambda calculus as Visitor in Java for @ccshan @djspiewak @kaleidic
View Evaluator.java
// Compositional evaluator as visitor
import java.math.BigInteger;
// Syntax
interface Exp {
<X> X accept(Visitor<X> v);
}
View idd.clj
;; 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
View aplastaneitor.hs
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
View phineas.js
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)
View phineas.js
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);
}
}
View jneira-4clojure-solution96.clj
;; 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])))))
View jneira-4clojure-solution91.clj
;; 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))))
View jneira-4clojure-solution92.clj
;; 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))
View jneira-4clojure-solution93.clj
;; 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))
View jneira-4clojure-solution84.clj
;; 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))))