Java to play with Function

This article is an article of a friend who also appeared in I wrote a higher-order function in Go language and played with it Swift playing with closure This is a homage to .com / _makanai / items / ca313c8e117da78154fa). It's nothing more than fun (as in the original article), so if you're looking for a useful implementation, browser back is recommended.

(Note) Please note that there may be strange parts because it is written with midnight tension m (_ _) m

Main subject

Since the return value of hoge1 ~ 4 is Function, more arguments can be passed.

//Takes an Integer and returns an Integer
Function<Integer, Integer> hoge1 = value -> value;
hoge1.apply(1); // return 1
//Takes an Integer and returns a function that takes an Integer and returns an Integer
Function<Integer, Function<Integer, Integer>> hoge2 = value -> (a -> value + a);
hoge2.apply(1).apply(2);    // return 3
///Takes an Integer, takes an Integer, takes an Integer and returns a function that returns an Integer
Function<Integer, Function<Integer, Function<Integer, Integer>>> hoge3 = value -> (a -> (b -> value + a + b));
hoge3.apply(1).apply(2).apply(4);   // return 7
//Takes an Integer, takes an Integer, takes an Integer, takes an Integer, and returns a function that returns an Integer.
Function<Integer, Function<Integer, Function<Integer, Function<Integer, Integer>>>> hoge4 = value -> (a -> (b -> (c -> value + a + b + c)));
hoge4.apply(1).apply(2).apply(4).apply(8);  // return 15

Since the argument of hoge5 ~ 7 is closure, it is necessary to pass the process in advance. (Hoge5 ~ 7 used the return value of hoge1 ~ 4 because the argument is a function)

//Takes an Integer and returns a function that takes an Integer and takes an Integer and takes an Integer and returns a function that takes an Integer and returns an Integer
Function<Function<Integer, Integer>, Function<Integer, Function<Integer, Function<Integer, Integer>>>> hoge5 = hoge -> (a -> (b -> (c -> hoge.apply(a + b + c))));

hoge5.apply(hoge1).apply(1).apply(2).apply(4);  // return 7
hoge5.apply(hoge2.apply(1)).apply(2).apply(4).apply(8); // return 15
hoge5.apply(hoge3.apply(1).apply(2)).apply(4).apply(8).apply(16);   // return 31
hoge5.apply(hoge4.apply(1).apply(2).apply(4)).apply(8).apply(16).apply(32); // return 63
//Takes an Integer and takes an Integer and returns an Integer Takes a function that takes an Integer and takes an Integer and returns an Integer
Function<Function<Integer, Function<Integer, Integer>>, Function<Integer, Function<Integer, Function<Integer, Integer>>>> hoge6 = hoge -> (a -> (b -> (c -> hoge.apply(a).apply(b + c))));

hoge6.apply(hoge2).apply(1).apply(2).apply(4);  // return 7
hoge6.apply(hoge3.apply(1)).apply(2).apply(4).apply(8); // return 15
hoge6.apply(hoge4.apply(1).apply(2)).apply(4).apply(8).apply(16);   // return 31
//Takes an Integer and takes an Integer Takes a function that takes an Integer and returns an Integer, takes an Integer and takes an Integer and returns a function that takes an Integer and returns an Integer
Function<Function<Integer, Function<Integer, Function<Integer, Integer>>>, Function<Integer, Function<Integer, Function<Integer, Integer>>>> hoge7 = hoge -> (a -> (b -> (c -> hoge.apply(a).apply(b).apply(c))));

hoge7.apply(hoge3).apply(1).apply(2).apply(4);  // return 7
hoge7.apply(hoge4.apply(1)).apply(2).apply(4).apply(8); // return 15

Of course, you can have the function that is the return value as it is. I'm passing a number, but it's not actually calculated

hoge7.apply(hoge3); // return Function
hoge7.apply(hoge3).apply(1);    // return Function
hoge7.apply(hoge3).apply(1).apply(2);   // return Function

Turn the loop and add everything ... As you can see, the calculation that used the for statement before Java8! It's getting sleepy ... w

int sum = IntStream.rangeClosed(1, 100).sum();    // return 5050

Take T and T and return T Take a closure, take T and take T and return T (See you tomorrow ... ww)


Turn the loop to get the maximum value (let's do something with stream processing ...)

Finally

Thank you to those who read everything properly. (I'm exhausted at the end, so I'll do it again next time ...) Be careful not to embed the above shit code that is difficult for others to read in the actual product.

It's hard to get used to functional programming, but it's fun when you get to know it. For the time being, I'm going to do functional programming with Java and read a great H book.

P.S Where is the best line break for Java lambda expressions ... (...?)

Recommended Posts

Java to play with Function
Connect to DB with Java
Connect to MySQL 8 with Java
Play with Java function nodes that can use Java with Node-RED
Java to learn with ramen [Part 1]
[Java] Points to note with Arrays.asList ()
Dare to challenge Kaggle with Java (1)
I tried to interact with Java
Java, arrays to start with beginners
Play with Markdown in Java flexmark-java
I want to make a function with kotlin and java!
How to compile Java with VsCode & Ant
[Java] How to use the hasNext function
[Java] How to compare with equals method
How to use BootStrap with Play Framework
Introduction to algorithms with java --Search (depth-first search)
Try to implement login function with Spring-Boot
Authentication function with Play Framework [Registration and authentication]
[Java] Introduction to Java
Post to Slack from Play Framework 2.8 (Java)
How to implement TextInputLayout with validation function
[Processing × Java] How to use the function
Introduction to java
Easy to trip with Java regular expressions
Introduction to algorithms with java --Search (breadth-first search)
Challenge to deal with garbled characters with Java AudioSystem.getMixerInfo ()
[Java] How to test for null with JUnit
I tried to make Basic authentication with Java
Introduction to algorithms with java --Search (bit full search)
Deploy Java web app to Azure with maven
Try to link Ruby and Java with Dapr
How to use Java framework with AWS Lambda! ??
I want to use java8 forEach with index
Try to implement login function with Spring Boot
How to use Java API with lambda expression
Getting started with Kotlin to send to Java developers
Steps to play mp3 with haml using audio_tag
I want to play with Firestore from Rails
Try to implement TCP / IP + NIO with JAVA
[Java] Article to add validation with Spring Boot 2.3.1.
Easy to make LINE BOT with Java Servlet
I tried to break a block with java (1)
[Introduction to Spring Boot] Authentication function with Spring Security
Install java with Homebrew
Play Framework2.5 (Java) Tips
Changes from Java 8 to Java 11
Sum from Java_1 to 100
Change seats with java
Install Java with Ansible
[Java] Connect to MySQL
Comfortable download with JAVA
Switch java with direnv
Kotlin's improvements to Java
From Java to Ruby !!
Download Java with Ansible
14 Corresponds to function expressions
Let's scrape with Java! !!
Introduction to java command
Build Java with Wercker
With Kotorin ―― 7. Scoping Function
Serverless Function with Micronaut