1038 items | 1 visits
My bookmarks about clojure
Updated on Nov 06, 11
Created on Sep 29, 09
Category: Computers & Internet
URL:
((fn[a] (*a 2)) 1)
is exactly equivalent to
(let [a 1]
(* a 2))
((fn[a] (*a 2)) 1)
is exactly equivalent to
(let [a 1] (* a 2))
user> (apply (fn [x y & more] x) (report-seq "" (iterate inc 0)))
(first 0 )
(rest after 0 )
(first 1 )
(rest after 1 )
(first 2 )
(rest after 2 )
By simply using (defmethod multifn dispatch-val *name* [args] &body), you can give each instance of a multimethod a unique name, which will show up in stack traces and profile results.
safeHead :: [a] -> Maybe a
safeHead [] = Nothing
safeHead (x:_) = Just x
safeTail :: [a] -> Maybe [a]
safeTail [] = Nothing
safeTail (_:xs) = Just xs
safeLast :: [a] -> Maybe a
safeLast [] = Nothing
safeLast (y:[]) = Just y
safeLast (_:xs) = safeLast xs
safeInit :: [a] -> Maybe [a]
safeInit [] = Nothing
safeInit (x:[]) = Just []
safeInit (x:xs) = Just (x : fromJust(safeInit xs))
(defmacro cache
"Replaces the function that the given name refers to
with a memoizing version of it."
([fn-sym] `(cache ~fn-sym ~fn-sym))
([fn-cached fn-uncached]
`(let [var-meta# (meta (var ~fn-uncached))]
(def ~fn-cached (memoize ~fn-uncached))
(reset-meta! (var ~fn-cached) var-meta#)
(var ~fn-cached))))
memoize
on a function.
user=> (use 'clojure.contrib.generic.functor)
nil
user=> (fmap inc {:a 4, :b 3})
{:a 5, :b 4}
Actually it is not limited to maps:
user=> (fmap inc [4 3])
[5 4]
user=> (use 'clojure.contrib.generic.functor)
nil
user=> (fmap inc {:a 4, :b 3})
{:a 5, :b 4}
Actually it is not limited to maps:
user=> (fmap inc [4 3])
[5 4]
1038 items | 1 visits
My bookmarks about clojure
Updated on Nov 06, 11
Created on Sep 29, 09
Category: Computers & Internet
URL: