From e9049b36f7789e84e1d95f34806e2c1bd05c1a4b Mon Sep 17 00:00:00 2001 From: hyesungoh Date: Tue, 21 May 2024 16:14:59 +0900 Subject: [PATCH] =?UTF-8?q?=EC=B1=95=ED=84=B0=2012?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\354\230\244\355\230\234\354\204\261.md" | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 "\354\261\225\355\204\260_12/\354\230\244\355\230\234\354\204\261.md" diff --git "a/\354\261\225\355\204\260_12/\354\230\244\355\230\234\354\204\261.md" "b/\354\261\225\355\204\260_12/\354\230\244\355\230\234\354\204\261.md" new file mode 100644 index 0000000..d74a91b --- /dev/null +++ "b/\354\261\225\355\204\260_12/\354\230\244\355\230\234\354\204\261.md" @@ -0,0 +1,40 @@ +# 함수형 반복 + +## Map + +```js +function map(arr, f) { + const newArr = [] + for (const val of arr) { + newArr.push(f(val)) + } + return newArr +} +``` + +## Filter + +```js +function filter(arr, f) { + const newArr = [] + for (const val of arr) { + if (f(val)) newArr.push(val) + } + return newArr +} +``` + +## Reduce + +```js +function reduce(arr, init, f) { + let acc = init + for (const val of arr) { + acc = f(acc, val) + } + return acc +} +``` + +* fold 라는 이름으로도 사용됨 + + foldLeft, foldRight 같이 탐색 방향에 따른 버전도 있음