I’m trying to write to a text file using the above method by applying the concept of currying. The first lambda expression receives the file path as parameter and the second one takes in a String value that should be written to the text file. The write method receives a BiConsumer argument because data is written differently depending on the
Tag: functional-programming
How do I map a Comparator to a Comparator using a Function?
I have a comparator of type Comparator<Integer> and a function Function<Pair<Integer,?>,Integer> expressed as Pair::left (that returns an Integer). I need to obtain a comparator of type Comparator<Pair<Integer,?>>. If I wanted to simply map a function Function<T,U> to a resulting function Function<T,V> though a function Function<U,V> I could simply apply andThen() method like this: Is it possible to obtain Comparator<Pair<Integer,?>> in
Why does Java type inference fail to distinguish between Function and Consumer?
Given the following identity functions: I observe the following behaviour in JDK 11 and JDK 17: Why can’t the compiler resolve these symbols by using the return type of the expression? The curly brace versions work fine, and I would have thought they would be the more difficult cases. I understand that you can explicity cast the lambda function, but
How to change keys from a map in scala
How to change the following keys from the map without any variables full functional to I tried with map and was able to change the keys to the same strings but not to different ones. Answer You can apply a map to all the items but focus only on the keys: You can define f to be a function or
how to fix the second parameter of a function with vavr?
Suppose I have a function which takes two parameter. I want to fix the second parameter and makes it a Function1<T1,R>. With Function2.apply(T1 t), I can only fix the first parameter, is there a way to fix the second parameter? Answer There’s no utility function built into vavr that does a partial application of the second argument. The available utility
Rewrite nested ternary operators in functional way
I have the following piece of code: Could you please help me simplify the above by means of the functional programming? I would like to use something similar to Answer Collectors.joining will only insert a delimiter if there are 2 or more elements. In your case, that’s when both are non-empty. If both are empty, the result is “” If
Trying to mimic java functional interface
I am trying to mimic the java’s Function<T,R> by writing below code : when I am writing below code in andThen, it’s working fine (pointing to apply(T t) of interface) and perfectly chaining the other functional implementations But when writing below snippet, it’s falling into recursion, logically that’s correct , but why in snippet A, after.apply(apply(t)) is calling outer apply(T
Reference antecedent Java stream step without breaking the stream pipeline?
I am new to functional programming, and I am trying to get better. Currently, I am experimenting with some code that takes on the following basic form: First a hashmap is used to get the frequency of each number in the list. Next, we sum up the amount of keys which have their values that also exist as keys in
How to reduce a limited stream (e.g. an ArrayList) from right in Java?
I would like to convert a list to a self-implemented linked list using reduce. I managed to do that in a reverse order: Output is I think to obtain result in the correct order, I may have to use something like foldRight or reduceRight in other languages. Does java provide anything similar, given that the stream is guaranteed to be
Java 8 convert for loop and summing of values to stream/lambda?
I have some String inputs that I am looping over that I am trying to convert to java 8 stream/lambdas but was having some issues. My boilerplate code looks like this: I was trying to do something like … but couldn’t get the summ’ing logic down. could anyone point me in the right direction? Answer You need to apply first