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

Clarify differences between .head and .first #2961

Closed
AlexDaniel opened this issue Jun 22, 2019 · 5 comments
Closed

Clarify differences between .head and .first #2961

AlexDaniel opened this issue Jun 22, 2019 · 5 comments
Assignees
Labels
docs Documentation issue (primary issue type) RFE Request for enhancement. Try to use alternative wording that explain the concept more clearly

Comments

@AlexDaniel
Copy link
Member

Sometimes I use .first when I mean .head, just because I confuse them often. Example:

<AlexDaniel> c: HEAD my @a = <foo bar baz>; my Seq $s = @a.Seq; say $s.first; @a[0] = 20; say $s.first

<lizmat> AlexDaniel: not sure why you use .first instead of .head

It made me wonder if I'm not the only one, and indeed:

Moreover, people often use .grep().first too (when they can just use first instead):

I think there's something wrong when a specifically dedicated feature is not being used. That being said, I don't see any downside in using .first instead of .head

@AlexDaniel
Copy link
Member Author

@jnthn @JJ doc issue or language issue? :)

@jnthn
Copy link
Contributor

jnthn commented Jun 22, 2019

If you just want the first thing in the list, I don't think there's anything wrong with using first instead of head: their behavior without an argument is the same.

The reason the two exist is because head is about taking a number of elements from the head of the list (and if you don't say how many, we assume 1), whereas first is about finding the first matching thing in the list (and if you don't care what is matched, then the first element will do). If your task is "get the first element in this sequence/list", they're equally good at it, reaching the same outcome, just from a different perspective.

Personally, I feel first reads more naturally than head in many cases, so would make the opposite choice than (I'm assuming) @lizmat does when either work. If you're using tail in the same breath it'd be more fitting to use head/tail rather than first/tail, but that's an issue of art rather than science.

Thus, from a language point of view, I don't see a problem, and from a documentation point of view, I don't think it's worth spilling much ink trying to advocate one over the other, especially when I don't think there's any consensus on which to advocate. :-)

I'd rather any more words spent in the docs go on pointing out - if not already done so - that .first(...) will serve the role of .grep(...).first. That said, seeing this (thanks to laziness, quite minor) mistake doesn't surprise me; I've made the exact same myself in another language before becoming aware that its first could also take a matcher. It's something a linter could maybe point out, though.

@AlexDaniel
Copy link
Member Author

@JJ, feel free to do whatever you want here. Maybe it'll make sense to open a new ticket in the doc repo (so that we still have the decision in this repo in case it comes up again).

@JJ
Copy link
Contributor

JJ commented Aug 20, 2019

I can move it to the doc repo. Maybe it's the best thing. Changing or deprecating a feature is a big thing, but we can just document it better (maybe as a trap) so that they are known and used properly.

@JJ JJ transferred this issue from Raku/problem-solving Aug 20, 2019
@JJ JJ added the RFE Request for enhancement. Try to use alternative wording that explain the concept more clearly label Aug 21, 2019
@coke coke added the docs Documentation issue (primary issue type) label Dec 1, 2020
@JJ JJ changed the title .head and .first Clarify differences between .head and .first Apr 2, 2021
@coke
Copy link
Collaborator

coke commented Dec 11, 2022

I agree with @jnthn, closing ticket.

@coke coke closed this as completed Dec 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation issue (primary issue type) RFE Request for enhancement. Try to use alternative wording that explain the concept more clearly
Projects
None yet
Development

No branches or pull requests

4 participants