Last active
May 12, 2018 18:30
-
-
Save irrationnelle/072c6920bc3272f50e416ee17dc82c2f to your computer and use it in GitHub Desktop.
파이프라인 오퍼레이터 및 람다
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { curry } from 'ramda'; // webpack의 tree-shaking을 사용하기 위해 { curry } 만 추출 | |
const double = n => n * 2; | |
const increment = n => n + 1; | |
const add = (x, y) => x + y; | |
const boundScore = (min, max, score) => Math.max(min, Math.min(max, score)); | |
// pipeline operator 없이 함수들을 조합한 경우 | |
const result1 = double(increment(double(double(5)))); // 42 | |
// pipeline operator 을 사용하여 함수들을 조합한 경우 | |
const result2 = 5 |> double |> double |> increment |> double; // 42 | |
console.log('result1', result1); | |
console.log('result2', result2); | |
const person = { score: 25 }; | |
const newScore1 = person.score | |
|> double | |
|> (_ => add(7, _)) // partial application | |
|> (_ => boundScore(0, 100, _)); // // partial application | |
console.log('newScore1', newScore1); //=> 57 | |
const curriedAdd = curry(add); // currying | |
const addSeven = curriedAdd(7); | |
const newScore2 = person.score | |
|> double | |
|> addSeven | |
|> (_ => boundScore(0, 100, _)); | |
console.log('newScore2', newScore2); //=> 57 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment