|
1 | 1 | # JLet |
2 | 2 |
|
3 | | -A set of scope functions for Java |
| 3 | +[](https://central.sonatype.com/artifact/dev.jlet/jlet) |
| 4 | +[](https://javadoc.io/doc/dev.jlet) |
| 5 | +[](https://github.com/jlet-functions/jlet/blob/main/LICENSE) |
| 6 | +[](https://github.com/jlet-functions/jlet/actions/workflows/tests.yml/) |
| 7 | + |
| 8 | +A set of scope functions for Java. |
| 9 | + |
| 10 | +## Contents |
| 11 | + |
| 12 | +* [What is JLet](#what-is-jlet) |
| 13 | +* [How to use](#how-to-use) |
| 14 | +* [Functions](#functions) |
| 15 | + * [`let`](#let) |
| 16 | + * [`test`](#test) |
| 17 | + * [`run`](#run) |
| 18 | + * [`with`](#with) |
| 19 | + * [`it`](#it) |
| 20 | +* [Checked exceptions](#checked-exceptions) |
| 21 | +* [Contributors](#contributors) |
| 22 | +* [Licence](#license) |
| 23 | + |
| 24 | +## What is JLet |
| 25 | + |
| 26 | +JLet is a set of scope functions for Java, similar to Kotlin's scope functions but Java-specific. |
| 27 | + |
| 28 | +When you call a scope function on an object with a lambda expression provided, it forms a temporary scope. In this |
| 29 | +scope, you can access the object as a lambda expression argument. |
| 30 | + |
| 31 | +## How to use |
| 32 | + |
| 33 | +Requires Java 8+ version. |
| 34 | + |
| 35 | +Maven: |
| 36 | + |
| 37 | +<!-- @formatter:off --> |
| 38 | +```xml |
| 39 | +<dependencies> |
| 40 | + <dependency> |
| 41 | + <groupId>dev.jlet</groupId> |
| 42 | + <artifactId>jlet</artifactId> |
| 43 | + <version>1.0</version> |
| 44 | + </dependency> |
| 45 | +</dependencies> |
| 46 | +``` |
| 47 | +<!-- @formatter:on --> |
| 48 | + |
| 49 | +Gradle (Groovy): |
| 50 | + |
| 51 | +<!-- @formatter:off --> |
| 52 | +```groovy |
| 53 | +dependencies { |
| 54 | + implementation 'dev.jlet:jlet:1.0' |
| 55 | +} |
| 56 | +``` |
| 57 | +<!-- @formatter:on --> |
| 58 | + |
| 59 | +Gradle (Kotlin): |
| 60 | + |
| 61 | +<!-- @formatter:off --> |
| 62 | +```kotlin |
| 63 | +dependencies { |
| 64 | + implementation("dev.jlet:jlet:1.0") |
| 65 | +} |
| 66 | +``` |
| 67 | +<!-- @formatter:on --> |
| 68 | + |
| 69 | +## Functions |
| 70 | + |
| 71 | +### `let` |
| 72 | + |
| 73 | +* has arguments (from 1 to 8) |
| 74 | +* has result |
| 75 | + |
| 76 | +<!-- @formatter:off --> |
| 77 | +```java |
| 78 | +String origin = "abcdef"; |
| 79 | +String result = let(origin.toLowerCase(), origin.toUpperCase(), (lower, upper) -> |
| 80 | + lower + upper |
| 81 | +); |
| 82 | +``` |
| 83 | +<!-- @formatter:on --> |
| 84 | + |
| 85 | +### `test` |
| 86 | + |
| 87 | +* has arguments (from 0 to 8) |
| 88 | +* has result (`boolean`) |
| 89 | + |
| 90 | +<!-- @formatter:off --> |
| 91 | +```java |
| 92 | +if (test(() -> { |
| 93 | + // calculations |
| 94 | + return true; |
| 95 | +})) { |
| 96 | + System.out.println("OK"); |
| 97 | +} |
| 98 | +``` |
| 99 | +<!-- @formatter:on --> |
| 100 | + |
| 101 | +### `run` |
| 102 | + |
| 103 | +* has no arguments |
| 104 | +* has no result |
| 105 | + |
| 106 | +<!-- @formatter:off --> |
| 107 | +```java |
| 108 | +run(() -> |
| 109 | + System.out.println("OK") |
| 110 | +); |
| 111 | +``` |
| 112 | +<!-- @formatter:on --> |
| 113 | + |
| 114 | +### `with` |
| 115 | + |
| 116 | +* has arguments (from 1 to 8) |
| 117 | +* has no result |
| 118 | + |
| 119 | +<!-- @formatter:off --> |
| 120 | +```java |
| 121 | +String origin = "abcdef"; |
| 122 | +with(origin.substring(0, 3), substring -> |
| 123 | + System.out.println("substring: " + substring) |
| 124 | +); |
| 125 | +``` |
| 126 | +<!-- @formatter:on --> |
| 127 | + |
| 128 | +### `it` |
| 129 | + |
| 130 | +* has arguments (from 0 to 8) |
| 131 | +* has result (function result for the case of no arguments or the first value for others) |
| 132 | + |
| 133 | +<!-- @formatter:off --> |
| 134 | +```java |
| 135 | +Map<Integer, String> map = it(new HashMap<>(), m -> { |
| 136 | + m.put(1, "first"); |
| 137 | + m.put(2, "second"); |
| 138 | +}); |
| 139 | + |
| 140 | +MyObj myObj = new MyObjBuilder() |
| 141 | + .setValue1(100) |
| 142 | + .setValue2(it(() -> { |
| 143 | + // calculations |
| 144 | + return 200; |
| 145 | + })) |
| 146 | + .setValue3(it(map, m -> System.out.println(m))) |
| 147 | + .build(); |
| 148 | +``` |
| 149 | +<!-- @formatter:on --> |
| 150 | + |
| 151 | +## Checked exceptions |
| 152 | + |
| 153 | +All of these functions ignore checked exceptions. |
| 154 | + |
| 155 | +<!-- @formatter:off --> |
| 156 | +```java |
| 157 | +static void throwingMethod() throws Throwable { |
| 158 | + throw new Throwable(); |
| 159 | +} |
| 160 | + |
| 161 | +static void method() { |
| 162 | + run(() -> throwingMethod()); |
| 163 | +} |
| 164 | +``` |
| 165 | +<!-- @formatter:on --> |
| 166 | + |
| 167 | +## Contributors |
| 168 | + |
| 169 | +* [@evpl](https://github.com/evpl) as Evgenii Plugatar |
| 170 | + |
| 171 | +## License |
| 172 | + |
| 173 | +JLet is open-source project, and distributed under the [MIT](LICENSE) license. |
0 commit comments