Skip to content

Instantly share code, notes, and snippets.

@fnx-ableton
Last active August 29, 2015 14:04
Show Gist options
  • Save fnx-ableton/74380ed44a7989f60db2 to your computer and use it in GitHub Desktop.
Save fnx-ableton/74380ed44a7989f60db2 to your computer and use it in GitHub Desktop.
-- what does this function do?
someFn :: (a -> b) -> [a] -> [b]
-- map!
map :: (a -> b) -> [a] -> [b]
-- not only lists are mappable
data Maybe a = Totes a | Nada
mapMaybe :: a -> b -> Maybe a -> Maybe b
mapMaybe f (Totes x) = Totes (f x)
mapMaybe f Nada = Nada
-- abstraction \o/
class Mappable m where
mmap :: (a -> b) -> m a -> m b
instance Mappable List where
mmap = map
instance Mappable Maybe where
mmap f (Totes x) = Totes (f x)
mmap f Nada = Nada
-- what do we know about this function without looking at implementations?
someFn :: Mappable m, Num a => m a => m a
-- either it returns its input or it applies a numerical operation to all items contained in it
-- e.g.:
someFn stuff = mmap (*2) stuff
@fnx-ableton
Copy link
Author

maybe i could reverse things and start with the desired function type

@fnx-ableton
Copy link
Author

how about adding Joinable?

@fnx-ableton
Copy link
Author

class Joinable j where
  join :: j (j a) -> j a

and then? hm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment