Skip to content

Instantly share code, notes, and snippets.

@aavogt
Created February 14, 2015 07:01
Show Gist options
  • Save aavogt/f64db5678181307bebb9 to your computer and use it in GitHub Desktop.
Save aavogt/f64db5678181307bebb9 to your computer and use it in GitHub Desktop.
CanMap
{-# LANGUAGE OverlappingInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}
module Main where
import Prelude hiding (map)
import qualified Data.Text as T
import qualified Data.Vector.Unboxed as U
import Data.Char as C
import Control.Monad (Functor)
class CanMap ca cb a b | ca -> a, cb -> b, cb a -> ca, ca b -> cb where
map :: (a -> b) -> ca -> cb
instance CanMap T.Text T.Text Char Char where
map = T.map
instance Functor f => CanMap (f a) (f b) a b where
map = fmap
instance (U.Unbox a, U.Unbox b) => CanMap (U.Vector a) (U.Vector b) a b where
map = U.map
main = do
let l = [1,2,3] :: [Int]
let m = Just 5
let t = (T.pack "Hello")
v = U.fromList [1,2,3]
print $ map (*2) l
print $ map (*2) m
print $ map C.toUpper t
print $ map (fromIntegral :: Int -> Double) v
return ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment