In Swift, function types are treated as first class citizens, and can be used anywhere, as any other types. Its syntax is straightforward, and can be easily understood and used.
On the other hand, Java does support function programming via lambda expressions, but in general, I have found it less intuitive, harder to understand, and seemingly more restrictive, compared to Swift's function type.
My question is, since the purpose of Swift's function type and Java's lambda expression is to achieve functional programming, is there anything that Swift's function type can do, but Java's lambda expression can't? Or are they both equally powerful?
Lambda expression is a new and important feature of Java which was included in Java SE 8. It provides a clear and concise way to represent one method interface using an expression. It is very useful in collection library. It helps to iterate, filter and extract data from collection.
A lambda expression is a short block of code which takes in parameters and returns a value. Lambda expressions are similar to methods, but they do not need a name and they can be implemented right in the body of a method.
A lambda expression (lambda) is a short-form replacement for an anonymous class. Lambdas simplify the use of interfaces that declare single abstract methods. Such interfaces are known as functional interfaces. A functional interface can define as many default and static methods as it requires.
How to replace lambda expression with method reference in Java 8. If you are using a lambda expression as an anonymous function but not doing anything with the argument passed, you can replace lambda expression with method reference.
It is incorrect to compare Java's lambdas (implementation) and Swift's functional type (type). But it is possible to compare Java's lambda to Swifts's Closures. And Swift's functional type to Java's functional interfaces.
Closures are more powerful than lambdas:
(major) Closures may capture non-constant variables, e. g
func makeIncrementer(forIncrement amount: Int) -> () -> Int {
var runningTotal = 0
return () -> Int {
runningTotal += amount
return runningTotal
}
}
(minor) Closures support shorthand argument names, e.g.
reversedNames = names.sorted(by: { $0 > $1 } )
(minor) Trailing closures support, e.g.
someFunctionThatTakesAClosure() {
// trailing closure's body goes here
}
From the other hand, functional interfaces are more powerful than functional types. They allows to declare additional methods, e.g. java.util.Comparator that defines a bunch of convenient methods for comparator building, such as reversed
and thenComparing
.
I can only think of one restriction in the Java functional API.
In order to write a lambda expression in Java, you must need an interface with one method. There are already some interfaces made for you in the JDK, but IMHO, their names are less descriptive than the swift's function types. You can't guess immediately that Predicate<T>
accepts a T
parameter and returns a boolean
. On the other hand, T -> Bool
is much cleaner and clearer.
I think other than that, Java's lambda expressions can do anything that swift function types can.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With