Giter Site home page Giter Site logo

lambdax's Introduction

LambdaX logo

LambdaX 0.5.0 Maven CentralJavadocsGitHub

Build StatuscodecovCoverage StatusCodacy BadgeBCH complianceGitHub code size in bytes FOSSA Status

⚠️ This library is under experimental development - expect the major version ⚠️

This library contains utility classes with useful lambdas.

Features

LambdaX uses concepts of functional programming:

LambdaX provides the following:

Examples

Chain

  • Data flow:
    int integer = ChainX.of("some string")
            .map(String::length)
            .filter(length -> length > 0)
            .mutate(length -> holder[0] = length)
            .orElse(-1);

Collection

  • Get the value from Map and print it on the console:
    Map<Integer, String> map = new HashMap<>();
    map.put(1, "one");
    Holder<Map<Integer, String>> holder = new Holder<>(map);
// Plain Java
    Map<Integer, String> map = holder.get();
    String actual = map.get(key);
    System.out.println(actual);
// Chaining Style
    Optional.of(holder)
            .map(Holder::get)
            .map(m -> m.get(key))
            .ifPresent(System.out::println);
// LambdaX
    String actual = Optional.of(holder)
             .map(Holder::get)
             .map(MapX.get(key))
             .ifPresent(System.out::println);
  • Insert the value into Collection if it does not contain it:
    Collection<Integer> numbers = new ArrayList<>();
    Holder<Collection<Integer>> holder = new Holder<>(numbers);
    int value = 1;
// Plain Java
    Collection<Integer> target = holder.get();
    if (!target.contains(value)) {
        target.add(value);
    }
// Chaining Style  
    Optional.of(holder)
            .map(Holder::get)
            .filter(collection -> !collection.contains(value))
            .ifPresent(collection -> collection.add(value));
// LambdaX
    Optional.of(holder)
            .map(Holder::get)
            .filter(CollectionX.notContains(value))
            .ifPresent(CollectionX.onlyAdd(value));

Predicate

  • Filter holders by name (not null safe):
List<Holder<Item>> filterByName(List<Holder<Item>> list) {
    return list.stream()
            .filter(PredicateX.of(Holder<Item>::get)
                    .map(Item::getName) // NPE if item is null
                    .equal("Item name"))
            .collect(Collectors.toList());
}

and null safe variant:

List<Holder<Item>> filterByExistingName(List<Holder<Item>> list) {
    return list.stream()
            .filter(PredicateX.ofNullable(Holder<Item>::get)
                    .map(Item::getName)
                    .equal("Item name")
                    .orLie()) // return false if item or item name is null
            .collect(Collectors.toList());
}

Installation

Releases are available in Maven Central.

List of version changes.

Maven

Add this snippet to the pom.xml dependencies section:

<dependency>
  <groupId>io.github.alexengrig</groupId>
  <artifactId>lambdax</artifactId>
  <version>0.5.0</version>
</dependency>

Gradle

Add this snippet to the build.gradle dependencies section:

implementation 'io.github.alexengrig:lambdax:0.5.0'

Others

Other snippets are available in The Central Repository.

License

This project is licensed under Apache License, version 2.0.

FOSSA Status

lambdax's People

Contributors

alexengrig avatar codacy-badger avatar dependabot[bot] avatar fossabot avatar imgbotapp avatar restyled-commits avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

lambdax's Issues

Create PredicateX#isNull and PredicateX#notNull

Use:

boxes
    .stream()
//      .filter(box -> box.getItem().getName() == null
    .filter(PredicateX.of(Box::getItem).map(Item::getName).isNull())
//      .filter(box -> box.getItem().getName() != null
    .filter(PredicateX.of(Box::getItem).map(Item::getName).notNull())

Create MapX

Create utility class with useful lambdas for java.lang.Map.

Fix MapX#get

Fix MapX#get return type:

Function<Map<? extends K, ? extends V>, ? extends V>

Add null safe implementation of PredicateI and ComparablePredicatI

Add check of the arguments on null - require not null.

PredicateX.of(Box::getItem).map(null); // throws the NPE

Add check of the mapper result on null - skip mapping.

Box box = new Box(null); // without a item
PredicateX.of(Box::getItem)
    .map(Item::getName) // NPE
    .equal("some name");

Solution

Add PredicateX.ofNullable(...) with null handling.

Fix PredicateX#map

Describe the bug
Cannot resolve method 'map(java.util.function.Function<java.util.Map<? extends K,? extends V>,V>)'

Target
Method: PredicateX#map and MapX#get

Code

class Entry {
    private Map<Integer, String> numbers;
}

<...>

List<String> numbers = entries.stream()
    .filter(PredicateX.of(Entry::getNumbers)
                    // .map(MapX.get(1)) // doesn't work
                    // .map(m -> m.get(1)) // doesn't work
                    .map((Map<Integer, String> m) -> m.get(1)) // works
                    .nonNull())
    .collect(Collectors.toList());

Create DequeX

Create utility class with useful lambdas for java.lang.Deque.

Create QueueX

Create utility class with useful lambdas for java.lang.Queue.

Create OptionalX

Create utility class with useful lambdas for java.lang.Optional.

Create ChainX

Use:

ChainX.of(HashMap<Integer, String>::new)
    .mutate(MapX.onlyPut(1, "one"))
    .mutate(MapX.onlyPut(2, "two"))
    .get();

Refactor PredicateX

Migrate PredicateX to interface.
Create PredicateChainX.

PredicateX.chain(Box::get);
PredicateChainX.of(Box:get);
  • Implemention
  • Test
  • Documentation
  • CHANGES
  • README
  • MIGRATION

Create ListX

Create utility class with useful lambdas for java.lang.List.

Create SetX

Create utility class with useful lambdas for java.lang.Set.

Create PredicateX#from(Predicate)

Use:

Predicate<Box> predicate = Box::isEmpty; // ok
Predicate<Box> predicate = Box::isEmpty.and(nextPredicate); // not work
Predicate<Box> predicate = PredicateX.from(Box::isEmpty).and(nextPredicate); // work

Implementation:

<T> Predicate<T> from(Predicate<? super T> checker) {
    return (Predicate<T>) checker;
}

Negate methods - "not"

Add methods with negate predicate; name prefix "not".
Example: Collection#notContains.

Create mappable predicate

Create static constructor of, methods map, check, equal, less, greater, lessOrEqual, greaterOrEqual.

Use:

boxes
    .stream()
//  NPE    .filter(box -> box.getItem().getName().equals("Coca-Cola")
    .filter(PredicateX.of(Box::getItem).map(Item::getName).check("Coca-Cola"::equals))
    .collect(Collectors.toList());

Fix JavaDoc

public static Predicate xnor(Predicate first, Predicate second, Predicate... others)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.