[ysh] implement List remove() & List insert() - #2247
Conversation
andychu
left a comment
There was a problem hiding this comment.
This looks excellent, thank you!
|
|
||
| Insert an element into the list at the given. | ||
| Negative indices index backwards from the back of the list. Indices greater than | ||
| than the number of elements in the list insert at the back of the list. |
There was a problem hiding this comment.
Thanks for documenting these edge cases!
|
Ah for the We distinguish between integers in the type system:
I will check out the branch and make it compile Thanks for adjusting the algorithm! |
|
@andychu thanks! I wonder if it would be easy to catch that in the dev build... I should have tried to do a non dev build locally I guess. Let me know if the algo looks alright, not used to implementing these sort of things myself! |
|
Usually I "lean" on the CI -- it catches a lot of problems It is OK to just hack and see what breaks! I made a few changes and now it passes http://op.oilshell.org/uuu/github-jobs/9092/ I also am wondering about the negative overflow case .... I wrote a test to show that Python always inserts it at the beginning e.g. if the list is length 10, then inserting at -11 -12 -13 -14 always puts it at the beginning (which is perhaps not intuitive) Let me compare with what we're doing |
|
Does my last test case not catch that? |
Use assert [expected === actual]
|
Ah OK I think the negative overflow may have been right -- it was hard for me to tell by reading it Although the negative index case seems off by one, at least compared with Python: If you insert at -1, Python does this: The code was making it like I don't think I've ever use this negative behavior, but I tend to follow Python and JS unless there's a reason not to I fixed that, so I think it's right now Thanks for the help! We can use help on other missing methods, as you saw in And keep us updated on Zulip on the YSH-ing :-) |
|
And BTW I don't expect contributors to fix the C++ stuff, because it's essentially a custom language (although we document it here if interested -- https://oils.pub/release/0.26.0/doc/oils-repo/mycpp/README.html . Actually mycpp is no longer that hacky, so I should remove that disclaimer ... ) Doing the Python implementation is always the first step, and that's very helpful! After awhile you get used to it, it's kinda fun to write typed Python, and then have it "magically" be 2x-50x faster |
|
Nice! Thanks for the info and for an excellent new contributor experience :) My bad on the -ve indexing, I think I just assumed without checking pythons behaviour hahah |
Add missing
List->remove()andList->insert()methods.Please do suggest improvements to tests. I'm also assuming that mirroring the behaviour of python's remove and insert methods is alright.