Created
August 21, 2015 13:13
-
-
Save notogawa/314962b5980fb2c79561 to your computer and use it in GitHub Desktop.
This file contains 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
{-# LANGUAGE MultiParamTypeClasses #-} | |
{-# LANGUAGE FlexibleInstances, FlexibleContexts #-} | |
module Flatten (flatten) where | |
import Control.Monad | |
class Flatten m x a where | |
flatten :: x -> m a | |
instance Monad m => Flatten m (m a) a where | |
flatten = id | |
instance (Monad m, Flatten m (m x) a) => Flatten m (m (m x)) a where | |
flatten = flatten . join | |
flattenList1 :: [Int] | |
flattenList1 = flatten [[1::Int]] | |
flattenList2 :: [Int] | |
flattenList2 = flatten [[[2::Int]]] | |
flattenMaybe1 :: Maybe String | |
flattenMaybe1 = flatten (Just (Just "")) | |
flattenMaybe2 :: Maybe String | |
flattenMaybe2 = flatten (Just (Just (Just ""))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment