开发者

Fun things to do with generators and sequences

开发者 https://www.devze.com 2023-01-24 07:57 出处:网络
Does anyone else feel that the iterators are coming up short when you want to take a part a sequence piece by piece?

Does anyone else feel that the iterators are coming up short when you want to take a part a sequence piece by piece?

Maybe I should just start writing my code in F# (btw anybody knows if F# uses lazy evaluation) but I've found myself wanting a way to pull at a sequence in a very distinct way.

e.g.

// string.Split implemented as a generator, with lazy evaluation
var it = "a,b,c".GetSplit(',').GetEnumerator();
it.MoveNext();
var a = it.Current;
it.MoveNext();
it.MoveNext();
var c = it.Current;

That works, but I don't like it, it is ugly. So can I d开发者_JAVA技巧o this?

var it = "a,b,c".GetSplit(',');
string a;
var c = it.Yield(out a).Skip(1).First();

That's better. But I'm wondering if there's another way of generalizing the same semantic, maybe this is good enough. Usually I'm doing some embedded string parsing, that's when this pops out.

There's also the case where I wish to consume a sequence up to a specific point, then basically, fork it (or clone it, that's better). Like so

var s = "abc";
IEnumerable<string> a;
var b = s.Skip(1).Fork(out a);
var s2 = new string(a.ToArray()); // "bc"
var s3 = new string(b.ToArray()); // "bc"

This last one, might not be that useful at first, I find that it solves backtracking issues rather conveniently.

My question is do we need this? or does it already exist in some manner and I've just missed it?


Sequences basically work OK at what they do, which is to provide a simple interface that yields a stream of values on demand. If you have more complicated demands then you're welcome to use a more powerful interface.

For instance, your string examples look like they could benefit being written as a parser: that is, a function that consumes a sequence of characters from a stream and uses internal state to keep track of where it is in the stream.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号