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

Remove getting needless list length #12998

Merged
merged 4 commits into from
Apr 19, 2022
Merged

Conversation

auduchinok
Copy link
Member

Removes some needless list traversals.

@@ -206,6 +206,8 @@ module internal List =

val internal allEqual: xs:'T list -> bool when 'T: equality

val isSingleItem: xs: 'T list -> bool
Copy link
Contributor

Choose a reason for hiding this comment

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

I think isSingleton is more consistent (List.isEmpty, List.singleton) but no big deal

Copy link
Member

@KevinRansom KevinRansom left a comment

Choose a reason for hiding this comment

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

I disagree with @dsyme here singleton has a particular meaning in programming. isSingleItem is a bit more specific here.

@vzarytovskii
Copy link
Member

I disagree with @dsyme here singleton has a particular meaning in programming. isSingleItem is a bit more specific here.

It also means "list with a single element" in many functional languages, besides, we already use it in lists and arrays List.singleton, Array.singleton.

@smoothdeveloper
Copy link
Contributor

@KevinRansom, the contention is that isSingleton is almost always going to be List.isSingleton.

I think the reasoning of making it the pendant of singleton is good, in case we like to surface the function in FSharp.Core.

@auduchinok
Copy link
Member Author

auduchinok commented Apr 18, 2022

I've been actually choosing between isSingleton and isSingleItem names and decided to go with the latter. I'm OK with changing it to isSingleton, thanks!

@KevinRansom
Copy link
Member

sounds good.

@smoothdeveloper
Copy link
Contributor

@duckmatt / @knocte / @jgardella, wondering if this wouldn't be worth a rule in FSharpLint?

@knocte
Copy link

knocte commented Apr 19, 2022

wondering if this wouldn't be worth a rule in FSharpLint?

Good idea, although it would be much easier to make the violators of the rule respect it if there was an isSingleton function in FSharp's List, shouldn't we add that function first?

@smoothdeveloper
Copy link
Contributor

@dsyme, a bit tangential, I remember some code reviews where we you preferred I change singleton calls into [value] literals, in the compiler code.

Do you feel = [_] could one day, be a legit operator application, and thus, a valid alternative to isSingleton?

It would be cool to have such bindings turn into boolean expression, if there is no ambiguity introduced in the language and such suggestion would makes sense.

@dsyme
Copy link
Contributor

dsyme commented Apr 20, 2022

you preferred I change singleton calls into [value] literals, in the compiler code.

Yes, [expr] is generally preferred.

Do you feel = [_] could one day, be a legit operator application, and thus, a valid alternative to isSingleton?

The general construct would be "test if something matches a pattern and return a bool". F# doesn't have this construct mainly out of simplicity, Probably expr match? pat or something would be the natural way of writing it. But TBH I think F# is simpler without these numerous variations on pattern matching syntax cuteness, and it leads to less code churn between different ways of writing the same thing

@smoothdeveloper
Copy link
Contributor

Thanks for the feedback, I think this, and fsprojects/FSharpLint#543 would push us towards bringing the function in FSharp.Core, so FSharpLint can propose a quick fix to the issue (and embark the diagnostic about wrong code which I was concerned about).

charlesroddie pushed a commit to charlesroddie/fsharp that referenced this pull request May 2, 2023
* Remove getting needless list length

* Cleanup

* Update illib.fs

* List.isSingleItem -> List.isSingleton

Co-authored-by: Don Syme <dsyme@users.noreply.github.com>
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.

6 participants