2

How can I write the following function using Java 8?

private static final Function<String, Integer> EmpIdToInt = (id) -> {   
    return Integer.valueOf(ACI.generate("emp",id).revealId().intValue());
};

Is there a better way of writing this function in Java 8?

Can anyone help?

4
  • 3
    You're using 8, what's the issue? Commented Apr 21, 2017 at 4:11
  • Is there a better way of writing this function in java 8?? Commented Apr 21, 2017 at 4:12
  • 2
    Integer.valueOf() is unnecessary. Commented Apr 21, 2017 at 6:37
  • 4
    Function<String,Integer> empIdToInt=id->ACI.generate("emp",id).revealId().intValue(); Commented Apr 21, 2017 at 7:55

2 Answers 2

3

Is there a better way of writing this function in java 8??

you're already using the features of java 8 and yes you can make the code shorter by removing
( ) because there is only one param and removing { } because there is only one statement of execution.

you could simplify it like so:

private static final Function<String, Integer> EmpIdToInt = id -> Integer.valueOf(ACI.generate("emp",id).revealId().intValue());
Sign up to request clarification or add additional context in comments.

Comments

2

Note: I take away private static final for printing page.

IF your revealId is an Integer you can simplified to :

Function<String, Integer> EmpIdToInt = id -> ACI.generate("emp",id).revealId();

OR when revealId is not an Integer, but a int will be auto-boxing to an Integer, so you can remove the Integer.valueOf method call:

Function<String, Integer> EmpIdToInt = id -> ACI.generate("emp",id)
                                                .revealId().intValue();

OR you can using a curry method chaining the functions step by step:

Note: class X is where revealId method is declared, and class Y is where intValue method is declared.

// revealId is an Integer
Function<String, Integer> EmpIdToInt = curry(ACI::generate, "emp")
                                      .andThen(X::revealId);
// revealId is not an Integer
Function<String, Integer> EmpIdToInt = curry(ACI::generate, "emp")
                                      .andThen(X::revealId)
                                      .andThen(Y::intValue);

private static <T, A, R> Function<T, R> curry(BiFunction<A, T, R> it, A arg) {
    return other -> it.apply(arg, other);
}

11 Comments

I think it's safe to assume Y is a Number.
I believe it's "partial application". See here for more detail.
@shmosel is correct. Simply said, currying means converting the (a,t)->r function to the a->t->r form. When you apply that (outer) function to a value, you get a function result of the t -> r form, which is partial application. However, I usually call such methods bind, as the concept of binding parameter argument values to a function, to get a function with less parameters, is immediately understandable for average Java developers without the need to know the FP terms.
No, currying would be private static <T, A, R> Function<T, Function<A, R>> curry(BiFunction<A, T, R> it) { return t -> a -> it.apply(a, t); } As said in my previous comment, when you do curry(ACI::generate).apply("emp"), you’re doing partial function application, which your method does in one step.
@holi-java: well, in our projects, we also have a need to bind other parameters than the first, e.g. Function<T,R> bindSecond(BiFunction<T,A,R> func, A arg), which have no FP equivalent, so there is no benefit in naming bindFirst as partialApplication or curryAndApply, just because it happens to match that FP concept. Consistency is more important. Note that in real FP languages, you usually don’t have functions of such names, as currying or partial application happens implicitly. The need for specialized methods translating from one interface to another, is a Java specific issue.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.