Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing Java's lambda expression with Swift's function type

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?

like image 883
Thor Avatar asked Nov 07 '16 06:11

Thor


People also ask

What does Java's lambda expression allow you to do?

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.

What is the function type of lambda expression?

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.

How lambda expression and functional interfaces are related?

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.

Which can be used instead of lambda expression?

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.


2 Answers

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:

  1. (major) Closures may capture non-constant variables, e. g

    func makeIncrementer(forIncrement amount: Int) -> () -> Int {
        var runningTotal = 0
        return () -> Int {
            runningTotal += amount
            return runningTotal
        }
    }
    
  2. (minor) Closures support shorthand argument names, e.g.

    reversedNames = names.sorted(by: { $0 > $1 } )
    
  3. (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.

like image 182
kgeorgiy Avatar answered Oct 14 '22 15:10

kgeorgiy


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.

like image 20
Sweeper Avatar answered Oct 14 '22 16:10

Sweeper