1

I asked here: https://stackoverflow.com/questions/36385548/how-to-programmatically-construct-a-lambda-in-java-8 but I wanted to know if there are alternatives to construct lambdas in clojure.

For the use case, I'm attempting to wrap the rethinkdb library: http://www.rethinkdb.com/api/java/#map


I may be wrong, but in the rethinkdb driver, somehow the lambdas are compiled into ast syntax that are converted to js and sent to the database. I believe I need to somehow explicitly create a lambda. http://www.rethinkdb.com/blog/lambda-functions/ so this quesion How to implement lambda as a function called "lambda" in Clojure? only shows how to use a function, not a lambda.

Community
  • 1
  • 1
zcaudate
  • 13,998
  • 7
  • 64
  • 124
  • You can always use [`reify`](http://clojure.github.io/clojure/clojure.core-api.html#clojure.core/reify) with the appropriate Java functional interface. Though I feel you should add more details here, it isn’t clear what you mean by ‘constructing a lambda function in Clojure’. – glts Apr 03 '16 at 14:55
  • see update... though happy to be wrong about how the rethinkdb lambdas work – zcaudate Apr 03 '16 at 20:09

1 Answers1

6

Lambdas are just a syntax sugar, like clojure macros. You can NOT use macros in java, or the "lambda syntax" of java in clojure. But you can create in clojure the object that the java lambda syntax creates.

That is, in java the lambda syntax creates an object that implements the interface according to the type in the method. That interface has a single non-default and non-static method.

If you want to construct a "java lambda" in clojure, what you really need to do is create an object implementing that interface.

Functional interface: https://docs.oracle.com/javase/8/docs/api/java/lang/FunctionalInterface.html How you see the lambdas as an argument: How do I define a method which takes a lambda as a parameter in Java 8?

hernan
  • 572
  • 4
  • 10