Git Product home page Git Product logo

javascript-fp's Introduction

Javascript-FP

Functional programming to Javascript ES6+

평가

  • 코드가 계산(Evaluation)되어 값을 만드는 것

    log(1 + 2); // 3
    
    log([1, 2]); //배열로 평가
    
    log([1, 2, [3, 4]]); //[1, 2 , [Array[2]]]
    
    log([1, 2, ...[3, 4]]); //[1, 2, 3, 4]

일급

  • 값으로 다룰 수 있다.

  • 변수에 담을 수 있다.

  • 함수의 인자로 사용될 수 있다.

  • 함수의 결과로 사용될 수 있다.

    const a = 10;
    
    const add10 = (a) => a + 10;
    
    add10(a);
    
    const r = add10(a);
    log(r); //20

일급 함수

  • 함수를 값으로 다룰 수 있다.

  • 조합성과 추상화의 도구이다.

    const add = (a) => a + 5;
    
    log(add); // a => a + 5
    log(add(5)); //10 평가 후 결과로 전달
    
    const f1 = () => () => 1; // 함수를 return
    log(f1()); // () => 1
    
    const f2 = f1();
    log(f2); // () => 1
    log(f2()); // 1 원하는 시점에 평가

고차 함수

  • 함수를 값으로 다루는 함수를 고차함수라 하는데
    두가지 종류가 있음.

    • 함수를 인자로 받아서 실행하는 경우

      const apply1 = (f) => f(1); // const apply1 = (f) => a => a + 2(1)
      const add2 = (a) => a + 2;
      
      log(apply1(add2)); // 3
      log(apply1((a) => a - 1)); // 0
      
      const times = (f, n) => {
        let i = -1;
        while (++i < n) f(i);
      };
      
      times(log, 3); // 0 1 2
      times((a) => log(a + 10), 3); // 10 11 12
      //Applicative programming
    • 함수를 만들어 리턴하는 경우 (클로저를 만들어 리턴)

      const addMaker = (a) => (b) => a + b;
      // b => a + b 는 a를 기억하고 있다.
      const add10 = addMaker(10);
      log(add10); // b => a + b
      
      log(add10(5)); // 15
      log(add10(10)); // 20

javascript-fp's People

Contributors

wanderedtola avatar

Stargazers

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