@jamessawle/java-lambda
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

Java Lambda

A package to provide functional primitives, inline with Java's functional patterns for Javascript, following the Java SE SDK for provided features.

The name is based on the fact that the primitives were added as part of Java 8's Lambda functions.

Install

Node: npm i @jamessawle/java-lambda

Usage

Optional

The Optional class allows for safe processing of data that may not be present.

import { Optional } from '@jamessawle/java-lambda'

const input = 12 | undefined
const result = Optional.ofNullable(input)
  .map((x) => x * 2)
  .filter((x) => x > 20)
  .orElseGet(5)

// 12 => 24
// undefined => 5

Note Optionals are not intended to be used as object values or method parameters, but instead a clear way to represent "no result" - designer of Optional type.

Stream (Not implemented yet)

The Stream class allows for the processing of data in the form of a pipeline. Utility methods are provided to turn different types into a Stream.

Whilst similar to the utility methods map, filter etc. on Arrays in Javascript; the Stream class will lazily operate on each element in the Stream, only processing data that is required to fulfill the final result.

In the following example, only the first element in the provided array will ever be processed:

const result = Stream.of([12, 24, 36, 48, 60])
  .map((x) => x / 2)
  .filter((x) => x < 20)
  .findFirst()

// => 6

/@jamessawle/java-lambda/

    Package Sidebar

    Install

    npm i @jamessawle/java-lambda

    Weekly Downloads

    0

    Version

    0.1.0

    License

    MIT

    Unpacked Size

    19.3 kB

    Total Files

    6

    Last publish

    Collaborators

    • jamessawle