Inheritance from a generic in Swift [duplicate] - swift

In Swift, how do you convert an Array to a Tuple?
The issue came up because I am trying to call a function that takes a variable number of arguments inside a function that takes a variable number of arguments.
// Function 1
func sumOf(numbers: Int...) -> Int {
var sum = 0
for number in numbers {
sum += number
}
return sum
}
// Example Usage
sumOf(2, 5, 1)
// Function 2
func averageOf(numbers: Int...) -> Int {
return sumOf(numbers) / numbers.count
}
This averageOf implementation seemed reasonable to me, but it does not compile. It gives the following error when you try to call sumOf(numbers):
Could not find an overload for '__converstion' that accepts the supplied arguments
Inside averageOf, numbers has the type Int[]. I believe sumOf is expecting a Tuple rather than an Array.
Thus, in Swift, how do you convert an Array to a Tuple?

This has nothing to do with tuples. Anyway, it isn't possible to convert from an array to a tuple in the general case, as the arrays can have any length, and the arity of a tuple must be known at compile time.
However, you can solve your problem by providing overloads:
// This function does the actual work
func sumOf(_ numbers: [Int]) -> Int {
return numbers.reduce(0, +) // functional style with reduce
}
// This overload allows the variadic notation and
// forwards its args to the function above
func sumOf(_ numbers: Int...) -> Int {
return sumOf(numbers)
}
sumOf(2, 5, 1)
func averageOf(_ numbers: Int...) -> Int {
// This calls the first function directly
return sumOf(numbers) / numbers.count
}
averageOf(2, 5, 1)
Maybe there is a better way (e.g., Scala uses a special type ascription to avoid needing the overload; you could write in Scala sumOf(numbers: _*) from within averageOf without defining two functions), but I haven't found it in the docs.

As of Swift 4.1 (in Xcode 9.2), there is no need to overload with sumOf(_ numbers: Int...), the function that forward variadic parameter(s) will IMPLICITLY change it to a single parameter of array of individual parameter(s). E.g. the following code will work without the overloading:
// This function does the actual work
func sumOf(_ numbers: [Int]) -> Int {
return numbers.reduce(0, +) // functional style with reduce
}
func averageOf(_ numbers: Int...) -> Int {
// This calls the first function directly
return sumOf(numbers) / numbers.count
}
print(averageOf(2, 5, 1))
Don't know whether this is a bug of the compiler or not :)

I realize this is an older post, but this came up rather high in the search results and I found a working solution.
You can write the sumOf function to accept an array of integers as the number parameter and overload the sumOf function to accept a variadic input for the numbers parameter which will be passed to the first version as an array. This way the averageOf function can pass its variadic input as array to sumOf.
This does not seem very ideal because you need to overload each function that works like this, but it will work in the way you wanted.
func sumOf(numbers: [Int]) -> Int {
var sum = 0
for number in numbers {
sum += number
}
return sum
}
// Function 1
func sumOf(numbers: Int...) -> Int {
return sumOf(numbers: numbers)
}
// Example Usage
sumOf(2, 5, 1)
// Function 2
func averageOf(numbers: Int...) -> Int {
return sumOf(numbers: numbers) / numbers.count
}

I don't think he needs to use .reduce. Instead just change the parameter definition in his sumOf function. Instead of:
func sumOf(numbers: Int...) -> Int {
var sum = 0
for number in numbers {
sum += number
}
return sum
}
write:
func sumOf(numbers: [Int]) -> Int {
var sum = 0
for number in numbers {
sum += number
}
return sum
}

Related

Compile time error in using variadic parameters with passing function as parameter into another function [duplicate]

