Is there a "headNtail" like operation available in F#? #12466
Unanswered
The-Futurist
asked this question in
Q&A
Replies: 1 comment 2 replies
-
|
There isn't, and in general FSharp.Core doesn't expose anything working with enumerators. You can code one up pretty much as you've shown |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I was wondering if there's a way to consume a sequence and avoid the duplicate iterating we get with
Seq.headandSeq.tail.If a function is passed a sequence and needs to consume the head element and then (perhaps recursively) consume the "rest" of the sequence we end up iterating the sequence twice (I think), that is we must enumerate the sequence one step to get
headand we must enumerate it one step to gettailbecause in each case we are starting with the same sequence.So we could avoid this if there was a function "headNtail" that accepted a sequence and returned a tuple that contained the
headand thetailso that thetailis automatically available and already "pointing at" the correct position.A crude C# mockup of this is here:
Here we return the partially "consumed" enumerator each time, so I was wondering if there's any merit in such a thing being available in the F#
Seqlibrary in some way? This cropped in during a discussion here in Discord.---- UPDATE
I played around with this after looking at the
Seqsource code, it seems to do part of what I'm speaking of but I don't think its right:---- UPDATE
These two functions kind of work in unison, the first gets the ball rolling then the second can be called recursively where
sndapplied to the returned result each time refers to the remaining sequence...Anyway I'm sure you get the idea, I'm clearly a novice with F# but this idea seems to have merit.
Beta Was this translation helpful? Give feedback.
All reactions