Skip to content
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

Add transpose and transpose' to NonEmptyArray #228

Merged
merged 8 commits into from
Aug 6, 2022

Conversation

newlandsvalley
Copy link
Contributor

Description of the change

Now that we have transpose in Array we can add two variants of it to NonEmptyArray. transpose handles the case where both the inner and outer arrays are non-empty. transpose' handles the case where the inner arrays are typed as a standard Array. The transpose' implementation courtesy of @JordanMartinez .

For a discussion see #227.


Checklist:

  • Added the change to the changelog's "Unreleased" section with a reference to this PR (e.g. "- Made a change (#0000)")
  • Linked any existing issues or proposals that this pull request should close
  • Updated or added relevant documentation
  • Added a test for the contribution (if applicable)

Copy link
Contributor

@JordanMartinez JordanMartinez left a comment

Choose a reason for hiding this comment

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

My comment below also applies to unsafeFromArray2D.

and implement the functions that transform 2D arrays between Array and
NonEmptyArray representations to use coerce.
Copy link
Contributor

@JordanMartinez JordanMartinez left a comment

Choose a reason for hiding this comment

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

I think adding one more test should be done, but otherwise this looks good to go.

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.

so that it returns a Maybe.  This is necessary in order to handle the
edge case of transpose` (NonEmptyArray [ [] ]).
Copy link
Member

@garyb garyb left a comment

Choose a reason for hiding this comment

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

I'll merge and release this later today, writing myself a script to make the process easier, and this will be the test case 😉

@garyb garyb merged commit 43e0284 into purescript:master Aug 6, 2022
@garyb
Copy link
Member

garyb commented Aug 6, 2022

Released as v7.1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants