GithubHelp home page GithubHelp logo

mtumilowicz / java11-vavr093-partial-function-lifting-workshop Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 121 KB

Vavr Partial Function and function lifting workshops.

License: GNU General Public License v3.0

Groovy 74.05% Java 25.95%
vavr partial-functions workshop workshop-materials workshops lifting lift

java11-vavr093-partial-function-lifting-workshop's Introduction

Build Status License: GPL v3

java11-vavr093-partial-function-lifting-workshop

project description

theory in a nutshell

  • a partial function from X to Y is a function f: K โ†’ Y, for some K c X. For x e X\K function is undefined
  • it generalizes the concept of a function f: X โ†’ Y by not forcing f to map every element of X to an element of Y
    • that means a partial function works properly only for some input values
    • in programming, if the function is called with a disallowed input value, it will typically throw an exception
      • in particular - every function that throws an exception is a partial function
  • partial function (to set intuition)
    int do(int positive) {
        if (positive < 0) {
                throw new IllegalArgumentException("Only positive integers are allowed"); 
        }
        // other stuff
    }
    
  • vavr's partial function interface
    public interface PartialFunction<T, R> {
        R apply(T t);
    
        boolean isDefinedAt(T value); // tests if a value is contained in the function's domain
    }
    
    • the caller is responsible for calling the method isDefinedAt() before this function is applied to the value
    • if the function is not defined for a specific value, apply() may produce an arbitrary result
      • in particular - random values
      • more specifically - it is not guaranteed that the function will throw an exception
    • do(int positive) mentioned above - rewritten with vavr:
      class RandomIdentity implements PartialFunction<Integer, Integer> {
          
          @Override
          public Integer apply(Integer o) {
              if (!isDefinedAt(o)) {
                  throw new IllegalArgumentException("Only positive integers are allowed");
              }
              // other stuff
          }
      
          @Override
          public boolean isDefinedAt(Integer value) {
              return nonNull(value) && value > 0;
          }
      }
      
  • In programming - we usually lift partial function f: (K c X) -> Y to g: X -> Option<Y> in such a manner:
    • a lifted function returns Some, if the function is invoked with allowed input values
      • g(x).get() = f(x) on K
    • a lifted function returns None instead of throwing an exception, if the function is invoked with disallowed input values
      • g(x) = Option.none() for x e X\K

conclusions in a nutshell

In vavr we have two approaches to lifting:

  • lifting function with Option
    Function2<Integer, Integer, Integer> divide = (a, b) -> a / b;
    
    Function2<Integer, Integer, Option<Integer>> lifted = Function2.lift(divide);
    
    lifted.apply(1, 0) == Option.none()
    lifted.apply(4, 2) == Option.some(2)
    
  • lifting function with Try
    Function2<Integer, Integer, Integer> divide = (a, b) -> a / b;
    
    Function2<Integer, Integer, Try<Integer>> lifted = Function2.liftTry(divide);
    
    lifted.apply(1, 0).failure
    lifted.apply(1, 0).cause.class == ArithmeticException
    lifted.apply(4, 2) == Try.success(2)
    
  • Try is nearly always better, because it contains specific exception, which is often a very valuable information, we do not want to lose

java11-vavr093-partial-function-lifting-workshop's People

Contributors

mtumilowicz avatar

Stargazers

 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.