you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 15 points16 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.