Last active
December 2, 2016 10:43
-
-
Save timruffles/705903d7b4b50b38be1d8dcbbc6c4ef8 to your computer and use it in GitHub Desktop.
mapValues -> Functor for functions
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
// (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))); |
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
-- 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