Transpose for NonEmptyList
Masynchin opened this issue · 2 comments
Masynchin commented
It is already transpose for NonEmptyArray, but no for NonEmptyList. One thing is that NonEmptyArray uses unsafe under the hood. I think transpose implementation for NonEmpty can be created without using unsafe, but can't provide any realization. Hope for help!
Masynchin commented
I currently do this code:
transposeN :: forall a. NonEmptyList (NonEmptyList a) -> NonEmptyList (NonEmptyList a)
transposeN xss =
case fromList tails of
Nothing -> singleton heads
Just tss -> zipWith cons' heads tss
where
heads = head <$> xss
tails = transpose' $ tail <$> xss
transpose' :: forall a. NonEmptyList (L.List a) -> L.List (L.List a)
transpose' = L.transpose <<< toListBut haven't test. Maybe it is solution.
Masynchin commented
But haven't test.
I tested it - it doesn't work. Here is new solution which passed tests:
transposeN :: forall a. NonEmptyList (NonEmptyList a) -> NonEmptyList (NonEmptyList a)
transposeN xss =
case sequence $ fromList <$> tails of
Nothing -> singleton heads
Just tss -> cons' heads tss
where
heads = head <$> xss
tails = transpose <<< toList $ tail <$> xss