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
-
I was wondering if there's a way to consume a sequence and avoid the duplicate iterating we get with
Seq.head
andSeq.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
head
and we must enumerate it one step to gettail
because 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
head
and thetail
so that thetail
is 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#
Seq
library in some way? This cropped in during a discussion here in Discord.---- UPDATE
I played around with this after looking at the
Seq
source 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
snd
applied 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