Code Monkey home page Code Monkey logo

y-combinator-java's Introduction

y-combinator-java

In functional-programing, the Y-Combinartor is defined as :

Definition

It is also called Fixed-point combinator:

Explain

This is a java version of Y-Combinator.

Usage:

public static void main(String[] args) {
UnaryOperator<Function<Integer, BigInteger>> fib =
    (Function<Integer, BigInteger> f) ->
        (Integer n) -> {
          if (n == 0) return BigInteger.ZERO;
          if (n == 1 || n == 2) return BigInteger.ONE;

          Integer m = n >>> 1;
          if ((n & 1) == 0) {
            BigInteger temp = f.apply(m + 1).add(f.apply(m - 1));
            return f.apply(m).multiply(temp);
          } else {
            BigInteger fm = f.apply(m);
            BigInteger fm1 = f.apply(m + 1);
            return fm.multiply(fm).add(fm1.multiply(fm1));
          }
        };

int n = 1000000;
long t1 = System.nanoTime();
BigInteger n1 = new Y<Integer, BigInteger>().apply(fib).apply(n);
long t2 = System.nanoTime();
BigInteger n2 = new CachedY<Integer, BigInteger>().apply(fib).apply(n);
long t3 = System.nanoTime();
System.out.printf("Y,len:%d,time:%dms%n", n1.toString().length(), (t2 - t1) / 1000_000);
System.out.printf("CachedY,len:%d,time:%dms%n", n2.toString().length(), (t3 - t2) / 1000_000);
}
//Y,len:208988,time:3772ms
//CachedY,len:208988,time:28ms

y-combinator-java's People

Contributors

yuyuzha0 avatar

Watchers

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