I'm sure there's a simple function to do this, it should:
> groupEvery [1,2,3,4,5,6] 2
[[1,2],[3,4],[5,6]]
I just don't know off hand and hoogle was no help..
I don't know of any built-in function for that, but it can be implemented easily:
slice :: Int -> [a] -> [[a]]
slice _ [] = []
slice n xs = hs : slice n ts
where (hs, ts) = splitAt n xs
It repeatedly splits its input to first n
elements and the rest. Or, using unfoldr
from Data.List
:
slice n = unfoldr (\xs -> if null xs then Nothing
else Just (splitAt n xs))
Update: Just for fun I tried to make as short version as possible using standard functions. So far I've got
import Data.Functor ((<$))
import Data.List (unfoldr)
import Data.Maybe (listToMaybe)
slice :: Int -> [a] -> [[a]]
slice n = unfoldr (\xs -> splitAt n xs <$ listToMaybe xs)
using <$
from Maybe
's instance of Functor
. Or slightly shorter but even less comprehensible using Applicative
for (->) [a]
:
slice n = unfoldr (liftA2 (<$) (splitAt n) listToMaybe)
slice n = unfoldr ((<$) <$> splitAt n <*> listToMaybe)
The most incomprehensible version of my attempts was
import Control.Monad.Trans
import Control.Monad.Trans.Maybe
slice n = unfoldr (runMaybeT ((MaybeT listToMaybe) >> (lift $ splitAt n)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With