In Swift, how do you convert an Array to a Tuple?
The issue came up because I am trying to call a function that takes a variable number of arguments inside a function that takes a variable number of arguments.
// Function 1
func sumOf(numbers: Int...) -> Int {
var sum = 0
for number in numbers {
sum += number
}
return sum
}
// Example Usage
sumOf(2, 5, 1)
// Function 2
func averageOf(numbers: Int...) -> Int {
return sumOf(numbers) / numbers.count
}
This averageOf implementation seemed reasonable to me, but it does not compile. It gives the following error when you try to call sumOf(numbers):
Could not find an overload for '__converstion' that accepts the supplied arguments
Inside averageOf, numbers has the type Int[]. I believe sumOf is expecting a Tuple rather than an Array.
Thus, in Swift, how do you convert an Array to a Tuple?
This has nothing to do with tuples. Anyway, it isn't possible to convert from an array to a tuple in the general case, as the arrays can have any length, and the arity of a tuple must be known at compile time.
However, you can solve your problem by providing overloads:
// This function does the actual work
func sumOf(_ numbers: [Int]) -> Int {
return numbers.reduce(0, +) // functional style with reduce
}
// This overload allows the variadic notation and
// forwards its args to the function above
func sumOf(_ numbers: Int...) -> Int {
return sumOf(numbers)
}
sumOf(2, 5, 1)
func averageOf(_ numbers: Int...) -> Int {
// This calls the first function directly
return sumOf(numbers) / numbers.count
}
averageOf(2, 5, 1)
Maybe there is a better way (e.g., Scala uses a special type ascription to avoid needing the overload; you could write in Scala sumOf(numbers: _*) from within averageOf without defining two functions), but I haven't found it in the docs.
As of Swift 4.1 (in Xcode 9.2), there is no need to overload with sumOf(_ numbers: Int...), the function that forward variadic parameter(s) will IMPLICITLY change it to a single parameter of array of individual parameter(s). E.g. the following code will work without the overloading:
// This function does the actual work
func sumOf(_ numbers: [Int]) -> Int {
return numbers.reduce(0, +) // functional style with reduce
}
func averageOf(_ numbers: Int...) -> Int {
// This calls the first function directly
return sumOf(numbers) / numbers.count
}
print(averageOf(2, 5, 1))
Don't know whether this is a bug of the compiler or not :)
I realize this is an older post, but this came up rather high in the search results and I found a working solution.
You can write the sumOf function to accept an array of integers as the number parameter and overload the sumOf function to accept a variadic input for the numbers parameter which will be passed to the first version as an array. This way the averageOf function can pass its variadic input as array to sumOf.
This does not seem very ideal because you need to overload each function that works like this, but it will work in the way you wanted.
func sumOf(numbers: [Int]) -> Int {
var sum = 0
for number in numbers {
sum += number
}
return sum
}
// Function 1
func sumOf(numbers: Int...) -> Int {
return sumOf(numbers: numbers)
}
// Example Usage
sumOf(2, 5, 1)
// Function 2
func averageOf(numbers: Int...) -> Int {
return sumOf(numbers: numbers) / numbers.count
}
I don't think he needs to use .reduce. Instead just change the parameter definition in his sumOf function. Instead of:
func sumOf(numbers: Int...) -> Int {
var sum = 0
for number in numbers {
sum += number
}
return sum
}
write:
func sumOf(numbers: [Int]) -> Int {
var sum = 0
for number in numbers {
sum += number
}
return sum
}

Swift operators on arrays whose contents match specific protocols, "where" keyword usage

I'm trying to make a Swift operator that applies to all Arrays which contain only Numeric elements, it should look something like this:
prefix operator ∑
prefix func ∑ (array: Array<T>) -> T where T == Numeric {
var sum: T = 0
for item in array {
sum += item
}
return sum
}
but that's a compiler error (error: 'where' clause cannot be attached to a non-generic declaration)
I could, of course, write many functions in the form:
prefix operator ∑
prefix func ∑ (array: Array<Double>) -> Double {
var sum: Double = 0
for item in array {
sum += item
}
return sum
}
which works, but the point of generics is that I shouldn't have to.
This is probably because I don't understand how to use the "where" keyword, but I'm finding it difficult to search for such a (no pun intended) generic word, as I get too many irrelevant results.
Edit:
And I can't write prefix func ∑ (array: Array<Numeric>) -> Numeric { because then Swift can't figure out how to compile sum += item.
You could constraint the generic type to be a Numeric by typing <T: Numeric>, thus the signature of the function would be:
func ∑ <T: Numeric>(array: Array<T>) -> T
I would also suggest to use reduce(_:_:) method to achieve such a task:
prefix operator ∑
prefix func ∑ <T: Numeric>(array: Array<T>) -> T {
return array.reduce(0, +)
}
// calling:
let array = Array(repeating: 1, count: 20)
let summation = ∑array // 20
For more information about generics type constraints, you could review official documentation.
prefix operator ∑
prefix func ∑ <T>(array: Array<T>) -> T where T: Numeric {
var sum: T = 0
for item in array {
sum += item
}
return sum
}

Efficiently aggregating an array of non-optionals without a default value

