Skip to content

Instantly share code, notes, and snippets.

(def v [[1 2] [1 1] [2 1] [2 4]])
(partition-by #(first %) v)
;; => (([1 2] [1 1]) ([2 1] [2 4]))
(group-by #(first %) v)
;; => {1 [[1 2] [1 1]], 2 [[2 1] [2 4]]}
;; ruby version
;; v.group_by{|v| v[0]}
--langdef=Clojure
--langmap=Clojure:.clj
--regex-clojure=/\([ \t]*create-ns[ \t]+([-[:alnum:]*+!_:\/.?]+)/\1/n,namespace/
--regex-clojure=/\([ \t]*def[ \t]+([-[:alnum:]*+!_:\/.?]+)/\1/d,definition/
--regex-clojure=/\([ \t]*defn[ \t]+([-[:alnum:]*+!_:\/.?]+)/\1/f,function/
--regex-clojure=/\([ \t]*defn-[ \t]+([-[:alnum:]*+!_:\/.?]+)/\1/p,private function/
--regex-clojure=/\([ \t]*defmacro[ \t]+([-[:alnum:]*+!_:\/.?]+)/\1/m,macro/
--regex-clojure=/\([ \t]*definline[ \t]+([-[:alnum:]*+!_:\/.?]+)/\1/i,inline/
--regex-clojure=/\([ \t]*defmulti[ \t]+([-[:alnum:]*+!_:\/.?]+)/\1/a,multimethod definition/
--regex-clojure=/\([ \t]*defmethod[ \t]+([-[:alnum:]*+!_:\/.?]+)/\1/b,multimethod instance/
FallVelocity = fun(Distance) ->
math:sqrt(2 * 9.8 * distance)
end.
(defn fall-velocity
[distance]
(sqrt (* 2 9.8 distance))
;; Clojure vs Erlang on counter recursion
;; Clojure version
(ns count)
(defn down
[x]
(if (zero? x) (prn "blastoff!")
(do
(prn x)
-module(pascal).
-export([triangle/1]).
triangle(Rows) ->
triangle([[0,1,0]],1,Rows).
triangle(List, Count, Rows) when Count >= Rows -> lists:reverse(List);
triangle(List, Count, Rows) ->
[Previous | _] = List,
%% erlang pattern matching and list comprehension vs clojure destructure and hof
-module(shop).
-export([total/1]).
cost(oranges) -> 5;
cost(newspaper) -> 8;
cost(apples) -> 2;
cost(pears) -> 9;
cost(milk) -> 7.
(ns example.errors)
(defn clean-address [params]
"Ensure (params :address) is present"
(if (empty? (params :address))
[nil "Please enter your address"]
[params nil]))
(defn clean-email [params]
"Ensure (params :email) matches *@*.*"
-module(math).
-compile(export_all).
%% erlang version
pow(_X, 0) ->
1;
pow(X, N) ->
X * pow(X, N-1).
%% clojure version
%% clojure version, recursion is all you need
(defn take
"Returns a lazy sequence of the first n items in coll, or all items if
there are fewer than n."
{:added "1.0"
:static true}
[n coll]
(lazy-seq
(when (pos? n)
(when-let [s (seq coll)]