Tag: java

Clojure and Java functional interfaces

Java 8 came out in 2014 and brought along functional interfaces. Functional in general just means that you can treat functions (or methods) as “things” instead of having no proper way to talk about them. In this sense Javascript for example is functional: you can pass around functions all you want. Java’s had Runnable and Callable that are pretty similar in concept.

Then came Java 8 and with it the “mighty arrows.” For some reason Ruby, Javascript and Java all opted to use the same bit of syntax to talk about lambdas (anonymous functions): ->. In Ruby it’s ->(foo) { foo }, in Javascript it’s (foo) -> foo, and surprisingly in Java it’s the same. Run a few rounds with futures and/or streaming stuff and you’ll definitely want to pass such a lambda to forEach for example.


Clojure vs Java varargs

Variable length argument lists (varargs) have been around since Java 5 (so quite a long while), yet I get the impression that many people either don’t know about this feature or their tools don’t support it. I ran into one of them working with Netty from Clojure and it wasn’t trivial at all how to use them through interop.


Using Java signatures in Clojure

A while back I was trying to implement HTTP signatures to use with ActivityPub interactions with Mastodon. In Clojure. There is a go-to library for Clojure when it comes to crypto stuff, but I couldn’t get it to do the specific thing I needed: SHA-256 / RSA signatures. I looked at other options too, but as I’m not familiar with NaCl, that was just a confusing mess of wrappers around Java wrapped around C.

In the end I went with using Java interop to call Bouncy Castle stuff directly. I hate Java and interop in Clojure just feels wrong, but at least I could get it to work. Not to mention if something, Bouncy Castle is maintained. It wasn’t exactly a joyride, but it works. Check out the source if you’re interested (or want to use it). I didn’t make it stand-alone or put it up on Clojars (yet).