Author Topic: Functional Library in Java  (Read 1572 times)

Offline TechSY730

  • Core Member Mark V
  • *****
  • Posts: 4,570
Functional Library in Java
« on: December 17, 2010, 11:49:55 pm »
Hey, I'm working on a functional library for Java. If any of you are familiar with the STL in C++, you may know about the algorithm and the functional libraries. I am basically trying to do the same with Java, a type safe way to be able to pass functions objects of certain parameters along. Unlike the reflection, these function objects are lightweight and can be called with little overhead.

There is already an existing library out that does this, but their function objects only support a certain number of parameters per type, and each type does not share a super-interface. This one does, allowing universal extensible functionality.

I am even providing functionality like bind1st, bind2nd, bind, for_each, and many other meta-programming constructs that can use these functions, like in C++.

Now in C++, the behind the scenes template meta-programming stuff gets REALLY convoluted, and its a similar situation here. Worse, unlike C++, Java's generics cannot be "overloaded", and a proper compile time generic signature of an arbitrary number of arguments cannot be automatically generated . This makes the general case very messy, and on a few occasions type safety will not be possible. For instance, the bind() method (the ultra-flexible one) cannot be made in a universally typesafe way in Java.

Now, of course, the end programmer needs not worry about all this stuff. There are much simpler interfaces and abstract classes which take care of the worst for you.

Now why would you want to use this? Many good intermediate to advanced C++ books talk about the <algorithm> and the <tr1/functional> (or possibly <functional> on certain platforms) libraries and why its a good idea to use them. Basically, there are some really clever for_each, et al, implementations that use clever optimizations (like smart loop-unrolling) or even parallelization, and you don't have to know about any of it to take advantage of it. Plus, it can turn an ugly multiline for loop into a single line of code of a few function calls.

If you think I should post this on Google code or something, or if you want any nifty examples about how this can shrink several lines into one, feel free to ask.

Whew, that was a long post.

Offline TechSY730

  • Core Member Mark V
  • *****
  • Posts: 4,570
Re: Functional Library in Java
« Reply #1 on: December 17, 2010, 11:53:09 pm »
EDIT: I just realized I posted about a very high level library I'm making for fun on a video game forum. Maybe I'm targeting the wrong audience. :D

Offline ShadowOTE

  • Hero Member
  • *****
  • Posts: 517
Re: Functional Library in Java
« Reply #2 on: December 20, 2010, 01:30:21 am »
Meh, it's off topic, so you're good. Plus, a bunch of the people here are either programmers, gamers, or other interested individuals. Anyway, it sounds like an interesting project. Good luck!

Offline TechSY730

  • Core Member Mark V
  • *****
  • Posts: 4,570
Re: Functional Library in Java
« Reply #3 on: December 27, 2010, 06:46:04 pm »
I'm probably going to discontinue this project, as I have just found Scala. It is a functional and object-oriented hybrid programming language. All of the things I was trying to implement via "hacks" are formal, type-safe language constructs, and even better, it compiles into .class files so it can be run on a Java VM (as such, there are many similarities between Scala and Java).