Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Clojure/Java Interop "Hello World"

 
Sean Crotty
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best chance I have of getting Clojure into my company is to introduce it via small helper APIs that can be called by regular Java code. For example, I imagine tree/node manipulation would be much simpler to code in Clojure. But it would only be useful if POJ (plain ol' Java) could interact with it to perform CRUD.

Is there a good document that shows how to:
1) Write Clojure code that can be accessed by POJ?
2) Access that code via POJ?

In other words, is there a "Hello World" for Clojure/Java interop.

Thanks!
Sean
 
Sean Corfield
Ranch Hand
Posts: 305
11
Clojure Linux Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a Hello World example here:

http://clojure.org/compilation

To create functions callable from Java you need to remember that the first argument is 'this' (which is implicitly passed to Java methods).

Hopefully someone can point you to more specific Java interop examples but once you have your first function working, it's pretty much plain sailing from then on (in my experience).
 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Show them simple yet important examples like how to iterate over a data structures in Clojure, this will make them cry immediately as they will recall how million times they wrote for loops in Java.
You can also solve the sleeping barber shop algorithm in Clojure to show them how elegant the concurrency programming in Clojure really is.
 
Sean Crotty
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sean Corfield wrote:There's a Hello World example here:

http://clojure.org/compilation

To create functions callable from Java you need to remember that the first argument is 'this' (which is implicitly passed to Java methods).

Hopefully someone can point you to more specific Java interop examples but once you have your first function working, it's pretty much plain sailing from then on (in my experience).

Thanks, Sean. That is a very helpful link!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic