If I use functional-style method chains for string manipulation, I can not use the usual machinery for getting the first or last few characters: I do not have access to a reference to the current string, so I can not compute indices.
Example:
[some, nasty, objects]
.map( { $0.asHex } )
.joined()
.<first 100>
.uppercased()
+ "..."
for a truncated debug output.
So how to I implement <first 100>
, or do I have to break the chain?
String
will (once again) conform toCollection
, meaning that you'll be able to just get theprefix(_:)
:) – Sleazyindex(atOffset:)
, which would just offset thestartIndex
by a given offset – but these can always be added as extensions). Usually, I don't find sub-collections to be cumbersome – they generally implement the same interface as the parent collection, and can always be easily converted back with an initialiser call (although that being said, they're annoying to work with in extensions due to.... – Sleazyassociatedtype
where
clauses, so the compiler doesn't know various things about them, e.g they should beCollection
s themselves, with elements and indices of the same type as parent collection – but thankfully this will all change soon with SE-0157 & SE-0142). Although, in general, I don't think the Collection API is too bad – it's fundamentally a simple concept, just a.. – SleazyRangeReplaceableCollection
). I'm afraid I can't recommend any resources for you though, I actually don't find the official documentation too bad (it's certainly better than it used to be!). You can always ask a question about a specific part ofCollection
that you feel the documentation has missed – and I'll be happy to (attempt to) answer :) – SleazystartIndex
which I can offset using an integer to get the index I actually want to take an element from. So why on earth don't I get the good oldget(i)
, or maybe even an array-style subscript? And why can I sometimes use only one of open and closed ranges? Maybe my complaints are just incompleteness of the API as opposed to conceptual issues, but so far I have not learned what the advantage of the very verbose constructs are. Scala, for instance, has a very strong Collection API -- but I found it easy to use as well. Anyway, thanks for your elaborate comments! – DillinghamClosedRange
– there's asubscript
overload defined on_Indexable
(whichCollection
internally derives from). Regarding not having a convenientget(i)
-style method – I don't know the full rationale, but I'm aware that the concern is partly performance related, for examplefor i in 0..<string.characters.count { print(string.get(i)) }
feels like it should be a linear time operation... – SleazyString.CharacterView
isn't aRandomAccessCollection
– having to saystr.index(str.startIndex, offsetBy: i)
, while cumbersome, at least makes you more aware of this fact. Although as I said earlier, it's still not as sleek as it could be – I'm not sure if adding aget(_:)
-style method would be the optimal solution, due to the fact that it would confuse theArray
API slightly (do I sayarray.get(i)
orarray[i]
?) – but regardless, it's definitely lacking some shorter way of getting an element at a given index offset. – Sleazy