Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[오혜성] 챕터 12: 함수형 반복 #68

Merged
merged 1 commit into from
May 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions 챕터_12/오혜성.md
Original file line number Diff line number Diff line change
@@ -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 같이 탐색 방향에 따른 버전도 있음
Loading