This file contains hidden or 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 $ | |
| "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))) |
This file contains hidden or 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
| #(get (vec %) %2) |
This file contains hidden or 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
| (pop [1 2 3]) | |
| ; => [1 2] | |
| (pop '(1 2 3)) | |
| ; => (2 3) | |
| ;; | |
| (seq (pop (vec '(1 2 3)))) | |
| ; => (1 2) |
This file contains hidden or 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
| (conj `(1 1) 2) | |
| ; => (2 1 1) | |
| (conj [1 1] 2) | |
| ; => [1 1 2] | |
| ;; | |
| (cons 2 `(1 1)) | |
| ; => (2 1 1) |
This file contains hidden or 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
| (def first-n-fib | |
| #(loop [l [] a 0 b 1 c 0] | |
| (if (= c %) | |
| l | |
| (recur | |
| (conj l a) | |
| b | |
| (+ a b) | |
| (+ 1 c))))) |
This file contains hidden or 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
| 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: |
This file contains hidden or 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
| class Range | |
| def crop(value) | |
| value < min ? min : value > max ? max : value | |
| end | |
| end | |
| class Foobar | |
| MINIMUM_VALUE = 10 | |
| MAXIMUM_VALUE = 32 |
This file contains hidden or 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
| 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} |
This file contains hidden or 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
| alias :fn :lambda | |
| module Kernel | |
| def same; fn { |x| x } end | |
| end |
This file contains hidden or 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
| def a | |
| x = 1 | |
| y = 2 | |
| l = lambda { x + y } | |
| y = 3 | |
| l.call | |
| end | |
| puts a # => 4 |