Coder Social home page Coder Social logo

optional2's Introduction

optional2

Optional with multiple values.

// In Functional Programming, single values chain together to form a result.
// In this example: name -> UUID -> id string
String userIdStr = Optional.of("Cora")
    .map(name -> TestService.findUserId(name))
    .map(userId -> userId.toString())
    .get();

// However, oftentimes there are multiple results that you want to combine.
// In pure FP, you'd use memoization, or perhaps a monad or a tuple.
// In Java, it usually means abandoning FP for common simple cases.
// I suggest that we could try using Optional2:
//
// name -> UUID -> (name + UUID) = user

    TestUser goodUser = Optional2.of("Cora")                       // Optional2 stores: "Cora"
    .andOf(name -> TestService.findUserId(name))                   // Optional2 stores: "Cora" + UUID
    .reduce((name, userId) -> TestService.loginUser(userId, name)) // "Cora" + UUID = user obj
    .get();

// Notice that in the .reduce() call, the vars have strong names and strong types.
// You get a great blend of FP and Java.
//
// Let's do one more example:

    Optional2
    .ofNullable(NULL_STR)
    .ifEmpty(() -> LOG.warn("Name is null! Using backup source."))
    .or(() -> Optional.of("Cora"))
    .andOf(name -> null)
    .or(() -> Optional.of(UUID.randomUUID()))
    .ifPresent((name, userId) -> LOG.info("User: {} {}", name, userId));

// The Java Optional methods you're used to are all there, just extended to support
// a second value, and with a little more usability sprinkled in.
//
// .reduce() is the only newly-added method.
// It reduces down to a Java Optional, and any Optional2 can be converted
// back into a Java Optional at any time via .getOptional().

optional2's People

Contributors

terheyden avatar

Watchers

 avatar

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.