Skip to content

Add transpose and transpose' to NonEmptyArray #228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 6, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Breaking changes:

New features:
- Added `transpose` to `Array` (#225 by @newlandsvalley and @JordanMartinez)
- Added `transpose` and `transpose' `to `Array.NonEmpty` (#227 by @newlandsvalley and @JordanMartinez)

Bugfixes:

Expand Down
1 change: 1 addition & 0 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"purescript-nonempty": "^7.0.0",
"purescript-partial": "^4.0.0",
"purescript-prelude": "^6.0.0",
"purescript-safe-coerce": "^2.0.0",
"purescript-st": "^6.0.0",
"purescript-tailrec": "^6.0.0",
"purescript-tuples": "^7.0.0",
Expand Down
40 changes: 40 additions & 0 deletions src/Data/Array/NonEmpty.purs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ module Data.Array.NonEmpty
, foldMap1
, fold1
, intercalate
, transpose
, transpose'
, scanl
, scanr

Expand Down Expand Up @@ -133,6 +135,7 @@ import Data.Tuple (Tuple(..))
import Data.Unfoldable (class Unfoldable)
import Data.Unfoldable1 (class Unfoldable1, unfoldr1)
import Partial.Unsafe (unsafePartial)
import Safe.Coerce (coerce)
import Unsafe.Coerce (unsafeCoerce)

-- | Internal - adapt an Array transform to NonEmptyArray
Expand Down Expand Up @@ -361,6 +364,43 @@ fold1 = F.fold1
intercalate :: forall a. Semigroup a => a -> NonEmptyArray a -> a
intercalate = F.intercalate

-- | The 'transpose' function transposes the rows and columns of its argument.
-- | For example,
-- |
-- | ```purescript
-- | transpose
-- | (NonEmptyArray [ NonEmptyArray [1, 2, 3]
-- | , NonEmptyArray [4, 5, 6]
-- | ]) ==
-- | (NonEmptyArray [ NonEmptyArray [1, 4]
-- | , NonEmptyArray [2, 5]
-- | , NonEmptyArray [3, 6]
-- | ])
-- | ```
-- |
-- | If some of the rows are shorter than the following rows, their elements are skipped:
-- |
-- | ```purescript
-- | transpose
-- | (NonEmptyArray [ NonEmptyArray [10, 11]
-- | , NonEmptyArray [20]
-- | , NonEmptyArray [30, 31, 32]
-- | ]) ==
-- | (NomEmptyArray [ NonEmptyArray [10, 20, 30]
-- | , NonEmptyArray [11, 31]
-- | , NonEmptyArray [32]
-- | ])
-- | ```
transpose :: forall a. NonEmptyArray (NonEmptyArray a) -> NonEmptyArray (NonEmptyArray a)
transpose =
(coerce :: (Array (Array a)) -> (NonEmptyArray (NonEmptyArray a)))
<<< A.transpose <<< coerce

-- | `transpose`' is identical to `transpose` other than that the inner arrays are each
-- | a standard `Array` and not a `NonEmptyArray`.
transpose' :: forall a. NonEmptyArray (Array a) -> NonEmptyArray (Array a)
transpose' = unsafeAdapt A.transpose

scanl :: forall a b. (b -> a -> b) -> b -> NonEmptyArray a -> NonEmptyArray b
scanl f x = unsafeAdapt $ A.scanl f x

Expand Down
16 changes: 16 additions & 0 deletions test/Test/Data/Array/NonEmpty.purs
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,22 @@ testNonEmptyArray = do
log "traverse1 should work"
assert $ traverse1 Just (fromArray [1, 2, 3, 4]) == NEA.fromArray [1, 2, 3, 4]

log "transpose swaps rows and columns for a regular two-dimension array"
assert $ NEA.transpose (fromArray [ (fromArray [1,2,3]), (fromArray [4,5,6]), (fromArray [7,8,9])]) ==
(fromArray [ (fromArray [1,4,7]), (fromArray [2,5,8]), (fromArray [3,6,9])])

log "transpose skips elements when rows don't match"
assert $ NEA.transpose (fromArray [ (fromArray [10,11]), (fromArray [20]), (fromArray [30,31,32])]) ==
(fromArray [ (fromArray [10,20,30]), (fromArray [11,31]), (fromArray [32])])

log "transpose' swaps rows and columns for a regular two-dimension array"
assert $ NEA.transpose' (fromArray [[1,2,3], [4,5,6], [7,8,9]]) ==
(fromArray [[1,4,7], [2,5,8], [3,6,9]])

log "transpose' skips elements when rows don't match"
assert $ NEA.transpose' (fromArray [[10,11], [20], [30,31,32]]) ==
(fromArray [[10,20,30], [11,31], [32]])

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can another test be added here for NEA.transpose' (fromArray [[]]) == (fromArray [[]])?

Copy link
Contributor Author

@newlandsvalley newlandsvalley Aug 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this test fails - it produces (fromArray []). Perhaps we need to special-case it? This works:

transpose' :: forall a. NonEmptyArray (Array a) -> NonEmptyArray (Array a)
transpose' (NonEmptyArray [[]]) = NonEmptyArray [[]]
transpose' xs = (unsafeAdapt A.transpose) xs

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I think my example is just wrong. Array has the same behavior in its test: https://github.com/purescript/purescript-arrays/blob/master/test/Test/Data/Array.purs#L280

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the test should be NEA.transpose' (fromArray [[]]) == (fromArray [])

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surely fromArray [] is an empty NonEmptyArray and thus eminently unsafe.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh gosh... 🤦‍♂️ Thanks!

Then the implementation should be

transpose' :: forall a. NonEmptyArray (Array a) -> Maybe (NonEmptyArray (Array a))
transpose' = fromArray <<< A.transpose <<< coerce

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not entirely happy with this. From my perspective, transpose' (fromArray [[]]) is a very odd thing to do anyway and I wouldn't have thought it would be entirely incorrect for it to be an idempotent operation. But the cost of doing what you suggest is to make all the normal cases more awkward by wrapping the result in the Maybe.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awkward or not, it is correct. And if that's the definition, then it's the same as:

fromArray <<< A.transpose <<< NEA.toArray

So, I would argue now that we shouldn't implement that here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll happily do what you suggest if it is the correct definition. Unless I hear otherwise from @garyb today I'll do it this way - I don't feel strongly about it in any case.

(However just to expand my approach a bit, I'd argue that the minimal result for transpose Array (Array a) is [], that for NonEmptyArray (NonEmptyArray a) is [[something]] and that for NonEmptyArray (Array a) is [[]]. There seems to be no equivalent function on ```NonEmptyList```` in the Haskell library. which might indicate correctness.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The result of transposing [[]] being [[]] does sound pretty reasonable to me (in both non-or-empty cases), but I guess if we have evidence from elsewhere that that's not normal, then yeah having transpose' fail with Nothing is probably the best path forward.

odd :: Int -> Boolean
odd n = n `mod` 2 /= zero

Expand Down