Skip to content

Instantly share code, notes, and snippets.

(defn $
"Amount of money in USD"
([x] ($ x 0))
([x y] (+ x (/ y 100))))
(defn €
"Amount of money in EUR"
([x] (€ x 0))
([x y] ($ (€-to-$ x) y)))
#(get (vec %) %2)
(pop [1 2 3])
; => [1 2]
(pop '(1 2 3))
; => (2 3)
;;
(seq (pop (vec '(1 2 3))))
; => (1 2)
(conj `(1 1) 2)
; => (2 1 1)
(conj [1 1] 2)
; => [1 1 2]
;;
(cons 2 `(1 1))
; => (2 1 1)
@fronx
fronx / first-n-fib.clj
Created August 3, 2011 21:08
different fibonacci implementations
(def first-n-fib
#(loop [l [] a 0 b 1 c 0]
(if (= c %)
l
(recur
(conj l a)
b
(+ a b)
(+ 1 c)))))
def clip(value, min, max)
value < min ? min :
value > max ? max : value
end
puts clip(34, 10, 32) # => 32
puts clip( 8, 10, 32) # => 10
# or:
class Range
def crop(value)
value < min ? min : value > max ? max : value
end
end
class Foobar
MINIMUM_VALUE = 10
MAXIMUM_VALUE = 32
class Hash
def map_values(&block)
merge(self) { |k, v| block.call(v) }
end
end
puts ({:a => 1, :b => 2}.map_values { |x| x * 2})
# => {:a=>2, :b=>4}
alias :fn :lambda
module Kernel
def same; fn { |x| x } end
end
def a
x = 1
y = 2
l = lambda { x + y }
y = 3
l.call
end
puts a # => 4