Skip to content

Instantly share code, notes, and snippets.

@notogawa
Created August 21, 2015 13:13
Show Gist options
  • Save notogawa/314962b5980fb2c79561 to your computer and use it in GitHub Desktop.
Save notogawa/314962b5980fb2c79561 to your computer and use it in GitHub Desktop.
{-# 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