How to manipulate the closure parameters by their position in Swift.
I've tried in the following way, But, couldn't get the idea behind it, from the documentation.
var additionClosure = { (a : Int , b : Int )-> Int in
return ($0 + $1)
}
var a = 10
var b = 20
println("additionClosure \(additionClosure(a,b))")
Any help would be appreciated..... Well, Thanks in advance.
`The numbered argument format is used for when you don't want to create a named closure.
Example:
import Foundation
func add(a : Int, b : Int) -> Int {
return a + b
}
func test(a : (Int, Int) -> Int) {
println("result: \(a(10,20))")
}
test(add) // Calling test with a named closure
test({$0 + $1}) // Calling test with an anonymous closure
In the first case you define add with two parameters, and give them names a and b. In the second case, you don't even define the name of the closure, just the required functionality using $0 and $1
Related
I want to save a function that takes two parameters as a function that only takes one parameter. I know I learned this with functional programming but I can't remember the methodology name or how to implement it.
Example: a methods like this:
func add (a: Int, b: Int) {
return a + b
}
And you can manipulate and save a new method that let’s say only increments a by 1:
let increment = add(b:1)
print(increment(a: 4))
// prints 5
Can you do this in swift?
It seems you're looking for function currying. This was a part of swift in earlier versions but was removed because it added too much complexity inside the compiler. (Like seen here: https://github.com/apple/swift-evolution/blob/master/proposals/0002-remove-currying.md)
I guess the closest you can get to a curried function is if you do something like this:
func add(_ x: Int) -> (Int) -> Int {
return { y in
y + x
}
}
With this you can say:
let add2 = add(2)
print(add2(3)) // prints 5
You are looking for Default Parameter Values
You can assign default values to the parameters. When calling this function if you pass the value to the parameters that passed value will be used else the default value will be used. To use the default value you can avoid the parameter in the function calling
func add(a: Int = 1, b: Int = 1) -> Int {
return a + b
}
print(add(a: 5, b: 5))//prints 10
print(add(a: 4))//prints 5
print(add(b: 4))//prints 5
print(add())//prints 2
Got it from Here
The Ultimate Closure
Finally, for the ultra parsimonious there is the following, without a byte wasted.
let testEquality9 : (Int, Int) -> Bool = (==)
Functions decalred with the func keyword are just closures with names. == is an example of one such named function. It takes 2 Int arguements, and returns a Bool telling you if they're equal. Its type is (Int, Int) -> Bool
testEquality9 is a closure, with the type (Int, Int) -> Bool. To it, the closure of the == function is assigned.
It can be called like this:
testEquality9(1, 2) // false
testEquality9(1, 1) // true
The key thing to draw from this is that functions are really just closures, so they can be used everywhere closures can be used.
For example, if you wanted to sort an array of Ints, you could use:
let ints = [3, 1, 4, 2]
let sorted = ints.sort{$0 < $1}
The sort(_:) method takes a closure that's of type (Int, Int) -> Bool. Our closure {$0 < $1} takes 2 Int params, and returns a Bool. So it fits that signiture.
However, we can make this code shorter. Because the < operator's function already has type (Int, Int) -> Bool, we can write this:
let sorted = ints.sort(<)
This passes the function (named closure) < in directly, without explicitly making our own closure to wrap around it.
This is not actually a closure, it's the equality operator that compares two integers stored into a variable.
Every operator is defined using a function and that function can be assigned to a variable. There is nothing else to it.
Operator Overloading:
func == (i : Int, j: Int) -> Bool {
return i == j
}
Should be equivalent to that.
As the others said, it's the abbreviation form of:
let testEquality9: (Int, Int) -> Bool = { (a: Int, b: Int) -> Bool in return a == b }
Reading from right to left, it creates a function that compares two Ints and assigns it to the constant testEquality9.
You need to mentally separate the 3 pieces:
The constant name:
let testEquality9
The constant type (it's a function type):
(Int, Int) -> Bool
And the value assigned to the constant:
(==)
OR, the long version:
{ (a: Int, b: Int) -> Bool in return a == b }
Enjoy Swift :)
Swift function parameters not accepted. Missing argument?
Calculate(theA, theB) //error: Missing argument label 'sideB:' in call
func Calculate(sideA: Int, sideB: Int) -> Int {
var ans = sideA + sideB
return ans;
}
You are missing the sideB: in your function call. I didn't want to rewrite your code (since you posted an image) but here's the working function call.
func calcButton(sender: AnyObject) {
let a: Int = 10
let b: Int = 11
calculate(a, sideB: b) //<-- Missing it here
}
func calculate(sideA: Int, sideB: Int) -> Int {
let a = sideA + sideB
return a
}
you might also want to have both variables in the function call so you can do this instead:
func calcButton(sender: AnyObject) {
let a: Int = 10
let b: Int = 11
calculate(sideA: a, sideB: b)
}
func calculate(sideA A: Int, sideB B: Int) -> Int {
return A + B
}
Just an FYI, use tab completion instead of writing out the function. Xcode will let you know all the function variables with placeholders so you can type them in.
you have missed the sideB param name in swift 3 first parameter is optional but second param is Mandatory that _ in there That’s an underscore. It changes the way the method is called. To illustrate this, here’s a very simple function:
func doStuff(thing: String) {
// do stuff with "thing"
}
It’s empty, because its contents don’t matter. Instead, let’s focus on how it’s called. Right now, it’s called like this:
doStuff(thing: "Hello")
You need to write the name of the thing parameter when you call the doStuff() function. This is a feature of Swift, and helps make your code easier to read. Sometimes, though, it doesn’t really make sense to have a name for the first parameter, usually because it’s built into the method name.
When that happens, you use the underscore character like this:
func doStuff(_ thing: String) {
// do stuff with "thing"
}
That means “when I call this function I don’t want to write thing, but inside the function I want to use thing to refer to the value that was passed in.
just learning about closures and nesting functions. Given the nested function below:
func printerFunction() -> (Int) -> () {
var runningTotal = 0
func printInteger(number: Int) {
runningTotal += 10
println("The running total is: \(runningTotal)")
}
return printInteger
}
Why does calling the func itself have an error, but when I assign the func to a constant have no error? Where is printAndReturnIntegerFunc(2) passing the 2 Int as a parameter to have a return value?
printerFunction(2) // error
let printAndReturnIntegerFunc = printerFunction()
printAndReturnIntegerFunc(2) // no error. where is this 2 going??
First of all you are getting error here printerFunction(2) because printerFunction can not take any argument and If you want to give an argument then you can do it like:
func printerFunction(abc: Int) -> (Int) -> (){
}
And this will work fine:
printerFunction(2)
After that you are giving reference of that function to another variable like this:
let printAndReturnIntegerFunc = printerFunction()
which means the type of printAndReturnIntegerFunc is like this:
that means It accept one Int and it will return void so this will work:
printAndReturnIntegerFunc(2)
(1) The function signature of printerFunction is () -> (Int) -> () which means it takes no parameter and returns another function, thats why when you try to call printerFunction(2) with a parameter gives you an Error.
(2) And the signature of the returned function is (Int) -> () which means it takes one parameter of Int and returns Void. So printAndReturnIntegerFunc(2) works
I suspect I am missing something really obviously wrong here, so forgive me if I am being a bit thick.
All the examples I see for closures are for passing a closure to the array map function. I want to write my own function which takes a closure
This is what I am attempting
func closureExample(numberToMultiply : Int, myClosure : (multiply : Int) -> Int) -> Int
{
// This gets a compiler error because it says myClosure is not an Int
// I was expecting this to do was to invoke myClosure and return an Int which
// would get multiplied by the numberToMultiply variable and then returned
return numberToMultiply * myClosure
}
I am completely stumped on what I am doing wrong
Please help!!
Same way you call any function, with ().
return numberToMultiply * myClosure(multiply: anInteger)
A working example:
func closureExample(numberToMultiply : Int, myClosure : (multiply : Int) -> Int) -> Int {
return numberToMultiply * myClosure(multiply: 2)
}
closureExample(10, { num in
return 2*num
}) // 40
You treat a closure parameter just like a function named with the parameter's name. myClosure is the closure that needs to operate on numberToMultiply, so you want:
return myClosure(numberToMultiply)
That will pass numberToMultiply to your closure, and return the return value.