Direct arbitrary-arity currying in java
-
preferably without a lot of boilerplate and absolutely retaining type safety.
-
Holy shit you have returned?!?
Also, I'm completely unable to determine what is being asked...
-
@Tsaukpaetra said in Direct arbitrary-arity currying in java:
I'm completely unable to determine what is being asked...
I'm guessing that he's asking for something that the base language doesn't support. He's also asking without the aid of the parts of a sentence that make it a question, or really the parts that make it a complete sentence either.
-
@dkf said in Direct arbitrary-arity currying in java:
@Tsaukpaetra said in Direct arbitrary-arity currying in java:
I'm completely unable to determine what is being asked...
I'm guessing that he's asking for something that the base language doesn't support. He's also asking without the aid of the parts of a sentence that make it a question, or really the parts that make it a complete sentence either.
Classic gribnit!
-
You’re not going to get the neat currying syntax of functional languages where you can just write
g = f 42 0
. Maybe you can build something likestd::bind_front
in C++? Not sure how powerful Java’stemplatesgenerics are to let you do that.
-
@topspin said in Direct arbitrary-arity currying in java:
Maybe you can build something like
std::bind_front
in C++?That'd need some sort of language extension to build the binding glue. They're not too difficult to do (apparently) with the more structured build system architectures that the Java ecosystem supports, but it does add complexity. But if I was going to add anything, it'd first be support for default values for arguments (as a shorthand for writing multiple methods). That'd shorten a lot of code, and the mechanism is obvious (except for needing minor trickery to hold private constants in an interface).
-
Doesn’t Java have lambdas though? That’s more verbose than a currying functor could be, but not dramatically so.
-
But I mean, it's Java. You will have boilerplate.
-
@topspin said in Direct arbitrary-arity currying in java:
Doesn’t Java have lambdas though?
Yep. Since 8.
-
You can't pass functions around in Java, so you'll need an interface for the things you care about. But then you can do it with lambdas fairly neatly.
interface ABC { int abc(A a, B b, C c); }
interface AB { int abc(A a, B b); }ABC abc = (whatever);
// currying expression
AB ab = (a, b) -> ABC.abc(a, b, new C(42));This isn't really what Java's for though, if you want to write functional code against the JVM use something like Scala.
-
@bobjanova The majority of interfaces that you need for basic “looks like” function passing are already there. With them written, you can use either
a -> b
lambda syntax orThing::methodName
syntax to pass things in. It only gets a little more messy when you want to make your own functional interface, and then only really once you get deep into failure handling (because exception signatures).
-
@dkf Yeah, if you're happy with the Function<A,B> family. Looks like you need to get a 3p library to have more than BiFunction, too - at which point imo you're better off creating your own functional interface with a meaningful name.
-
@bobjanova said in Direct arbitrary-arity currying in java:
Yeah, if you're happy with the Function<A,B> family.
It's really about the semantics of what you're doing with the functions. Writing a functional interface is extremely easy (any interface with just one method in it to implement will do) but writing the code to consume that and do something useful can be… more work, possibly a lot more. It depends on what's going on really.
Passing an absolutely arbitrary method isn't so easy, but that's because the semantics of that make it exceptionally difficult to use in any meaningful way.