Skip to content

Commit 0effaca

Browse files
authored
Merge pull request #161 from cherryblossom000/rm-fork-join-sols
remove fork-join solutions
2 parents e80e68e + 58f5a4c commit 0effaca

File tree

2 files changed

+1
-33
lines changed

2 files changed

+1
-33
lines changed

_chapters/asteroids.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,7 @@ The job of the programmer (e.g., you in Assignment 1) will be to create appropri
851851

852852
Finally, we need to make a couple more additions to the CSS to display the rocks and game over text:
853853

854-
```CSS
854+
```css
855855
.rock {
856856
fill: burlywood;
857857
}

_chapters/higherorderfunctions.md

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -462,38 +462,6 @@ Now we can try to find an id for each student, first from `class1` but fall back
462462
const ids = students.map(or(lookup(class1))(lookup(class2)))
463463
```
464464
465-
#### Solutions
466-
467-
We can use the fork-join combinator by considering the sum and count of the sequence as the two branches (`f` and `g`), and then using a join function to divide the sum by the count to compute the average.
468-
469-
```typescript
470-
const sum = arr => arr.reduce((acc, x) => acc + x, 0);
471-
const count = arr => arr.length;
472-
473-
const average = (sum, count) => sum / count;
474-
475-
// Create the average function using fork
476-
const computeAverage = fork(average, sum, count);
477-
478-
const values = [1, 2, 3, 4, 5];
479-
console.log(computeAverage(values));
480-
```
481-
482-
> 3
483-
484-
We would need four distinct generic types for this function:
485-
486-
```typescript
487-
function fork<T, U, V, R>(join: (a: U, b: V) => R, f: (value: T) => U, g: (value: T) => V): (value: T) => R {
488-
return (value: T) => join(f(value), g(value));
489-
}
490-
```
491-
492-
1. `T` for the type of the input value.
493-
2. `U` for the type of the result of function `f`.
494-
3. `V` for the type of the result of function `g`.
495-
4. `R` for the type of the result of the `join` function.
496-
497465
---
498466
499467
## End Note

0 commit comments

Comments
 (0)