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

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 ...)

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