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
(use '[clojure.core.match :only [match]]) | |
(defn evaluate [env [sym x y]] | |
(match [sym] | |
['Number] x | |
['Add] (+ (evaluate env x) (evaluate env y)) | |
['Multiply] (* (evaluate env x) (evaluate env y)) | |
['Variable] (env x))) | |
(def environment {"a" 3, "b" 4, "c" 5}) |
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
Number = lambda { |env, num| num } | |
Variable = lambda { |env, var| env[var] } | |
Add = lambda { |env, a, b| evaluate(env, a) + evaluate(env, b) } | |
Multiply = lambda { |env, a, b| evaluate(env, a) * evaluate(env, b) } | |
def evaluate(env, exp) | |
op, *args = exp | |
op.(env, *args) | |
end |
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
object Lambda extends App { | |
val number = (num: Int) => (env: Map[Symbol, Int]) => num | |
val variable = (id: Symbol) => (env: Map[Symbol, Int]) => env(id) | |
val add = (a: (Map[Symbol, Int]) => Int, b: (Map[Symbol, Int]) => Int ) => (env: Map[Symbol, Int]) => a(env) + b(env) | |
val multiply = (a: (Map[Symbol, Int]) => Int, b: (Map[Symbol, Int]) => Int ) => (env: Map[Symbol, Int]) => a(env) * b(env) | |
val environment = Map('a -> 1, 'b -> 2, 'c -> 3) | |
val expr_tree = add(variable('a), multiply(number(2), variable('b))) | |
println(expr_tree(environment)) |
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
(defrecord Tree [left elm right]) | |
(defprotocol Monoid | |
(append [a b] ) | |
(identity [a] )) | |
(defprotocol Foldable | |
(foldl [l f i]) | |
(mfirst [l])) | |
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
object P26 extends App { | |
def combinations[T](n: Int, l: List[T]): List[List[T]] = { | |
if(l.size < n) { | |
Nil | |
} else { | |
n match { | |
case 0 => Nil | |
case 1 => l.map{List(_)} | |
case _ => combinations(n-1,l.tail).map{ l.head :: _} ::: combinations(n, l.tail) | |
} |
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 & [f k] | |
(fn [& args] | |
(k (apply f args)))) | |
(defmacro cps [steps] | |
(if (seq steps) | |
`(& ~(first steps) | |
(cps ~(rest steps))) | |
`identity)) |
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
The Objective | |
This language experiment was designed to show how parametric polymorphism and type classes are | |
implemented in different languages. The implementation languages where chosen out of personal | |
preference. I am well aware it's not complete and would love for you to help me with that. If | |
you don't see your favorite language please add it as a comment. I am also not an expert in all | |
of these languages but did try to make them as idiomatic as possible. If you believe there is a | |
better solution please leave a comment with the implementation. | |
The Code |
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 lambda-calc.core) | |
;; THIS MACRO CREATES A LAMBDA THAT IS COMPATABLE WITH LAMBDA AS DEFINED IN | |
;; LAMBDA CALCULUS -> CURRIED NON-RECURSIVE | |
(defmacro f* [args & body] | |
(let [fsym (gensym "fn") | |
alist (for [n (range 1 (count args))] | |
(take n args)) | |
curries (map #(do `(~(vec %) (partial ~fsym ~@%))) alist)] | |
`(fn ~fsym ~@curries (~args ~@body)))) |
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 workbench.enlive.predicate | |
(:require | |
[clojure.zip :as z] | |
[workbench.enlive.engine | |
:refer [compile-step]] | |
[workbench.enlive.select | |
:refer [zip-select]])) | |
;; ## Builtin predicates | |
;; |
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
;; The macros | |
;; most macros here alias names from select.cljs | |
;; they will get used in regular calls | |
(ns lib.select | |
(:require | |
[clojure.string :as str])) | |
;; selector syntax | |
(defn intersection [preds] |
OlderNewer