3 ms·
I'm not very familiar with Swift. If I'm understanding right, the `prefix` function being defined is equivalent to LINQ's `TakeWhile` [0]. If so, the C# version
by jdmichal 10y ago
I'm not very familiar with Swift. If I'm understanding right, the `prefix` function being defined is equivalent to LINQ's `TakeWhile` [0]. If so, the C# version seems much easier to read to me:
public static IEnumerable<T> TakeWhile<T>(
this IEnumerable<T> source,
Func<T, bool> predicate) {
foreach(T value in source) {
if (predicate(value))
yield return value;
else
yield break;
}
}
Of course, there's a bunch of compiler magic going on here that's basically creating a state machine, which seems to be explicit in the Swift version. Are there any similar features in Swift to C#'s `yield`? If not, are any on the road map?
[0] https://msdn.microsoft.com/en-us/library/bb534804%28v=vs.110%29.aspx https://msdn.microsoft.com/en-us/library/bb534804%28v=vs.110...
- fleshweasel 10y agoBeyond being a little more explicit about the generator semantics, it's just noisier. "Self.Iterator.Element" is a lot of noise compared to "T". At the moment, I feel these sequence extensions are just allowing people to write toy programs with slightly less boilerplate, and are accomplishing little as far as improving our ability to produce useful software.