View gist:abdd5f43da4c128612a0d99f93990b77
In which I attempt to answer the question, "How/why are generics useful?"
reverse :: [a] -> [a]
-- reverse can operate on _any_ list: Int, Float, Bool, MyCustomTypeClass.
-- The alternative would be writing a reverse variant for every type:
intListReverse :: [Int] -> [Int]
charListReverse :: [Char] -> [Char]
sort :: Ord => [a] -> [a]
-- sort can operate on _any_ list which is an instance of Ord: Int, Float, Bool, MyCustomTypeClass
View csv-to-hash.rb
SOMETHING = ENV['CSV'].
split(/,/).
each_slice(2).
inject({}) { |memo, (key, value)|
memo[key] = value
memo
}
View compose.rkt
(define (count-zeros lst)
((compose length
(curry filter zero?)
flatten)
lst))
(count-zeros '(0 0 (1 (1 (0 0) 0)) (1 0 0))) ;; 7
View parser.hs
import Control.Applicative
import Data.Char
newtype Parser a = P (String -> [(a, String)])
instance Functor Parser where
fmap g p = P (\inp -> case parse p inp of
[] -> []
[(v, out)] -> [(g v, out)])
View auth.rb
def authenticate_with_http_basic(username, password)
basic = ActionController::HttpAuthentication::Basic
credentials = basic.encode_credentials(username, password)
page.driver.headers = {'Authorization' => credentials}
end
View no_comment.rb
url = widget_url widget,
noindex: false,
api_key: api_key
curl_compatible_url = url.gsub "&", "'&'"
View monad.hs
import Control.Monad
instance Monad [] where
xs >>= f = [y | x <- xs, y <- f x]
let pairs xs ys = xs >>= \x ->
ys >>= \y ->
[(x, y)]
let pairs' xs ys = do x <- xs
View applicative-notes.hs
-- the class of functors which support <*> and pure are called
-- applicative functors or applicatives
-- class Functor f => Applicative f where
-- pure :: a -> f a
-- <*> :: f (a -> b) -> f a -> f b
-- generalise the idea of mapping to functions with multiple arguments
-- associates to the left
View gist:8b1070e58722c2b636f384c8c484227c
- ADD/COPY only bust cache if changes are additive?