purescript/purescript-lists

Transpose for NonEmptyList

Masynchin opened this issue · 2 comments

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!

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 <<< toList

But haven't test. Maybe it is solution.

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