To supply callback function from clojure to java method expecting a lambda with interop

264 views
Skip to first unread message

Nagarajan N

unread,
Sep 19, 2015, 9:20:34 PM9/19/15
to Clojure
I started using clojure since last year. I like the language. It even made me more familiar with java. Before I detested java. Am moderately familiar with clojure way of making interop calls to java libraries. But I have no idea how to interop with a java library implementing java 8 lambdas as method params. I am not expecting any clean way to do this (I got that from this thread https://groups.google.com/forum/#!topic/clojure/WjTtqrLf_FY) . I just need a way (however verbose it is) to call a java method expecting a lambda.
For example a java 8 code like this
 
server.requestHandler(request -> {
  request.response().end("hello world!");
});

how do I call this in clojure ,something like this
 
(.requestHandler
   server 
   (some-magical-function-or-macro
     [request] 
     (doto request 
       (.response) 
       (.end "hello world!!")))) 

I 'd like someone to point me in the right direction for this.

Gary Verhaegen

unread,
Sep 20, 2015, 3:23:50 AM9/20/15
to clo...@googlegroups.com
The magical macro is reify. Ironically, you'll need to know the type of the lambda to use it from Clojure.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nagarajan N

unread,
Sep 20, 2015, 5:12:55 AM9/20/15
to Clojure
I am really sorry. I am unclear about the type. Can you verify what I know is correct
I found an example java code which will consume lambda as an argument .

public class Main {
  interface MyCalcLambda {
    BigInteger run(BigInteger input);
  }
  public static void runCalc(MyCalcLambda calc) {
    // ???
  }
  public static void main(String[] args) {
    runCalc(-> a.multiply(a));
  }
}

So here MyCalcLambda is the type of lambda. Is that correct?. So I can reify "MyCalcLambda" "run" 
method like any other java interface as long as I know the type "MyCalcLambda". Is that right? 

Library am going to use is an opensource library. I just need to write a layer in clojure so that It can be reused.So that shouldn't be big issue(I hope).

On Sunday, 20 September 2015 12:53:50 UTC+5:30, Gary Verhaegen wrote:
The magical macro is reify. Ironically, you'll need to know the type of the lambda to use it from Clojure.

On Sunday, 20 September 2015, Nagarajan N <onet...@gmail.com> wrote:
I started using clojure since last year. I like the language. It even made me more familiar with java. Before I detested java. Am moderately familiar with clojure way of making interop calls to java libraries. But I have no idea how to interop with a java library implementing java 8 lambdas as method params. I am not expecting any clean way to do this (I got that from this thread https://groups.google.com/forum/#!topic/clojure/WjTtqrLf_FY) . I just need a way (however verbose it is) to call a java method expecting a lambda.
For example a java 8 code like this
 
server.requestHandler(request -> {
  request.response().end("hello world!");
});

how do I call this in clojure ,something like this
 
(.requestHandler
   server 
   (some-magical-function-or-macro
     [request] 
     (doto request 
       (.response) 
       (.end "hello world!!")))) 

I 'd like someone to point me in the right direction for this.

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to

For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscribe@googlegroups.com.

ajoberstar

unread,
Sep 20, 2015, 9:48:30 AM9/20/15
to clo...@googlegroups.com
Yes, your understanding is correct. MyCalcLambda is the one you'll want to reify, and it will be just like using reify on any interface.

After that thread you linked to, I did create ike.cljj (https://github.com/ike-tools/ike.cljj) which supports converting Clojure functions to lambdas.

For a lot of use cases, it's probably better to stick with reify, but ike.cljj can cut down on some of the boilerplate.

Andrew Oberstar 

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to

For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.

Nagarajan

unread,
Sep 20, 2015, 9:57:44 AM9/20/15
to clo...@googlegroups.com

Thank you everyone for clearing this up. Since my case is for a specific java library, it's source should give me an idea what to do. Java 8 callback is still possible with clojure. I really like to praise whoever responsible for making decision of java 8 lamdas as interface

You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/QcKQ1PHLfh8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.

Gary Verhaegen

unread,
Sep 21, 2015, 3:30:26 AM9/21/15
to clo...@googlegroups.com
Note that you have to implement the one method of the functional interface. It may not always be called "run", as Java 8 places no restriction on the method name.

Nagarajan

unread,
Sep 21, 2015, 4:12:23 AM9/21/15
to clo...@googlegroups.com

Noted . Thank you very much

Reply all
Reply to author
Forward
0 new messages