Giter VIP home page Giter VIP logo

java11-category-theory-kleisli-category's Introduction

Build Status

java11-category-theory-kleisli-category

Reference: https://bartoszmilewski.com/2014/12/23/kleisli-categories/

preface

  • Kleisli operator (>=>) is generalisation of function composition (associativity).

project description

We provide:

  • Java implementation of the given examples (c++, haskell)
    • category
      static <A> Function<A, Writer<A>> identity() {
          return (A a) -> new Writer<>(a, "");
      }
      
      static <A, B, C> Function<A, Writer<C>> compose(Function<A, Writer<B>> f1,
                                                      Function<B, Writer<C>> f2) {
          return (A a) -> {
              Writer<B> f1value = f1.apply(a);
              Writer<C> f2value = f2.apply(f1value.result);
      
              return new Writer<>(f2value.result, f1value.log + f2value.log);
          };
      }
      
      where Writer is simply:
      class Writer<X> {
          X result;
          String log;
      
          Writer(X result, String log) {
              this.result = result;
              this.log = log;
          }
      }
      
    • tests
      var composed = KleisliWriterCategory.compose(this::toUpperCase, this::toLetters);
      
      var writer = composed.apply("test");
      
      assertThat(writer.result, is(new String[]{"T", "E", "S", "T"}));
      assertThat(writer.log, is("-toUpperCase-toLetters"));
      
      where functions are:
      private Writer<String> toUpperCase(String str) {
          return new Writer<>(str.toUpperCase(), "-toUpperCase");
      }
      
      private Writer<String[]> toLetters(String str) {
          return new Writer<>(str.split(""), "-toLetters");
      }
      
  • Java implementation of challenges (partial function category)
    • category
      static <A> Function<A, Optional<A>> identity() {
          return Optional::of;
      }
      
      static <A, B, C> Function<A, Optional<C>> compose(Function<A, Optional<B>> f1,
                                                      Function<B, Optional<C>> f2) {
          return f1.andThen(result -> result.flatMap(f2));
      }
      
    • tests
      @Test
      public void composing_safeRoot_safeReciprocal_zero() {
          var composed = KleisliPartialFunctionCategory.compose(this::safeRoot, this::safeReciprocal);
      
          var reciprocalRoot = composed.apply(0d);
      
          assertTrue(reciprocalRoot.isEmpty());
      }
      
      @Test
      public void composing_safeRoot_safeReciprocal_81() {
          var composed = KleisliPartialFunctionCategory.compose(this::safeRoot, this::safeReciprocal);
      
          var reciprocalRoot = composed.apply(0.25d);
      
          assertThat(reciprocalRoot.orElseThrow(), is(2d));
      }
      
      where functions are:
       private Optional<Double> safeRoot(double x) {
           return (x >= 0) ? Optional.of(Math.sqrt(x)) : Optional.empty();
       }
      
       private Optional<Double> safeReciprocal(double x) {
           return x != 0 ? Optional.of(1 / x) : Optional.empty();
       }
      

java11-category-theory-kleisli-category's People

Contributors

mtumilowicz avatar

Stargazers

 avatar  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.