Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
파이프라인 오퍼레이터 및 람다
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
You can’t perform that action at this time.