A set of scope functions for Java.
JLet is a set of scope functions for Java, similar to Kotlin's scope functions but Java-specific.
When you call a scope function with an object and a lambda expression, it forms a temporary scope. In this scope, you can access the object as a lambda expression argument.
Requires Java 8+ version.
Maven:
<dependencies>
<dependency>
<groupId>dev.jlet</groupId>
<artifactId>jlet</artifactId>
<version>1.0</version>
</dependency>
</dependencies>Gradle (Groovy):
dependencies {
implementation 'dev.jlet:jlet:1.0'
}Gradle (Kotlin):
dependencies {
implementation("dev.jlet:jlet:1.0")
}- has value arguments (from 1 to 8)
- has result (lambda expression result)
String shortString = let(someObject.toString(), s ->
s.length() > 10 ? s.substring(0, 11) : s
);- has value arguments (from 0 to 8)
- has result (lambda expression
booleanresult)
if (test(someObject.toString(), s -> s.length() > 10 && s.contains("."))) {
System.out.println("OK");
}- has no value arguments
- has no result
run(() -> {
String localVariable1 = "string1";
String localVariable2 = "string2";
System.out.println(localVariable1);
System.out.println(localVariable2);
});- has value arguments (from 1 to 8)
- has no result
with(someObject.toString(), s -> {
String shortString = s.length() > 10 ? s.substring(0, 11) : s;
System.out.println(shortString);
});There are two options
- has no value arguments
- has result (lambda expression result)
someObject.setString(it(() -> {
// calculations
return "string";
}));- has value arguments (from 1 to 8)
- has result (the first value argument)
someObject.setMap(it(new HashMap<>(), m -> {
m.put(1, "first");
m.put(2, "second");
}));All of these functions allow not catching checked exceptions.
static void throwingMethod() throws Throwable {
throw new Throwable();
}
static void method() {
run(() -> throwingMethod());
}- @evpl as Evgenii Plugatar
JLet is open-source project, and distributed under the MIT license.