My efforts to port http://www.youtube.com/watch?v=f6kdp27TYZs to Clojure.
func boring(msg string) {
for i := 0; ; i++ {
fmt.Println(msg, i)
time.Sleep(time.Second)
}
(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]} |
My efforts to port http://www.youtube.com/watch?v=f6kdp27TYZs to Clojure.
func boring(msg string) {
for i := 0; ; i++ {
fmt.Println(msg, i)
time.Sleep(time.Second)
}
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)] |