Skip to content

NonEmpty arrays #127

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 19 commits into from
Mar 10, 2018
Merged

NonEmpty arrays #127

merged 19 commits into from
Mar 10, 2018

Conversation

matthewleon
Copy link
Contributor

work in progress

addresses #120

@matthewleon
Copy link
Contributor Author

reminder to myself to add an explicit export list

Copy link
Contributor

@hdgarrood hdgarrood left a comment

Choose a reason for hiding this comment

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

I guess this is still WIP? Just a couple of thoughts.

show (NonEmptyArray xs) = "(NonEmptyArray " <> show xs <> ")"

derive newtype instance eqNonEmptyArray :: Eq a => Eq (NonEmptyArray a)
derive newtype instance eq1NonEmptyArray :: Eq1 NonEmptyArray
Copy link
Contributor

Choose a reason for hiding this comment

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

Presumably this won't work with 0.11.7? I wonder if it might be nice to make this code work with 0.11.7 first so that we can release it as a minor level change before we release 0.12, and then derive these instances later?

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've been compiling with 0.11.7, and it isn't complaining. Will this bug out at runtime?

Copy link
Contributor

Choose a reason for hiding this comment

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

Given

data X a = X
derive instance eq1X :: Eq1 X

try.purescript.org gives me "Cannot derive a type class instance for Data.Eq.Eq1 X since instances of this type class are not derivable." Is it possible you're using master by accident?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nope. 0.11.7 as installed by Homebrew. Did a clean install, deleted output and bower_components and it's all compiling fine for me.

I don't know what to say! Is there a commit after 0.11.7 that would have changed this behavior? Or is it possible that the deployed version on Try PureScript is wonky?

Copy link
Contributor

@hdgarrood hdgarrood Jan 28, 2018

Choose a reason for hiding this comment

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

This was only merged very recently: purescript/purescript#3207

What is the output of your purs --version? Is it possible you've got a newer purs installed locally to that project which is overriding the global one? Also can you link me to the homebrew formula you're using? I've checked locally with my 0.11.7 and I get the same thing; "instances of this type class are not derivable".

Copy link
Member

Choose a reason for hiding this comment

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

@hdgarrood this is a derive newtype instance, so I think it's fine.

Copy link
Contributor

Choose a reason for hiding this comment

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

lmao of course, I'm being silly. thanks

derive newtype instance altNonEmptyArray :: Alt NonEmptyArray

-- | Internal - adapt an Array transform to NonEmptyArray
adapt :: forall a b. (Array a -> Array b) -> NonEmptyArray a -> NonEmptyArray b
Copy link
Contributor

Choose a reason for hiding this comment

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

I realise this is internal, but even so I'm a little uneasy about defining this function with this type, as it's actually partial; if the function you provide can return an empty array you're in trouble. I think it might be better to use unsafeCoerce in individual implementations within this module; it's then easier (imo) to verify what's going on and that the coercion is justifiable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. Thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For the moment, I'm going to make a small concession on this and just rename these to unsafeAdapt. I can always go back and do the unsafeCoerce option later. That said, I do think that unsafeAdapt, along with docs, should tell anyone hacking on the library what to look for / be aware of.

@matthewleon
Copy link
Contributor Author

@hdgarrood thanks for the input. It is indeed very much WIP, but I'm happy to fix things as I'm working.

@garyb
Copy link
Member

garyb commented Mar 2, 2018

Is this good for "final" review now? 🙂

@matthewleon
Copy link
Contributor Author

Sadly I'm not confident yet that it is. I'll give it a look over the next couple of days and try to fix up any key, missing things.

@garyb
Copy link
Member

garyb commented Mar 2, 2018

No worries, I just wondered if it was waiting on one of us to approve now or if it was something you still considered WIP 🙂

@matthewleon
Copy link
Contributor Author

No worries, I just wondered if it was waiting on one of us to approve now or if it was something you still considered WIP 🙂

Yes, WIP for now, I'm just taking way too long. Time to wrap it up.

@garyb
Copy link
Member

garyb commented Mar 2, 2018

Look who you're talking to 😄 no pressure meant at all.

@matthewleon matthewleon force-pushed the nonempty branch 3 times, most recently from 3008e76 to d1366cb Compare March 9, 2018 03:04
@matthewleon
Copy link
Contributor Author

This is ready for review.

@garyb
Copy link
Member

garyb commented Mar 10, 2018

Looks like the build is failing due to JS formatting pickyness 😉... I'll review the changes proper now though.

@matthewleon
Copy link
Contributor Author

matthewleon commented Mar 10, 2018 via email

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.

Just some minor stuff, but this looks great.

-- Note that this is unsafe: if the transform returns an empty array, this can
-- explode at runtime.
unsafeAdapt'' :: forall a b c d. (a -> b -> Array c -> Array d) -> a -> b -> NonEmptyArray c -> NonEmptyArray d
unsafeAdapt'' f = unsafeAdapt' <<< f
Copy link
Member

Choose a reason for hiding this comment

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

I'd probably drop the prime'd varieties of this, we can just compose with unsafeAdapt where it's used, or unsafeAdapt <<< map f for the cases that need the double-prime'd version (map here working out as compose again, via the Functor instance for functions).

Copy link
Member

Choose a reason for hiding this comment

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

Same goes for the adaptAny variants.

singleton = NonEmptyArray <<< A.singleton

range :: Int -> Int -> Maybe (NonEmptyArray Int)
range x y = fromArray $ A.range x y
Copy link
Member

Choose a reason for hiding this comment

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

Maybe like with replicate this should always produce at least 1 value? Actually, isn't that basically guaranteed anyway? Unless "reverse-ranges" don't work, but I don't see why they couldn't (although obviously that'd be more of a change as we'd want it to behave consistently with normal arrays).

kill `unsafeAdapt'`, `unsafeAdapt''`, `adaptAny'`, `adaptAny''`
@matthewleon
Copy link
Contributor Author

@garyb thanks for the review. This should be ready for another round now.

@garyb garyb merged commit c87fdee into purescript:master Mar 10, 2018
@justinwoo justinwoo mentioned this pull request Mar 18, 2018
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