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

Proposal: make first and last always return 1 element, let take return from end of list #7128

Open
rgwood opened this issue Nov 14, 2022 · 5 comments
Labels
enhancement New feature or request inconsistent-behavior Behavior between different commands or types inconsistent/unexpected polish this problem makes nu feel unpolished

Comments

@rgwood
Copy link
Contributor

rgwood commented Nov 14, 2022

Following some discussion on Discord and in this issue, I would like to propose the following changes:

  1. Remove the rows parameter from first and last so they always return a single item
    1. Makes the commands' return type consistent
    2. Brings Nu's behaviour in line with many other languages
  2. Add negative number support to take so take -3 returns the last 3 elements.
    1. Inspired by Python's negative indexing (and has been suggested a few times by Python users)
    2. Lets us support existing last n functionality without adding a new command

Examples

[0 1 2] | first 2 # will no longer work
[0 1 2] | take 2  # will need to do this instead

[0 1 2] | last 2  # will no longer work
[0 1 2] | take -2 # will need to do this instead

Language Comparison

Functionality First element n-th element Last element First n elements Last n elements
Return Type T T T List<T> List<T>
Python (more_)itertools first nth last take(n) arr[-n:], tail(n)
Javascript (lodash) head() nth last() take(n) takeRight(n)
.NET / C# / LINQ First() ElementAt() Last() Take(n) TakeLast(n), array[^n..]
Rust first() nth() last() take(n) v[v.len()-3..]?
Scala head drop(n-1).next last take(n) .takeRight(n)
Nushell (current) first get n last first n, take n last n
Nushell (proposed) first get n last take n take -n

Next Steps

If this isn't too controversial I can implement it soon.

Additional context and details

No response

@rgwood rgwood added enhancement New feature or request polish this problem makes nu feel unpolished inconsistent-behavior Behavior between different commands or types inconsistent/unexpected labels Nov 14, 2022
@theAkito
Copy link

Three more for the fun of it.

Functionality First element n-th element Last element First n elements Last n elements
Kotlin first() [n] or get(n) last() take(n) takeLast(n)
Nim [0] [n] [^1] or pop [0..n] [^n..^1]
Borg N/A N/A N/A --first n --last n

Lets us support existing last n functionality without adding a new command

If this is guaranteed, all is fine.

@dandavison
Copy link
Contributor

LGTM!

@raccmonteiro
Copy link
Contributor

raccmonteiro commented Nov 14, 2022

this does not look very complex for someone new to rust. I would like to try

@rgwood
Copy link
Contributor Author

rgwood commented Nov 14, 2022

@raccmonteiro Thanks for the offer!

I'm planning on doing this one because it will require a small rewrite of last; to make a long story short last doesn't check types properly and I want to clean that up while I'm in the area.

@raccmonteiro
Copy link
Contributor

yeah no problem :) I will find another issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request inconsistent-behavior Behavior between different commands or types inconsistent/unexpected polish this problem makes nu feel unpolished
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants