Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Reusing Lambdas from Java in Clojure

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I am running an application on JRE8 JVM. The application defines a sizeable number of lambdas.
I am also augmenting the functionality with Clojure based reporting functions.

It would be ideal for me to simply reuse the lambdas defined in my Java8 source as closures in
Clojure.

How do I accomplish this? If I cannot do this, I would have do unfortunately redefine the lambdas as
Clojure closures.

Regards,

/Kobe
 
Author
Posts: 4
5
IntelliJ IDE Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure what you mean by "as closures in Clojure". Java lambdas capture their scope lexically just like a Clojure function would, so if you define something in Java it's going to capture the surrounding Java scope. I'm not sure what it would mean for Java code to capture a Clojure scope! You can of course call Java code that uses lambdas from Clojure, or even call lambdas directly (although it may be trickier than normal because of the invisible interfaces involved). Could you expand more on what you would like to do, maybe with a code example?

There is also a discussion about Java 8 lambda interop in Clojure which may be of help to you.
 
Rancher
Posts: 379
22
Mac OS X Monad Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rohan, like Francis I'm not entirely sure what you're trying to do, without seeing some code, but I wonder if this article will help you get a feel for how you might call Java closures from Clojure (by understanding how they compile):

http://www.infoq.com/articles/Java-8-Lambdas-A-Peek-Under-the-Hood
 
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rohan Bryant wrote:
It would be ideal for me to simply reuse the lambdas defined in my Java8 source as closures in Clojure.


Could you please elaborate what you mean by as closures in Clojure?
Have you already tried something and run into problems?
How does your use case differ from basic reuse as illustrated in the following example?



 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic