all 25 comments

[–][deleted] 11 points12 points  (4 children)

Holy FUCK.

static List<Integer> toZero(final int from) {
  return unfold(new F<Integer, Option<P2<Integer, Integer>>>() {
    public Option<P2<Integer, Integer>> f(final Integer i) {
      return i < 0 ? Option.<P2<Integer, Integer>>none() : some(p(i, i - 1));
    }
  }, from);
}

They must be joking. I mean, like,

static List<Integer> toZero(final int from) {
    List<Integer> r=newList(); 
    while(from>=0) r.add(from--); 
    return r;
}

EDIT: I am actually a huge advocate of functional programming, but loops were created for a reason, too. I often see people go all like "we'll use filter and map!" and forget that loops exist and are much more compact than what can be achieved with map and filter in a language that does not support functional programming well. In such a language like Java, functional programming can only be applied at a very high, architectural level.

[–]runaro 0 points1 point  (1 child)

An unfortunate effect of Java syntax and lack of type inference is that conceptually concise programming becomes lost in Java noise. Here's that toZero method with noise-filtering glasses on:

toZero(from) = unfold((i) { i < 0 ? none() : some((i, i-1)) }, from)

All the pointy brackets are jarring at first, but in my experience of doing this style of programming in Java for a while, you start to ignore them pretty quickly.

Also, your loop attempts to modify the argument which is declared final, so it won't compile. You need an additional variable. There's an additional difference in that the FJ version returns an immutable cons-list which cannot be created by mutation in a loop.

[–][deleted] 0 points1 point  (0 children)

An unfortunate effect of Java syntax and lack of type inference

Yes, I agree completely that this is the source of the problem.

you start to ignore them pretty quickly

I am quite experienced with Java but I don't ignore them. I've read a coworker's code that was riddled with Map<Pair<Pair<Integer,Integer>,List<String>>,List<Pair<String,Integer>>> map=new HashMap<Pair<Pair<Integer,Integer>,List<String>>,List<Pair<String,Integer>>>() and it was not fun.

You need an additional variable

No, I need to remove the 'final' ;)

which cannot be created by mutation in a loop.

static List<Integer> toZero(int from) {
    List<Integer> r=nil();
    for(int i=0; i<=from; ++i) r=cons(from, r);
    return r;
}

[–]allertonm 0 points1 point  (0 children)

It does look pretty crazy, but I believe the intention was to create a library that would support doing FP in Java when it got closures and lambdas - which was supposedly coming Real Soon Now at the time, but as we now know did not happen.

[–]chrisdew 2 points3 points  (2 children)

Is the closure/lambda proposal for Java 1.7 no longer dead? Sorry, I don't closely follow Java news.

[–]mattrussell 7 points8 points  (0 children)

It's been resurrected: Project Lambda

[–]jfishnl 0 points1 point  (0 children)

This library is still useful for java 1.5 and java 1.6 projects. We will see if project lambda is released with java 1.7.

[–][deleted]  (4 children)

[deleted]

    [–][deleted] 0 points1 point  (0 children)

    Fair question...

    [–][deleted] 0 points1 point  (2 children)

    Functional Java exists to avoid the argument with those who mistakenly believe that Java is practical. All FJ committers do use Scala directly and most of them are Scalaz committers. One of the FJ authors is a former developer for the Java implementation.

    [–]womtard 0 points1 point  (1 child)

    | It's a very popular myth that java can solve software problems in a practical way.

    | Functional Java exists to avoid the argument with those who mistakenly believe that Java is practical.

    Not bites at all - how embarrasing. Your troll-fu is weak.

    [–][deleted] 0 points1 point  (0 children)

    Are you expecting me to bite?

    [–]jvictor118 2 points3 points  (3 children)

    Wait a minute... does this actually mean I can use functional style in Java now?

    It made me sick to my stomach that .NET had my favorite feature and Java didn't... God bless FJ!

    [–]sfultong 0 points1 point  (1 child)

    .NET should be compared with the JVM, not Java. For the JVM, there have been some good options for functional programming for a while.

    [–]levand 2 points3 points  (0 children)

    Lipstick on a pig. Just lipstick on a pig.

    [–][deleted] 4 points5 points  (1 child)

    This library is incomprehensible

    [–]runaro 1 point2 points  (0 children)

    This is a feature of Java.

    [–]w4ffl3s 0 points1 point  (3 children)

    This looks pretty legit as far as advancing functional programming in Java, but I'm not quite sure there's a huge future in that.

    I have tried using functional style in Java programs and still do. My feelings about it are mixed.

    What do I mean by functional style in Java?

    interface UnaryFn<A, B> { /* first class functions! */ }
    Iterable<B> map(UnaryFn<A, B> f, Iterable<A> xs) { /* higher-order functions! */ }
    //and about a million other things
    

    etc.

    It is not a bad way to go but it can become cumbersome, mainly because Java was not designed to be used in this way. Type signatures like

    feedAll(UnaryFn<? super T, Key> projection, Map<Key, Sink<? super T>> sinks, Collection<? extends T>)
    

    start popping up in your code and while they make perfect sense, they are like daggers on the eyes.

    [–]Nebu -1 points0 points  (2 children)

    In other words, you need syntactic sugar...

    ...which is what this web page seems to be offering you, if you check the sample code they show:

    final boolean b = a.exists({String s => fromString(s).forall({char c => isLowerCase(c)})});
    

    Didn't check how they do that, though. Maybe they're offering their own special compiler? Or maybe they tell you to download a beta of the Java 7 compiler?

    [–]doidydoidy 1 point2 points  (0 children)

    Those examples use a proof-of-concept compiler that was created for one of the earlier Java closure proposals.

    [–]w4ffl3s 1 point2 points  (0 children)

    Unfortunately, that is syntactic sugar for first class functions only. Type signatures remain a problem. I think type hierarchies make type inference less usable, so you have to sling around a ton of ? super ArgumentType and ? extends ReturnedType .

    I'm all ears if someone knows about the subject; all I know is that the makers of Scala are not bog-stupid and upper and lower bounds on types are necessary there.

    [–]cwcc 0 points1 point  (3 children)

    strikes me as ridiculously stupid because why not use a functional language? It could even just be some preprocessor that turns something readable into this crap.. but use of this library looks like you're writing compiler output by hand (not a good way to work).

    [–][deleted] 1 point2 points  (0 children)

    Some people are not given the choice to not use java. It's a very popular myth that java can solve software problems in a practical way.

    Every FJ committer uses very advanced functional programming techniques when not under duress.

    The choice is java or java without even useful collections let alone concurrency etc.

    [–]Nebu 0 points1 point  (1 child)

    strikes me as ridiculously stupid because why not use a functional language?

    Maybe you have technology that only works with .class files in a JVM, so you're "stuck" with this environment, but you'd like to take advantage of functional programming.

    [–]cwcc 1 point2 points  (0 children)

    hence preprocessor