4 ms·
Swift: The joy of sequences
- sago 10y ago> meaning you can build functions like this: Is it just me, or is that code example incredibly byzantine? I know I'm only a Swift dilettante, but that struck me as the kind of code we want to actively avoid building. The conceptual grouping, the syntactic grouping and the line layout grouping seem to be carrying three contradictory messages about structure, requiring careful parsing to grok.
- ebbv 10y agoYeah I've used Swift a bit and found it in need of cleaning up. To be quite honest, I've found in the last 5 years a lot of professional developers have lost sight of the maxim that brevity is not better than clarity.
- melling 10y agoSwift is a big step forward over Objective C in many ways: http://ctarda.com/2016/05/clarity-is-more-important-than-brevity-sometimes-you-can-have-both http://ctarda.com/2016/05/clarity-is-more-important-than-bre...
- ebbv 10y agoYes it is. I like it a lot more than Go too. I wasn't slagging Swift, I was talking about the code example in the original article (like the comment I was replying to.)
- fleshweasel 10y agoDo you have any examples you care to share of Swift that needs cleaning up?
- ebbv 10y agoSorry for the poor wording, I was responding to the comment saying that the code example was hard to read. I was agreeing with that. I really like Swift itself.
- BrutallyHonest 10y agoThat's the biggest problem with adoption of academic-style (Haskell, OCaml etc.) functional programming languages. The proponents believe that 'shorter = more readable'. And if they would try to make it more readable it would start looking like Scala and C# (hence their popularity).
- rubiquity 10y agoDon't blame Haskell, OCaml, etc. here. 90% of that syntax that is in the example is Swift specific and I agree it is cluttered and ugly. Swift would do better to stop trying to be like C/C++ and be more like ML (you can tell the language wants to be like ML, but I'm guessing there are internal politics to make it more C/C++-like for adoption reasons. There's even (or there were) a few design documents hinting at intentionally being C like).
- gpderetta 10y agoI don't know, it seems to me that at least a few in the core swift team are well known C++ hackers. For example, in addition to Chris Lattner of LLVM fame, David Abrahams, which I believe is still the lead in charge of Swift standard library, was a founding member of Boost and, relevant to this post, (co-)author of Boost.Iterator. Doug Gregor which is also part of swift core team, is also a prominent ex-Booster. Edit: Doug was of course also the principal author of the failed C++11 Concepts proposal
- jlarocco 10y agoI don't think the OP was "blaming" Haskell and OCaml as much as he was blaming the people trying to add Haskell and features and syntax to Swift and C++. Haskell syntax works great for Haskell, because the whole language has consistent syntax for things like lazy evaluation and lambda functions. The problem is when people try to tack on those features to Swift or C++ and make it work with the ugly syntax from those languages, it ends up making it even uglier. To me, the sample code looks like a bastardized mix of C++ and Haskell, and it's pretty hard to figure out what it's doing, even in the toy code samples.
- austinz 10y agoI'm not particularly enthused with how the code was presented. Here's a marginally more readable version of the first large snippet that still doesn't exceed 77 (or 79) characters in width: https://gist.github.com/austinzheng/dbee353163eb22920d7a567c2366058f https://gist.github.com/austinzheng/dbee353163eb22920d7a567c...
- Razengan 10y agoI like this one better from the actual proposal on GitHub [1]: for view in sequence(first: someView, next: { $0.superview }) { // someView, someView.superview, someView.superview.superview, ... } Seems rather handy. [1]: https://github.com/apple/swift-evolution/blob/master/proposals/0094-sequence-function.md https://github.com/apple/swift-evolution/blob/master/proposa...
- jdmichal 10y agoI'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.
- noelwelsh 10y agoUnfold in Swift? Seems like a good step. It's a bit annoying to use raw unfold but it's easy enough to build utilities on top of it to capture common cases. Get the primitives right and everything else becomes easy.