Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/serg-maximchuk/function-utils
Java functional utils
https://github.com/serg-maximchuk/function-utils
functional java java-11 java-8 lambdas library utils utils-library
Last synced: about 1 month ago
JSON representation
Java functional utils
- Host: GitHub
- URL: https://github.com/serg-maximchuk/function-utils
- Owner: Serg-Maximchuk
- License: apache-2.0
- Created: 2019-07-11T09:00:50.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-07-27T08:32:13.000Z (over 4 years ago)
- Last Synced: 2024-10-13T15:42:16.419Z (3 months ago)
- Topics: functional, java, java-11, java-8, lambdas, library, utils, utils-library
- Language: Java
- Homepage:
- Size: 212 KB
- Stars: 3
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Java functional utils
![](https://img.shields.io/maven-central/v/io.github.serg-maximchuk/function-utils)
https://mvnrepository.com/artifact/io.github.serg-maximchuk/function-utils
Java 11
```groovy
compile 'io.github.serg-maximchuk:function-utils:1.1.6'
```
Or
```groovy
compile group: 'io.github.serg-maximchuk', name: 'function-utils', version: '1.1.6'
```
Or
```xmlio.github.serg-maximchuk
function-utils
1.1.6```
Java 8 suport:
```xmlio.github.serg-maximchuk
function-utils
1.1.1-java8```
```groovy
compile group: 'io.github.serg-maximchuk', name: 'function-utils', version: '1.1.1-java8'
```Dependency-free utility library born to reduce the code size and increase it's readability
when dealing with lambdas, and some more! It will help you to write java code in more
functional/imperative style.Throwing lambdas as a bonus!
Feel free to create a PR.
# Value::with
The main function, it will help you to write java code in more functional/imperative style.How many times you've been writing code like these pieces:
```
HttpHeaders headers = new HttpHeaders();
headers.add(AUTHORIZATION, "Basic ...");
return headers;
``````
User user = new User();
user.setEmail(email);
return repository.save(user);
``````
User user = new User();
users.add(user);
return user;
```The point is to shorten such cases, when you want to get/create something, made an action with
it, and return it back.Whole idea is to transform next common-used structure:
```
T t = new T();
t.f();
return t;
```
Or:
```
T t = new T();
outer.f(t);
return t;
```Into this:
```
return with(new T(), t -> t.f());
return with(new T(), t -> outer.f(t));// which also can be simplified
return with(new T(), T::f);
return with(new T(), outer::f);
```Meet the `Value` class and it's function `with`!
Main actors:
- `T value`, an operand of generic type, which will be returned in the end;
- void `Consumer action` whose the only argument would be the `value`.Take a look how it can reduce the code (`with` method is statically imported):
```
return with(new HttpHeaders(), headers -> headers.add(AUTHORIZATION, "Basic ..."));
``````
return repository.save(with(new User(), user -> user.setEmail(email)));
``````
return with(new User(), users::add);
```You may not be good enough in lambdas, so you may not understand these pieces of code,
don't panic! Try this approach few times, and you will start seeing potential places where
you can use this awesome function. You will start hating such code constructions:
```
Some some = new Some();
some.doSomething();
return some;
```**Note**, the `action` is a consumer - void function. The `Value::with` thing is **not** for
such case:
```
T t = new T();
return outer.f(t);
```
Which may be something like this in real life:
```
User user = new User();
return repo.save(user);
```
**BUT** the `Value::with` can help you in next case:
```
User user = new User();
user.setActive();
return repo.save(user);
```
Just don't forget to call `repo::save` method **outside** of a `Value::with`,
I believe you understand why.# Bonus: Throwing lambdas
Not only in the `Value::with` context, sometimes action in a lambda may throw some checked
exceptions. Throwing lambdas to the rescue:```
ThrowingBiConsumer
ThrowingBiFunction
ThrowingBinaryOperator
ThrowingBiPredicate
ThrowingBooleanSupplier
ThrowingConsumer
ThrowingDoubleBinaryOperator
ThrowingDoubleConsumer
ThrowingDoubleFunction
ThrowingDoublePredicate
ThrowingDoubleSupplier
ThrowingDoubleToIntFunction
ThrowingDoubleToLongFunction
ThrowingDoubleUnaryOperator
ThrowingFunction
ThrowingIntBinaryOperator
ThrowingIntConsumer
ThrowingIntFunction
ThrowingIntPredicate
ThrowingIntSupplier
ThrowingIntToDoubleFunction
ThrowingIntToLongFunction
ThrowingIntUnaryOperator
ThrowingLongBinaryOperator
ThrowingLongConsumer
ThrowingLongFunction
ThrowingLongPredicate
ThrowingLongSupplier
ThrowingLongToDoubleFunction
ThrowingLongToIntFunction
ThrowingLongUnaryOperator
ThrowingObjDoubleConsumer
ThrowingObjIntConsumer
ThrowingObjLongConsumer
ThrowingPredicate
ThrowingRunnable
ThrowingSupplier
ThrowingToDoubleBiFunction
ThrowingToDoubleFunction
ThrowingToIntBiFunction
ThrowingToIntFunction
ThrowingToLongBiFunction
ThrowingToLongFunction
ThrowingUnaryOperator
```
You can't compile such a thing without wrapping it into `try/catch` or using
`sneakyThrow` approach:
```
Value.with(t, t -> outer.thisMethodThrows(t));
```
But you may use built-in method with throwing lambda:
```
Value.withThrowing(t, t -> outer.thisMethodThrows(t));
```Throwing lambdas may be used as a stand-alone function whenever you want.
#
### Coming soon:
In throwing lambdas context: comparison why this library is better than others
```
https://www.javadoc.io/doc/io.vavr/vavr/1.0.0-alpha-2
https://github.com/pivovarit/throwing-function
https://github.com/SeregaLBN/StreamUnthrower
https://github.com/fge/throwing-lambdas
https://github.com/mscharhag/ET
https://github.com/jOOQ/jOOL
```