4 ms·
What do you mean? It's trivial to add extensions to Array, Collection, or Sequence. You can find multiple examples of your additions online right now.
by jshier 7y ago
What do you mean? It's trivial to add extensions to Array, Collection, or Sequence. You can find multiple examples of your additions online right now.
- melling 7y agoI didn’t say it was impossible. I said “add more generic functionality“ That’s why I provided two examples containing different types to illustrate what I meant. I don’t believe you can add a generic function to the array to handle this. In another repo, I’ve used the FloatingPoint protocol with some luck. However, this should also work with Int’s, for instance: extension Array where Element: FloatingPoint { var sum: Element { self.reduce(0, +) } } https://github.com/melling/data-science-from-scratch-swift/blob/master/swift/statistics.swift https://github.com/melling/data-science-from-scratch-swift/b...
- NobodyNada 7y ago> I don’t believe you can add a generic function to the array to handle this. What’s wrong with something like this? extension Array { func take(_ n: Int) -> ArraySlice<Element> { self[0..<n] } } For your other example, you can make the function accept an array of Ints by constraining Element to AdditiveArithmetic instead of FloatingPoint.
- melling 7y agoI think you’re making my case for me. So, I’ll need a separate function for Ints, Strings, Floats... What if I have an Array of my custom Person objects? [1,2,3].take(2) [“a”,”b”,”c”].take(2) [p1,p2,p3].take(2) How much logic are we going to need to repeat for all the types for all the functions?
- sixstringtheory 7y agoFWIW I'm with you on this. I'm still waiting for the rest of the generics manifesto to, you know, manifest: https://github.com/apple/swift/blob/master/docs/GenericsManifesto.md#parameterized-extensions https://github.com/apple/swift/blob/master/docs/GenericsMani... That one and the need for generic associatedTypes trip me up all the time. I think about it every time I see new feature announcements like today's. The opportunity cost of the wasted effort, the new thing that will probably make things harder for everyone. Possibly including those who try to fulfill the generics manifesto.
- Tainnor 7y agothere are holes in Swift's generics system, but this isn't one of them. you don't need separate extensions for int, string, float etc. arrays. the parent post gave you a generic implementation that works with any type of array
- melling 7y agoHmmm. I thought ArraySlice had different semantics. Why wouldn’t it be declared returning [Element]? extension Array { func take(_ n: Int) -> [Element] { self[0..<n] } } Apple provides this warning for ArraySlice: “Long-term storage of ArraySlice instances is discouraged. A slice holds a reference to the entire storage of a larger array, not just to the portion it presents, even after the original array’s lifetime ends. Long-term storage of a slice may therefore prolong the lifetime of elements that are no longer otherwise accessible, which can appear to be memory and object leakage.” https://developer.apple.com/documentation/swift/arrayslice https://developer.apple.com/documentation/swift/arrayslice Would it be acceptable to put these implementations in a Functional library?
- Someone 7y agoYes, it would. Returning an Array would require take to copy the data, slowing down your nice functional code.
- Tainnor 7y agoThis is because Swift is quite low-level when it comes to storage etc., so [0..<n] returns an ArraySlice. However, this has nothing to do with generics.If you're willing to sacrifice performance, you could easily write extension Array { func take(_ n: Int) -> [Element] { Array(self[0..<n]) } }
- rudedogg 7y agoThe example you didn't cover using AdditiveArithmetic is actually covered in the docs here: https://developer.apple.com/documentation/swift/additivearithmetic https://developer.apple.com/documentation/swift/additivearit... extension Sequence where Element: AdditiveArithmetic { func sum() -> Element { return reduce(.zero, +) } }