If I give a function parameter a default value that is a not a constant (e.g. the result of a function call) is that value just evaluated once (and if so, when?) or is it evaluated every time the function is called?
From The Swift Programming Language, under Language Reference > Declarations > Special Kinds of Parameters:
A parameter with an equals sign (=) and an expression after its type is understood to have a default value of the given expression. The given expression is evaluated when the function is called. If the parameter is omitted when calling the function, the default value is used instead.
You can demonstrate this for yourself by putting the following in a playground:
import Foundation
func foo(i: UInt32 = arc4random()) {
print(i)
}
foo()
foo()
foo()
foo()
foo()
which will print five different random numbers (unless the random number generator generates the same number five times by some astronomically improbable coincidence).
It's not explicit in the docs quoted above, so it's worth noting that when you do specify the argument when calling the function the default expression is not evaluated. You can demonstrate that in a playground too:
func getSomeInt() -> Int {
print("getSomeInt() was called")
return 42
}
func bar(_ i: Int = getSomeInt()) {
print(i)
}
bar(1)
and when that runs you'll see that "getSomeInt() was called" is not printed.
Related
In the Swift documentation Apple says this:
Closures are self-contained blocks of functionality that can be passed
around and used in your code. Closures in Swift are similar to blocks
in C and Objective-C and to lambdas in other programming languages.
Which I thought was the definition of First-class functions
And they also say this:
Closures can capture and store references to any constants and
variables from the context in which they are defined. This is known as
closing over those constants and variables. Swift handles all of the
memory management of capturing for you.
I thought this was the definittion of closures while the other defitintion was for first-class functions, but Apple seems the put them together and call it closure.
Have I misunderstood something? or are Apple calling closures and first-class functions closures?
I've written this example code, and just wanna know if I'm right in the written comments?
// 'a' takes a first class function, which makes 'a' a higher order function
func a(ch: () -> Void){
print("Something")
ch() // 'ch' is a first class function
print("Ended")
}
func closureFunc(){
var num = 2
a({
// access to 'num' is possible by closures
num = num*2
print(num)
})
}
closureFunc()
A First Class Function is a language feature that allows a function that can be assigned to a variable and passed around as if it were any other kind of data. Closures, lambdas and anonymous functions are all "First class functions".
Anonymous Functions, also called Lambda functions, are functions that don't have a name (such as the way a(ch:) has a name). Because they don't have a name, the only way to use them is by storing them in a variable or passing them in as arguments (parameters are essentially variables). Thus all Anonymous functions are also First Class Functions.
Closures are first class functions that capture the state around them. They can be anonymous, or have a name. Named closures are just your regular func functions.
a(ch:) is a higher order function, correct.
ch is a First Class Function (as it's stored in a variable), a Lambda (synonymous with FCF) and possibly also a closure, depending on whether or not its body references any external variables.
In the case of a(ch:) being called with that block, ch is a closure, because it's capturing num.
These notions are orthogonal. They are not directly related; they are two facts about functions in Swift.
Functions are first-class. This means they can be passed around — assigned as variables, passed into function parameters as arguments, and passed out of functions as results.
Functions are closures. This means that, at the point of definition, they capture the environment referred to inside the function body but declared outside the function body.
Here is an example (from a playground):
func multiplierMaker(i:Int) -> (Int) -> (Int) {
func multiplier(ii:Int) -> (Int) {
return ii*i
}
return multiplier
}
let g = multiplierMaker(10)
g(2) // 20
Think about the function multiplier:
The fact that multiplier can be returned as the result of the function multiplierMaker, and assigned to g, and that it has a well-defined type (Int) -> (Int), is because functions are first-class.
The fact that, when 10 is passed into multiplierMaker, the resulting multiplier function multiplies its parameter by 10, even when assigned to g and called later, is because functions are closures.
(Notice that this has nothing to do with anonymous functions. All answers or statements leading you to believe that closures have to do with anonymous functions are wrong. There are no anonymous functions in this example. An anonymous function is a closure, but only because all functions are closures.)
Functions can capture variables in the context they were declared in, and "A combination of a function and an environment of captured variables is called - closure" more
Here is a simple explanation of closures and first class functions in Swift:
Functions are first class objects, they can be assigned to variables, they can be passed as arguments and can be returned
There are two ways of defining functions in Swift: one using the func keyword and using 'closure expressions' - (does not mean closures). e.g.
func f() { print("nothing") }
let a = f // cannot use parentheses here
// or using closure expression:
let a = { () -> void in print("nothing") }
And finally the direct answer to your question: Functions can capture variables in the context they were declared in, and "A combination of a function and an environment of captured variables is called - closure" e.g.
func f() -> ()->()
{
var c = 0 // local var
func innerf()
{
c += 1 // c is now captured
}
return innerf
} // normally c would be released here. but since its used in innerf(), it will stay
let f1 = f
Now we call f1 a closure because it captured a variable.
In swift, is it possible to have a function accepting another function of the same type as itself?
For example, I have this function in python:
lambda f: f(f)
How can I define a function like this in swift? And what will the type of f be?
From you question it sounds as if you're looking for a way to define the self-application combinator (/U combinator). I'm not certain it's possible to implement a U combinator behaviour in Swift, but you could, however, dwell down into the related fix-point combinator (/Y combinator) and recursive closures.
You can achieve the behaviour of the Y combinator by defining a function that takes a function to function higher order function as parameter, say f: (T->U) -> (T->U), and returns the same type of function, i.e. (T->U). Using this approach, your function can take functions such as result from itself as an argument.
The short version is that the Y combinator computes the fixed point of
a functional -- a function that consumes (and in this case, produces)
another function.
The trick is to define recursive functions as the fixed points of
non-recursive functions, and then to write a fixed-point finder -- the
Y combinator -- without using recursion.
From http://matt.might.net/articles/js-church/.
Now, since you return a function, your return will be "nested" in two steps; the outer defining the return of the closure, and the inner the return of the type. The key is the inner, recursive, return; where you call the input (parameter) function itself without explicitly using its name: you make use of the function parameter which is---as described above---constructed as a closure type that can hold the functions itself.
func myCombinator<T,U>(f: (T->U) -> (T->U)) -> (T->U) {
return {
(x: T) -> U in
return f(myCombinator(f))(x)
}
}
Using this function, you can, e.g., calculate factorial of a number without the functions explicitly referring to their own name
func factorialHelper(recursion: Int -> Int)(n: Int) -> Int {
switch n {
case 0: return 1
default: return n * recursion(n-1)
}
}
let factorial = myCombinator(factorialHelper)
print("\(factorial(4))") // 24
For reference on the Y combinator and recursive closures in the context of Swift, see e.g.
https://xiliangchen.wordpress.com/2014/08/04/recursive-closure-and-y-combinator-in-swift/
https://gist.github.com/kongtomorrow/e95bea13162ca0e29d4b
http://rosettacode.org/wiki/Y_combinator#Swift
It's the second reference above from which the example in this answer is taken.
Returning shortly to the U combinator, there is one simple "native" swift case (however, quite useless) that at least simulates the form lambda f: f(f).
Consider a void function, say f, taking an empty tuple type as single function parameter. The empty tuple () is a type (typealias Void refers to type ()) as well as the single value of that type. Since f is void (no explicit return) it implicitly returns an empty tuple () as a value.
Hence---although not really related to the U combinator---you could write something like
func f(_: ()) { }
var lambda_f = f(f())
lambda_f = f(f(f()))
In Swift, how is tuple related to function argument?
In the following two examples the function returns the same type even though one takes a tuple while the other takes two arguments. From the caller standpoint (without peeking at the code), there is no difference whether the function takes a tuple or regular arguments.
Is function argument related to tuple in some ways?
e.g.
func testFunctionUsingTuple()->(Int, String)->Void {
func t(x:(Int, String)) {
print("\(x.0) \(x.1)")
}
return t
}
func testFuncUsingArgs()->(Int, String)->Void {
func t(x:Int, y:String) {
print("\(x) \(y)")
}
return t
}
do {
var t = testFunctionUsingTuple()
t(1, "test")
}
do {
var t = testFuncUsingArgs()
t(1, "test")
}
There is also inconsistencies in behavior when declaring tuple in function argument in a regular function (rather than a returned function):
func funcUsingTuple(x:(Int, String)) {
print("\(x.0) \(x.1)")
}
func funcUsingArgs(x:Int, _ y:String) {
print("\(x) \(y)")
}
// No longer works, need to call funcUsingTuple((1, "test")) instead
funcUsingTuple(1, "test")
funcUsingArgs(1, "test3")
UPDATED:
Chris Lattner's clarification on tuple:
"x.0” where x is a scalar value produces that scalar value, due to odd
behavior involving excessive implicit conversions between scalars and
tuples. This is a bug to be fixed.
In "let x = (y)”, x and y have the same type, because (x) is the
syntax for a parenthesis (i.e., grouping) operator, not a tuple
formation operator. There is no such thing as a single-element
unlabeled tuple value.
In "(foo: 42)” - which is most commonly seen in argument lists -
you’re producing a single element tuple with a label for the element.
The compiler is currently trying hard to eliminate them and demote
them to scalars, but does so inconsistently (which is also a bug).
That said, single-element labeled tuples are a thing.
Every function takes exactly one tuple containing the function's arguments. This includes functions with no arguments which take () - the empty tuple - as its one argument.
Here is how the Swift compiler translates various paren forms into internal representations:
() -> Void
(x) -> x
(x, ...) -> [Tuple x ...]
and, if there was a tuple? function, it would return true on: Void, X, [Tuple x ...].
And here is your proof:
let t0 : () = ()
t0.0 // error
let t1 : (Int) = (100)
t1.0 -> 100
t1.1 // error
let t2 : (Int, Int) = (100, 200)
t2.0 -> 100
t2.1 -> 200
t2.2 // error
[Boldly stated w/o a Swift interpreter accessible at the moment.]
From AirSpeedVelocity
But wait, you ask, what if I pass something other than a tuple in?
Well, I answer (in a deeply philosophical tone), what really is a
variable if not a tuple of one element? Every variable in Swift is a
1-tuple. In fact, every non-tuple variable has a .0 property that is
the value of that variable.4 Open up a playground and try it. So if
you pass in a non-tuple variable into TupleCollectionView, it’s legit
for it to act like a collection of one. If you’re unconvinced, read
that justification again in the voice of someone who sounds really
confident.
Remember the 'philosophical tone' as we've reached the 'I say potato; your say potato' phase.
A function in Swift takes a tuple as parameter, which can contain zero or more values. A parameterless function takes a tuple with no value, a function with one parameter takes a tuple with 1 value, etc.
You can invoke a function by passing parameters individually, or by grouping them into an immutable tuple. For example, all these invocations are equivalent:
do {
let t1 = testFunctionUsingTuple()
let t2 = testFuncUsingArgs()
let params = (1, "tuple test")
t1(params)
t1(2, "test")
t2(params)
t2(3, "test")
}
I am a completely newbie in swift and in functional Programming.
My silly question is the following:
Can a tuple return a set of functions?
Is it a function like that accepted?
someFunction(param: Bool) -> ((Int) -> Int, (Float) ->Float) -> Double)
Thanks for your reply
You can have a tuple of any type, and functions are types, so you can have tuples of functions.
For example, here’s a 2-tuple of two functions, one that takes two Ints and returns an Int, and one that takes two Doubles and returns a Double:
// a 2-tuple of the addition functions for ints and doubles
let tupleOfFunctions: ((Int,Int)->Int, (Double,Double)->Double) = (+,+)
When you say, “can a tuple return a set of functions?”, I’m guessing you mean, “can a function return a tuple of functions?”. And the answer is also yes:
func returnTwoFunctions(param: Bool) -> ((Int,Int)->Int, (Double,Double)->Double) {
// use the argument to drive whether to return some addition
// or some subtraction functions
return param ? (+,+) : (-,-)
}
Your example, though, is a bit scrambled – if you added a func keyword to the front, it’d be a function declaration, but you’ve got mismatched parentheses. What it looks most like is a function (someFunction) that takes one boolean argument, and returns a function that itself takes two functions and returns a double:
func someFunction(param: Bool) -> (Int->Int, Float->Float) -> Double {
let i = param ? 1 : 2
// this declares and returns a function that itself takes two functions,
// and applies them to pre-determined number
return { f, g in Double(g(Float(f(i)))) }
}
// h is now a function that takes two functions and returns a double
let h = someFunction(false)
// ~ and + are two unary functions that operate on ints and floats
h(~,+) // returns -3.0 (which is +(~2) )
Whether this is what you were intending, and whether you have a use-case for this kind of function, I’m not sure...
Suppose I have a function:
func test(closure: (closureArgs: Double ...) -> Double){
//some logic
}
Then later, I call it with:
test({$0 + $1 + $2 + $3})
Is it possible to get the number of closureArgs provided within test? The goal would be to do overloading. For example, test could include some code like:
func test(closure: (closureArgs: Double ...) -> Double){
//somehow get access to number of arguments in closureArgs within the closure that was passed.
}
To clarify - I mean I need to access closureArgs's length INSIDE test but OUTSIDE closure
Is it possible to get the number of closureArgs provided within test?
The Short Answer
No.
Slightly Longer Answer
No, it is not. Here's why:
The function is taking a closure as it's argument that by definition takes a variadic number of arguments. There's no possible way for someone calling the function to designate how many arguments the closure should take ahead of time. For example, if I were to call your function, it might look like this:
test() { (closureArgs: Double...) -> Double in
var n: Double = 0
for i in closureArgs {
n += i
}
return n
}
As you'll notice, I don't define the number of arguments anywhere because the number of arguments is only specified when the closure is called. Then the number can be determined inside, or possibly returned.
Basically, the closure is called within test, so only you the caller know how many arguments it takes. Anyone consuming this function has no control over it.
The only way I can think of is to have the closure return a tuple that contains the number of arguments and a function that gives the answer, like this:
func test(c: (Double...) -> (Int, (Double...) -> Double)) {
let (argCount, function): (Int, (Double...) -> Double) = { c($0) }()
switch argCount {
// do something with function here
}
}
func sum(a: Double...) -> (Int, ((Double...) -> Double)) {
return (a.count, { reduce(a, 0, +) })
}
Something like that might work, but I'm not in a place where I can test it.
Edit: I'm trying to test this now, and it's close, but it doesn't work yet...
Edit: Okay, this works, but maybe not in the way that you want...
func test(c: (Int, Double)) {
let (argCount, result) = c
switch argCount {
case 2:
println("2 args were passed")
println("The result was \(result)")
default:
println("nothing")
}
}
func sum(a: Double...) -> (Int, Double) {
return (a.count, reduce(a, 0, +))
}
Now test(sum(2, 4)) will print that the number of arguments was 2 and the result was 6.
The thing is, when you are passing a closure that already has arguments supplied (and it needs arguments to know how to calculate the count) then you're closure function already evaluates to a result. So have the closure return the number of arguments that it used to calculate the result along with the result. Then in test you can work with both the number of arguments and the result.
I don't know...it's hard to know exactly what you are after because I'm not sure what kind of logic you need to implement once you know the number of arguments...
Edit: Sorry I keep going on and on, but it seems like you know how many arguments are going to be included when you call test, right? I mean, in your example you supplied 4 arguments:
test({$0 + $1 + $2 + $3})
(Although, you really need to rewrite that as:
test({ return $0[0] + $0[1] + $0[2] + $0[3] })
...cuz remember, variadic parameters are passed as an array...)
So if you know how many arguments are going to be in the closure when you pass it to test, just include the number of arguments as a parameter in test, like this:
func test(count: Int, closure: (Double...) -> Double) {
switch count {
case 1:
// do what you want if there is only one argument
case 2:
// do what you want if there are two arguments...etc.
}
}
Now when you call test, you just pass the number of arguments as an Int:
test(4, { return $0[0] + $0[1] + $0[2] + $0[3] })
In other words, you always know how many arguments you are passing to the closure when you pass it. If you want test to know, just include that as a parameter.
The answer goes back to how Swift passes arguments to varargs functions, which is as an array. Generally speaking, the function definition:
func takesVarArgs(args: Double...)
Is syntactic sugar, providing for simpler invocation, for the declaration:
func takesVarArgs(args:[Double])
So, in your case, your closure will be invoked as:
func test(closure: (closureArgs:[Double]) -> Double)) -> Double
Then, within your closure you could get the number of arguments with:
closureArgs.count