Most of all, differences of opinion are opportunities for learning.
Terry Tempest William
//Functional
chases(tom, jerry)
// Object Oriented
tom.chases(jerry)
They've a temper, some of them—particularly verbs: they're the proudest—adjectives you can do anything with, but not verbs—however, I can manage the whole lot of them! Impenetrability! That's what I say!
Humpty Dumpty
f(x) = sin(x) / cos(x)
Haskell • CommonLisp • Scheme • ML • Erlang • Elixir • Scala • F# • Clojure • Elm • PureScript
(def count-up
(let [counter (atom 0)]
(fn [] (do (swap! counter inc) @counter))))
(count-up) ;;=> 1
(count-up) ;;=> 2
(count-up) ;;=> 3
object CurryPartial extends App {
def addCurry(x: Int)(y: Int) = x + y
def increment(who: Int) = addCurry(1, who)
}
(defn sum-of-list [cond-fn]
(fn [apply-fn n]
(->> (range n)
(filter cond-fn)
(map apply-fn)
(reduce +))))
(def sum-of-prime-list (sum-of-list is-prime?)) ; => Prime Number List
(sum-of-prime-list square 100000) ; => Sum of square of primes < 100000
A mode of containment φ is referentially transparent if, whenever an occurrence of a singular term t is purely referential in a term or sentence ψ(t), it is purely referential also in the containing term or sentence φ(ψ(t)).
TO understand recursion, you must first understand recursion
John D. Cook
def factorial(n: Int): Int = {
if (n == 0)
return 1
else
return n * factorial(n-1)
}
def fib( n : Int) : Int = n match {
case 0 | 1 => n
case _ => fib1( n-1 ) + fib1( n-2 )
}
def sort(a:Array[Int]): Array[Int] =
if (a.length < 2) a
else {
val pivot = a(a.length / 2)
sort (a filter (pivot>)) ++
(a filter (pivot == )) ++
sort (a filter(pivot <))
}
(defn fact[x]
(if (<= x 1) 1 (* x (fact (- x 1)))))
(defn fib [n]
(if (< n 2) n (+ (fib (- n 2) (- n 1)))))
(defn quick-sort [[pivot & coll]]
(when pivot
(concat
(quick-sort
(filter #(< % pivot) coll))
[pivot]
(quick-sort
(filter #(>= % pivot) coll)))))
@annotation.tailrec
def factorial(number: BigInt,
result: BigInt = 1): BigInt = {
if (number == 0) result
else factorial(number -1,
result * number)
}
@annotation.tailrec
def fib( n : Int) : Int = {
def fib_( n: Int,
a:Int,
b:Int): Int = n match {
case 0 => a
case _ => fib_tail( n-1, b, a+b )
}
return fib_( n, 0, 1)
}
(defn fact [x]
(loop [n x f 1]
(if (= n 1)
f
(recur (dec n) (* f n)))))
(defn fib [x, n]
(if (< (count x) n)
(fib (conj x (+ (last x)
(nth x (- (count x) 2)))) n)
x))
Y = λf.(λg.f(gg))(λg.f(gg))
Choose immutability and see where it takes you
Rich Hickey
It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures
Alan Perlis