Skip to content

Instantly share code, notes, and snippets.

@timruffles
Last active December 2, 2016 10:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timruffles/705903d7b4b50b38be1d8dcbbc6c4ef8 to your computer and use it in GitHub Desktop.
Save timruffles/705903d7b4b50b38be1d8dcbbc6c4ef8 to your computer and use it in GitHub Desktop.
mapValues -> Functor for functions
// (a -> a) -> (a -> b) -> (a -> b)
var mapValue = transform => fn => v => fn(transform(v));
// (string -> string) - so string is a above
var louder = x => x.toUpperCase();
// (string -> Element) - so element is b above
var hi = (m) => ({ type: 'h1', value: m })
// (string -> Element) - so we end up with a (a -> b) again
var titleLoud = mapValue(louder)(h1);
// Element
var hi = titleLoud('hi');
// Composition - pretty similar, (f ་ g)(x) = f(g(x))
var compose = f => g => x => f(g(x)));
-- Functor has only one typeclass method - fmap :: (a -> b) -> f a -> f b
-- the 'f' for function will be the type 'function that takes one argument', which is written like: `((->) r)`, confusingly! Ideally
-- it'd be written like (r ->) but syntax doesn't allow
-- instance Functor ((->) r) where
-- fmap f g = (\x -> f (g x))
addOne x = x + 1
timesTwo x = x * 2
-- 11
main = print $ fmap addOne timesTwo $ 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment