Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 07b2282

Browse files
author
Bob Lee
committed
Prepped Reduce Funcitonal
1 parent 0780b65 commit 07b2282

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed
0 Bytes
Binary file not shown.

‎source-code/6000_functional_programming/6004_reduce.playground/Contents.swift‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,35 @@
44
### Functional Programming
55
### Reduce
66

7-
**Problem:** A closure is too long to pass through a function
7+
**Problem:** Combine all elements into one
88

99
---
1010
*/
1111

1212

13+
let result = Array(1...10).reduce(0) { 0ドル + 1ドル }
14+
print(result)
15+
16+
17+
func myReduce(seed: Int, numbers: [Int], operation: (Int, Int) -> Int) {
18+
var current = seed
19+
for number in numbers {
20+
current = operation(current, number)
21+
}
22+
print("The number is \(current)")
23+
}
24+
25+
myReduce(seed: -100, numbers: Array(1...4), operation: { 0ドル + 1ドル })
26+
27+
// seed 없이 보여준다 초반에는
28+
29+
30+
1331
extension Array {
14-
func myReduce<T, U>(seed:U, combiner:(U, T) -> U) -> U {
32+
func myReduce<T, U>(seed:U, operation:(U, T) -> U) -> U {
1533
var current = seed
1634
for item in self {
17-
current = combiner(current, item as! T)
35+
current = operation(current, item as! T)
1836
}
1937
return current
2038
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /