Skip to content
Advertisement

Writing Content to a Text File with Currying

JavaScript

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 context (appending to the end of the file, replacing a specific line in the file etc.). It can be called like this:

JavaScript

I know that I’m not supposed to return Void with the Function interface. How can I change the method definition so that it can be called in a similar way? If possible, I would still like to apply currying in this case.

Advertisement

Answer

I know that I’m not supposed to return Void with the Function interface. How can I change the method definition so that it can be called in a similar way?

You can change the return type of write() method to Function<String,Consumer<String>> (a Function producing a Consumer). Since you don’t need to produce any result, it doesn’t make sense to use a Function. When you only need to fire an action, it’s a job for a Consumer implementation.

JavaScript

Usage example (write().apply().accept()):

JavaScript

Sidenote: always use try-with-resources to make sure that your resources are properly closed.

Advertisement