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

Added List.sort(comp) to List module #802

Merged
merged 14 commits into from Dec 3, 2020
Merged

Conversation

mathewmariani
Copy link
Contributor

Added sort(comp) function to List using an in-place quicksort implementation.

Added `sort(comp)` function to List using an in-place quick sort implementation.
@CohenArthur
Copy link
Contributor

Your doc example shows it, but it would be nice to have some documentation on the expected behavior of comp(). For example, saying it must take two arguments and return a bool, etc etc

added default comp funcion `List.sort()`.
added more documentation describing `comp`.
@mathewmariani
Copy link
Contributor Author

Your doc example shows it, but it would be nice to have some documentation on the expected behavior of comp(). For example, saying it must take two arguments and return a bool, etc etc

Thanks for pointing that out. I just updated the docs and added a default comp functions

@mhermier
Copy link
Contributor

From my experience at using the language, I would recommend to change sort() to be a uni-line. Stub functions are only meant to provide default values or simply computed values, they should behave exactly like their parent and also return the same thing. While technically equivalent in your case, making it already there, express intent more, avoid possible future bugs and late fixing.

So as a rule of thumb; if you write a one liner, it is highly likely you want to return the computed value, so use the real one liner expression (that implicitly returns the computed value). I know, it is not really intuitive or does not scale producing changes, but the language is like this...

@mathewmariani
Copy link
Contributor Author

mathewmariani commented Aug 20, 2020

From my experience at using the language, I would recommend to change sort() to be a uni-line. Stub functions are only meant to provide default values or simply computed values, they should behave exactly like their parent and also return the same thing. While technically equivalent in your case, making it already there, express intent more, avoid possible future bugs and late fixing.

So as a rule of thumb; if you write a one liner, it is highly likely you want to return the computed value, so use the real one liner expression (that implicitly returns the computed value). I know, it is not really intuitive or does not scale producing changes, but the language is like this...

Just to clarify you mean a uni-line (stub function) like so sort() { sort {|a, b| a < b} } or is there a Wren style that im missing?

@mhermier
Copy link
Contributor

mhermier commented Aug 20, 2020 via email

Copy link
Contributor

@bjorn bjorn left a comment

Choose a reason for hiding this comment

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

Some comments based on code review, I didn't test it.

doc/site/modules/core/list.markdown Outdated Show resolved Hide resolved
src/vm/wren_core.wren Outdated Show resolved Hide resolved
test/core/list/sort.wren Show resolved Hide resolved
test/core/list/sort.wren Show resolved Hide resolved
src/vm/wren_core.wren Outdated Show resolved Hide resolved
@mhermier
Copy link
Contributor

Does the tests pass now ?

@mathewmariani
Copy link
Contributor Author

mathewmariani commented Aug 25, 2020

Does the tests pass now ?

Not with the stack corruption issue

@mhermier
Copy link
Contributor

I think the test should be a bit more atomic to be more interesting.

Added `sort(comp)` function to List using an in-place quick sort implementation.
added default comp funcion `List.sort()`.
added more documentation describing `comp`.
@mathewmariani
Copy link
Contributor Author

I rebased from main and can confirm the tests are passing

@bjorn
Copy link
Contributor

bjorn commented Sep 22, 2020

@mathewmariani It seems you did a rebase followed by a merge. Please get rid of the merge for force-push just the rebased version of your branch.

@mathewmariani
Copy link
Contributor Author

@mathewmariani It seems you did a rebase followed by a merge. Please get rid of the merge for force-push just the rebased version of your branch.

I believe that's it

@mathewmariani
Copy link
Contributor Author

Should I resolve all the comments, or leave that up to the commenters?

@bjorn
Copy link
Contributor

bjorn commented Sep 27, 2020

@mathewmariani I see most of my comments marked as "Outdated" and I see no way to resolve them (neither the ones that haven't been marked outdated), so I guess that's on you.

@mathewmariani
Copy link
Contributor Author

Anything else needed for this to be merged?

@ChayimFriedman2
Copy link
Contributor

Nice work. Three things, however:

  1. I think it's preferred to implement this in C. Wren prefers to implement the core in C (speed).
  2. You implemented a quick sort. Do we need a stable sort instead?
  3. Should we add a non in-place sort, too? (Maybe a method in Sequence). It can be just:
    class Sequence {
      // ...
      sort() { sort {|a, b| a < b } }
      sort(comparer) { toList.sort(comparer) }
    }

@avivbeeri
Copy link
Contributor

I don't think it's semantically appropriate to have a Sequence.sort method, because a sequence could in theory be infinite in length (and they can be lazily evaluated), so a sorting operation would be impossible or intractable.

@ruby0x1
Copy link
Member

ruby0x1 commented Oct 11, 2020

Also a sort function in c runs into reentrancy (specifically, it's not possible when a user supplied compare function is involved)

@ChayimFriedman2
Copy link
Contributor

@ruby0x1 I think the reentrancy is about to be solved, am I wrong?

@avivbeeri I know that, be that still holds in Python for example, and it does have a sorted() function that operates on iterables.

@mathewmariani
Copy link
Contributor Author

@ChayimFriedman2 I implemented Quick Sort since it's the algorithm used in Lua's table.sort
@ruby0x1 is there anything left that I should be adding for this MR?

@ruby0x1
Copy link
Member

ruby0x1 commented Oct 13, 2020

I'll look closely soon, busy with some other Wren stuff locally.

src/vm/wren_core.wren Outdated Show resolved Hide resolved
src/vm/wren_core.wren Outdated Show resolved Hide resolved
@mhermier
Copy link
Contributor

mhermier commented Oct 26, 2020 via email

@ChayimFriedman2
Copy link
Contributor

I think start and end would be better.

@mathewmariani
Copy link
Contributor Author

@ChayimFriedman2 @mhermier I'm leaning towards i and j since that's what you find in most books. I also like more compact code.

@ruby0x1
Copy link
Member

ruby0x1 commented Oct 28, 2020

@mathewmariani code is for humans, not machines, legibility > compactness. Especially for wren, which strives to help people learn and understand, making the code clearer is part of that goal. In my code I used low high but start and end is also clear enough. I would prefer not to use single letter variables here.

changed variables names from `a`, `b` to `low`, `high` to increase readability
Updated the documentation for `list.sort()` to better explain the boolean result and the nature of the compare function
@ruby0x1
Copy link
Member

ruby0x1 commented Oct 29, 2020

Thanks @mathewmariani , I'll probably tweak some things after merging, but looks good from here.

@ruby0x1 ruby0x1 merged commit 3d5e68f into wren-lang:main Dec 3, 2020
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.

None yet

7 participants