Skip to content

Instantly share code, notes, and snippets.

View matlux's full-sized avatar

Mathieu Gauthron matlux

View GitHub Profile
@matlux
matlux / simple-clojure-scala-interop.scala
Last active January 23, 2017 11:35
Clojure and Scala are both amazing functional languages that have a lot in common. For example their immutable data structures. Both have some amazing libraries. Why not being able to call one from the other? This is a simple Clojure in Scala interop. This is an example showing how to call Clojure function within Scala using standard Scala data …
sealed trait EdnExpr {
def value : Expr
def mdata : Map[Expr,Expr]
override def equals(other : Any) = other match {
case that : EdnExpr => this.value == that.value
case _ => false
}
}
type Expr = Any
@matlux
matlux / church-boolean.clj
Last active August 29, 2015 14:16
Implementation of Church numbers and Church boolean logic in Clojure. This is a nice demonstration of how lambda calculus handles conditional logic with only on primitive (a lambda). No if statement is necessary to implement predicate and branching logic (at least when your language is lazy).
(ns ^{:doc "Playing with Church Boolean Logic."
:author "Mathieu Gauthron"}
net.matlux.church-boolean)
(def ZERO (fn [f] (fn [x] x)))
(def ONE (fn [f] (fn [x] (f x))))
(def TWO (fn [f] (fn [x] (f (f x)))))
(def AND (fn [p] (fn [q] ((p q) p) )))
(def pred (fn [n] (fn [f] (fn [x] (((n (fn [g] (fn [h] (h (g f))))) (fn [u] x) ) (fn [u] u) ) ))))
(ns transducers.core)
(defn my-filter [pred coll]
(reduce #(if (pred %2) (conj %1 %2) %1) [] coll))
(defn my-map [f coll]
(reduce (fn [r x]
(conj r (f x))) [] coll))
(comment
@matlux
matlux / Ants.scala
Last active August 29, 2015 14:06 — forked from mnd999/Ants.scala
package langtonsant
import scala.annotation.tailrec
import scala.collection.immutable.Map
object Ants extends App {
val columnNb = 50
val rowNb = 50
@matlux
matlux / chain-map-keys.clj
Last active January 2, 2016 12:09
Chain map keys macro. This enables you to navigate hierarchically inside the a map of maps (imbricated maps) using string keys as if you'd used keywords.
(ns chain-map-keys.core)
(def mymap {"key1" {"key2" {"key3" "value3"}}})
;(--> mymap "key1" "key2" "key3")
;; =>
;(-> (mymap "javahome") (get "key2") (get "key3"))
(defmacro --> [m firstkey & keys]
(let [a (map #(list 'get %) keys)]
`(-> (~m ~firstkey) ~@a)))