Issue
What is the difference between Swift Closures and Python Lambda expressions?
I often see websites describe Swift closures as something similar to Python lambda expressions. They serve similar purposes and even the Swift Documentation for closures states that
"Closures in Swift are similar to blocks in C and Objective-C and to lambdas in other programming languages."
Python Lambda Expressions
sum = lambda a, b : a + b
Swift Closures
var sum = { (a: Int, b: Int) -> Int in
return a + b
}
These two codes serve essentially the same purpose and I wanted to know how they differ in terms of the language.
Javascript closures on the other hand seem to serve a completely different purpose.
function greeting() {
let message = 'Hi';
function sayHi() {
console.log(message);
}
return sayHi;
}
let hi = greeting();
hi();
To my understanding, Swift closures are similar to Python lambdas but are completely different from Javascript closures. Swift closures are implementations of first-class functions whereas Javascript closures just allow access to variables outside of the parameter list.
I'm confused if there is any connection between Javascript closures and Swift closures because they have the same name.
Solution
A closure is a function that is defined in a way that allows it to carry values defined from another context, usually one of a "higher" scope.
The example you give above in Java is one. In Python, a lambda expression is not a closure. In Python, we would create a closure much like the Java example in your post. Here's an example of a Python closure:
def greet_boss(name):
def greet(person):
if person == name:
return "Yes sir!"
return f"Hello {name}"
return greet
greet_boss
encloses greet
and returns a function. Thus, you can do something like this: greet_boss("Obama")("Biden")
and get "Hello Biden." But if you do greet_boss("Obama")("Obama")
you get "Yes sir!".
Swift closures are also like that.
In Python, a lambda expression is an anonymous function. Lambda expressions define a callable, which works like a function, but it doesn't have a name. In Python, it is preferred only to use lambda expressions as parameters to higher order functions, like map
and filter
and other functions that take a callable as a parameter. Otherwise, you should probably just use a one line def func...
.
In functional programming languages functions are "first class citizens." This means that they can be passed around just like data (integers, strings, arrays, etc.) Not all languages treat functions this way. Some languages that are not strictly functional (e.g., Python) still have first class functions. In the above examples, functions are returned by other functions: the return functions are closures. You can also pass functions as parameter values when they are first class. Example:
def return_certain_times(list_of_times, time_filter):
out = []
for time in list_of_times:
if time_filter(time):
out.append(time)
return out
If you have a list of days of the week, you can do something like return_certain_time(my_times, lambda x: x == "Saturday')
and this would return a list of all the times that matched "Saturday". (This in not a great example of a function you would want, but it illustrates the concept pretty well.)
Answered By - philosofool Answer Checked By - David Marino (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.