I need to efficiently aggregate an array of non-optional values, knowing its size, having a way to get its values, but not having a default value.
Following is a rather synthetic example, resembling what I need. It won't compile, but it will give you the idea:
public func array<A>( count: Int, getValue: () -> A ) -> Array<A> {
var array = [A](count: count, repeatedValue: nil as! A)
var i = 0
while (i < count) {
array[i] = getValue()
i++
}
return array
}
Please note that the result of type Array<A?> won't do, I need non-optionals. Also note that the solution must be efficient, it must not do any extra traversals.
You can make a working function from your example code by using
the append() method to add array elements:
public func array<A>(count: Int, #noescape getValue: () -> A) -> [A] {
var array = [A]()
array.reserveCapacity(count)
for _ in 0 ..< count {
array.append(getValue())
}
return array
}
The #noescape
attribute tells the compiler that the passed closure does not outlive
the function call, this allows some performance optimizations,
compare #noescape attribute in Swift 1.2.
But it is easier to use the map() method of CollectionType:
/// Return an `Array` containing the results of mapping `transform`
/// over `self`.
///
/// - Complexity: O(N).
#warn_unused_result
public func map<T>(#noescape transform: (Self.Generator.Element) throws -> T) rethrows -> [T]
In your case:
public func array<A>(count: Int, #noescape getValue: () -> A) -> [A] {
let array = (0 ..< count).map { _ in getValue() }
return array
}
Here map() transforms each integer in the range 0 ... count-1
to an array element. The underscore in the closure indicates that
its argument (the current index) is not used.
I leave it to you to check which method is faster.
Example usage:
let a = array(10) { arc4random_uniform(10) }
print(a) // [3, 7, 9, 4, 2, 3, 1, 5, 9, 7] (Your output may be different :-)

“Functions are a first-class type” in swift?

the little knowledge , I have about first class function is that it supports passing functions as arguments and we can also return them as the values in another function ... I am very new in Swift programming language can anyone please elaborate it with an example.
A very simple example to demonstrate this behaviour:
func functionA() {
println("Hello by functionA")
}
func executeFunction(function: () -> ()) {
function()
}
executeFunction(functionA)
First-Class functions are functions that can return another functions.
For instance:
func operate( operand: String) -> ((Double, Double) -> Double)?{
func add(a: Double, b: Double) -> Double {
return a + b
}
func min(a: Double, b: Double) -> Double{
return a - b
}
func multi(a: Double, b: Double) -> Double {
return a * b
}
func div (a: Double, b: Double) -> Double{
return a / b
}
switch operand{
case "+":
return add
case "-":
return min
case "*":
return multi
case "/":
return div
default:
return nil
}
}
The function operate returns a function that takes two double as its arguments and returns one double.
The usage of this function is:
var function = operate("+")
print(" 3 + 4 = \(function!(3,4))")
function = operate("-")
print(" 3 - 4 = \(function!(3,4))")
function = operate("*")
print(" 3 * 4 = \(function!(3,4))")
function = operate("/")
print(" 3 / 4 = \(function!(3,4))")
When you don't care about the implementation of a function, using First-Class functions to return these functions become beneficials. Plus, sometimes, you are not responsible to develop (or not authorised ) of the functions like add, min. So someone would develop a First-Class function to you that returns these functions and it is your responsibility to continue ....
A function that returns a function while capturing a value from the lexical environment:
A function of an array of Comparables that returns a function of a test predicate that returns a function of a value that returns a Bool if the value is the extreme of the array under test. (Currying)
Any programming language is said to have first-class-functions, when functions are treated like normal variables. That means a function can be passed as parameter to any other function, can be returned by any function and also can be assigned to any variable.
i.e., (Referring apple's examples)
Passing function as parameter
func hasAnyMatches(list: [Int], condition: (Int) -> Bool) -> Bool {
for item in list {
if condition(item) {
return true
}
}
return false
}
Returning function
func makeIncrementer() -> ((Int) -> Int) {
func addOne(number: Int) -> Int {
return 1 + number
}
return addOne
}
Properties of First class function
A function is an instance of the Object type.
You can store the function in a variable.
You can pass the function as a parameter to
another function.
You can return the function from a function.
You can store them in data structures such as hash tables, lists, …
refer https://www.geeksforgeeks.org/first-class-functions-python/

Passing lists from one function to another in Swift

I can't quite understand why I can't past an Int[] from one function to another:
func sumOf(numbers: Int...) -> Int {
var sum = 0
for number in numbers {
sum += number
}
return sum
}
func average(numbers:Int...) -> Double {
var sum = sumOf(numbers)
return Double(sum) / Double(numbers.count)
}
This gives me the following error:
Playground execution failed: error: <REPL>:138:19: error: could not find an overload for '__conversion' that accepts the supplied arguments
var sum = sumOf(numbers)
^~~~~~~~~~~~~~
Thanks for your help!
The numbers: Int... parameter in sumOf is called a variadic parameter. That means you can pass in a variable number of that type of parameter, and everything you pass in is converted to an array of that type for you to use within the function.
Because of that, the numbers parameter inside average is an array, not a group of parameters like sumOf is expecting.
You might want to overload sumOf to accept either one, like this, so your averaging function can call the appropriate version:
func sumOf(numbers: [Int]) -> Int {
var sum = 0
for number in numbers {
sum += number
}
return sum
}
func sumOf(numbers: Int...) -> Int {
return sumOf(numbers)
}
I was able to get it to work by replacing Int... with Int[] in sumOf().
func sumOf(numbers: Int[]) -> Int {
var sum = 0
for number in numbers {
sum += number
}
return sum
}
func average(numbers: Int...) -> Double {
var sum = sumOf(numbers)
return Double(sum) / Double(numbers.count)
}
average(3,4,5)
Your problem is Int... is a variable amount of Ints while Int[] is an array of Int. When sumOf is called it turns the parameters into an Array called numbers. You then try to pass that Array to sumOf, but you had written it to take a variable number of Ints instead of an Array of Ints.