Skip to content

jlet-functions/jlet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JLet

Maven Central Javadoc License GitHub Actions Workflow Status

A set of scope functions for Java.

Contents

What is JLet

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.

How to use

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")
}

Functions

let

  • 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
);

test

  • has value arguments (from 0 to 8)
  • has result (lambda expression boolean result)
if (test(someObject.toString(), s -> s.length() > 10 && s.contains("."))) {
  System.out.println("OK");
}

run

  • has no value arguments
  • has no result
run(() -> {
  String localVariable1 = "string1";
  String localVariable2 = "string2";
  System.out.println(localVariable1);
  System.out.println(localVariable2);
});

with

  • 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);
});

it

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");
}));

Checked exceptions

All of these functions allow not catching checked exceptions.

static void throwingMethod() throws Throwable {
  throw new Throwable();
}

static void method() {
  run(() -> throwingMethod());
}

Contributors

  • @evpl as Evgenii Plugatar

License

JLet is open-source project, and distributed under the MIT license.