You're looking at Less Wrong's discussion board. This includes all posts, including those that haven't been promoted to the front page yet. For more information, see About Less Wrong.

Viliam_Bur comments on Open thread, Jan. 12 - Jan. 18, 2015 - Less Wrong Discussion

6 Post author: Gondolinian 12 January 2015 12:39AM

You are viewing a comment permalink. View the original post to see all comments and the full post content.

Comments (155)

You are viewing a single comment's thread. Show more comments above.

Comment author: Viliam_Bur 16 January 2015 11:50:08AM *  0 points [-]

Java programmers are usually familiar with procedural programming, not functional. The older ones are probably former C/C++ programmers, so they mostly write C/C++ code using Java syntax. That probably includes most textbook authors.

Nothing in Java prevents you from having intermediate states, and composing the functions. You just have to specify the data type for each intermediate state, which may require creating a new class (which is a lot of typing), or typing something like Pair<Map<Integer, Integer>, List<Integer>>, so yeah, there are inconveniences.

As a crazy creative solution, I could imagine wrapping the "class with the five or six functions" into multiple interfaces. Something like this:

Old version:

class Something {
void step1(T1 param1) { ... }
void step2(T2 param2) { ... }
void step3(T3 param3) { ... }
R getResult() { return ...; }
}

New version:

interface AfterStep2 {
R step3(T3 param3);
}
interface AfterStep1 {
AfterStep2 step2(T2 param2);
}
class Something implements AfterStep1, AfterStep2 {
AfterStep1 step1(T1 param1) { ...; return this; }
AfterStep2 step2(T2 param2) { ...; return this; }
R step3(T3 param3) { ...; return ...; }
}

This would force users to write things like:

R result = something.step1(x1).step2(x2).step3(x3);

I also admit my colleagues would kill me after doing this, and the jury would probably free them.