-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
81 changed files
with
1,574 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
```js | ||
// hooks.ts | ||
// getting rid of the useEffect will remove complexity and unnecessary renders | ||
|
||
export function useGetOrCreateProduct() { | ||
const [product, setProduct] = (useState < Product) | (null > null) | ||
|
||
const { | ||
mutate: createWidget, | ||
data: newWidget, | ||
reset: resetCreateWidget, | ||
} = usePostWidget() | ||
const { | ||
mutate: createGadget, | ||
data: newGadget, | ||
reset: resetCreateGadget, | ||
} = usePostGadget() | ||
|
||
useEffect(() => { | ||
if (newWidget) { | ||
setProduct(newWidget) | ||
resetCreateWidget() | ||
} | ||
|
||
if (newGadget) { | ||
setProduct(newGadget) | ||
resetCreateGadget() | ||
} | ||
}, [newWidget, newGadget]) | ||
|
||
return { | ||
mutate: (id: Widget["id] | Gadget["id"]) => { | ||
if (isWidget(id)) { | ||
createWidget({ id }); | ||
} else { | ||
createGadget({ id }); | ||
} | ||
}, | ||
data: product | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
```js | ||
// hooks.ts | ||
|
||
export function useGetOrCreateProduct() { | ||
const [product, setProduct] = (useState < Product) | (null > null) | ||
|
||
const { | ||
mutate: createWidget, | ||
data: newWidget, | ||
reset: resetCreateWidget, | ||
} = usePostWidget() | ||
const { | ||
mutate: createGadget, | ||
data: newGadget, | ||
reset: resetCreateGadget, | ||
} = usePostGadget() | ||
|
||
useEffect(() => { | ||
if (newWidget) { | ||
setProduct(newWidget) | ||
resetCreateWidget() | ||
} | ||
|
||
if (newGadget) { | ||
setProduct(newGadget) | ||
resetCreateGadget() | ||
} | ||
}, [newWidget, newGadget]) | ||
|
||
return { | ||
mutate: (id: Widget["id] | Gadget["id"]) => { | ||
if (isWidget(id)) { | ||
createWidget({ id }); | ||
} else { | ||
createGadget({ id }); | ||
} | ||
}, | ||
data: product | ||
} | ||
} | ||
``` |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+212 Bytes
functional-programming/__MACOSX/functional-light-v3/._data-structures
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+302 Bytes
functional-programming/__MACOSX/functional-light-v3/async/._README.md
Binary file not shown.
Binary file added
BIN
+212 Bytes
functional-programming/__MACOSX/functional-light-v3/async/._ex.fixed.html
Binary file not shown.
Binary file added
BIN
+302 Bytes
functional-programming/__MACOSX/functional-light-v3/async/._ex.fixed.js
Binary file not shown.
Binary file added
BIN
+212 Bytes
functional-programming/__MACOSX/functional-light-v3/async/._ex.html
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+302 Bytes
functional-programming/__MACOSX/functional-light-v3/async/._rxjs.umd.min-v6.5.1.js
Binary file not shown.
Binary file added
BIN
+302 Bytes
functional-programming/__MACOSX/functional-light-v3/closure/._README.md
Binary file not shown.
Binary file added
BIN
+302 Bytes
functional-programming/__MACOSX/functional-light-v3/closure/._ex.fixed.js
Binary file not shown.
Binary file added
BIN
+302 Bytes
functional-programming/__MACOSX/functional-light-v3/closure/._ex.js
Binary file not shown.
Binary file added
BIN
+302 Bytes
functional-programming/__MACOSX/functional-light-v3/composition/._README.md
Binary file not shown.
Binary file added
BIN
+302 Bytes
functional-programming/__MACOSX/functional-light-v3/composition/._ex.fixed.js
Binary file not shown.
Binary file added
BIN
+302 Bytes
functional-programming/__MACOSX/functional-light-v3/composition/._ex.js
Binary file not shown.
Binary file added
BIN
+302 Bytes
functional-programming/__MACOSX/functional-light-v3/data-structures/._README.md
Binary file not shown.
Binary file added
BIN
+302 Bytes
functional-programming/__MACOSX/functional-light-v3/data-structures/._ex.fixed-1.js
Binary file not shown.
Binary file added
BIN
+302 Bytes
functional-programming/__MACOSX/functional-light-v3/data-structures/._ex.fixed-2.js
Binary file not shown.
Binary file added
BIN
+302 Bytes
functional-programming/__MACOSX/functional-light-v3/data-structures/._ex.js
Binary file not shown.
Binary file added
BIN
+302 Bytes
functional-programming/__MACOSX/functional-light-v3/immutability/._README.md
Binary file not shown.
Binary file added
BIN
+302 Bytes
functional-programming/__MACOSX/functional-light-v3/immutability/._ex.fixed.js
Binary file not shown.
Binary file added
BIN
+302 Bytes
functional-programming/__MACOSX/functional-light-v3/immutability/._ex.js
Binary file not shown.
Binary file added
BIN
+302 Bytes
functional-programming/__MACOSX/functional-light-v3/impurity/._README.md
Binary file not shown.
Binary file added
BIN
+302 Bytes
functional-programming/__MACOSX/functional-light-v3/impurity/._ex.fixed-1.js
Binary file not shown.
Binary file added
BIN
+302 Bytes
functional-programming/__MACOSX/functional-light-v3/impurity/._ex.fixed-2.js
Binary file not shown.
Binary file added
BIN
+302 Bytes
functional-programming/__MACOSX/functional-light-v3/impurity/._ex.js
Binary file not shown.
Binary file added
BIN
+302 Bytes
functional-programming/__MACOSX/functional-light-v3/lists/._README.md
Binary file not shown.
Binary file added
BIN
+302 Bytes
functional-programming/__MACOSX/functional-light-v3/lists/._ex.fixed.js
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+302 Bytes
functional-programming/__MACOSX/functional-light-v3/point-free/._README.md
Binary file not shown.
Binary file added
BIN
+302 Bytes
functional-programming/__MACOSX/functional-light-v3/point-free/._ex.fixed.js
Binary file not shown.
Binary file added
BIN
+302 Bytes
functional-programming/__MACOSX/functional-light-v3/point-free/._ex.js
Binary file not shown.
Binary file added
BIN
+358 Bytes
functional-programming/__MACOSX/functional-light-v3/recursion/._README.md
Binary file not shown.
Binary file added
BIN
+302 Bytes
functional-programming/__MACOSX/functional-light-v3/recursion/._ex.fixed.js
Binary file not shown.
Binary file added
BIN
+302 Bytes
functional-programming/__MACOSX/functional-light-v3/recursion/._ex.js
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Async FP | ||
|
||
## Instructions | ||
|
||
1. Run the exercise solution file (HTML) in the browser and observe how the countdown timer operates in the console: the timer prints the starting countdown value ("0:05") immediately, then counts down once per second to "0:00", then prints "Complete!". | ||
|
||
2. Now, run the original exercise file (HTML) in the browser and compare the difference in (broken) behavior. | ||
|
||
3. Attach `formatCountdown(..)` to the `countdown` observable (using an FP list operation!), and fill in the implementation (`// TODO` comment) so that the countdown timer prints as specified above. |
14 changes: 14 additions & 0 deletions
14
functional-programming/functional-light-v3/async/ex.fixed.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Exercise: Async FP -- Fixed</title> | ||
</head> | ||
<body> | ||
<h1>Exercise: Async FP -- Fixed</h1> | ||
|
||
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.5.1/rxjs.umd.min.js"></script> | ||
<script src="ex.fixed.js"></script> | ||
|
||
</body> | ||
</html> |
30 changes: 30 additions & 0 deletions
30
functional-programming/functional-light-v3/async/ex.fixed.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
"use strict"; | ||
|
||
const countdownLength = 5; | ||
|
||
var timer = rxjs.interval(1000).pipe( | ||
rxjs.operators.take(countdownLength), | ||
); | ||
var countdown = | ||
rxjs.merge(rxjs.of(-1),timer) | ||
.pipe( rxjs.operators.map(formatCountdown) ); | ||
|
||
countdown.subscribe( | ||
console.log.bind(console), | ||
null, | ||
console.log.bind(console,"Complete!") | ||
); | ||
|
||
|
||
// ************************************* | ||
|
||
function formatCountdown(counter) { | ||
return formatTime(countdownLength - counter - 1); | ||
} | ||
|
||
function formatTime(time) { | ||
var minutes = Math.floor(time / 60); | ||
var seconds = time % 60; | ||
if (seconds < 10) seconds = `0${seconds}`; | ||
return `${minutes}:${seconds}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Exercise: Async FP</title> | ||
</head> | ||
<body> | ||
<h1>Exercise: Async FP</h1> | ||
|
||
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/6.5.1/rxjs.umd.min.js"></script> | ||
<script src="ex.js"></script> | ||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
"use strict"; | ||
|
||
const countdownLength = 5; | ||
|
||
var timer = rxjs.interval(1000).pipe( | ||
rxjs.operators.take(countdownLength), | ||
); | ||
var countdown = | ||
rxjs.merge(rxjs.of(-1),timer) | ||
//.pipe( | ||
// rxjs.operators. --whatever-- | ||
//); | ||
|
||
countdown.subscribe( | ||
console.log.bind(console), | ||
null, | ||
console.log.bind(console,"Complete!") | ||
); | ||
|
||
|
||
// ************************************* | ||
|
||
function formatCountdown(counter) { | ||
// TODO | ||
} | ||
|
||
function formatTime(time) { | ||
var minutes = Math.floor(time / 60); | ||
var seconds = time % 60; | ||
if (seconds < 10) seconds = `0${seconds}`; | ||
return `${minutes}:${seconds}`; | ||
} |
258 changes: 258 additions & 0 deletions
258
functional-programming/functional-light-v3/async/rxjs.umd.min-v6.5.1.js
Large diffs are not rendered by default.
Oops, something went wrong.
20 changes: 20 additions & 0 deletions
20
functional-programming/functional-light-v3/closure/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Closure | ||
|
||
This is an exercise to practice closure. | ||
|
||
## Instructions | ||
|
||
1. Modify `strBuilder(..)` so that it can take a string and return back a function. | ||
|
||
**Note:** For purposes of this exercise, assume that `strBuilder(..)` itself is always called with a string initially. | ||
|
||
2. For each call to a function here, if a string is passed, a function should be returned. | ||
|
||
3. If a non-string is passed (such as passing no argument), a string value should be returned, which is the concatenation of all the passed in strings. | ||
|
||
4. Hints: | ||
- You can use `typeof foo == "string"` to test if `foo` is a string. | ||
|
||
- Look at the test cases at the bottom of the exercise file to clarify any questions about expected behavior. | ||
|
||
- Ensure your function(s) are pure. Avoid mutating a closed over variable, which would be a side-effect. |
23 changes: 23 additions & 0 deletions
23
functional-programming/functional-light-v3/closure/ex.fixed.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
"use strict"; | ||
|
||
function strBuilder(str) { | ||
return function next(v){ | ||
if (typeof v == "string") { | ||
return strBuilder(str + v); | ||
} | ||
return str; | ||
}; | ||
} | ||
|
||
var hello = strBuilder("Hello, "); | ||
var kyle = hello("Kyle"); | ||
var susan = hello("Susan"); | ||
var question = kyle("?")(); | ||
var greeting = susan("!")(); | ||
|
||
console.log(strBuilder("Hello, ")("")("Kyle")(".")("")() === "Hello, Kyle."); | ||
console.log(hello() === "Hello, "); | ||
console.log(kyle() === "Hello, Kyle"); | ||
console.log(susan() === "Hello, Susan"); | ||
console.log(question === "Hello, Kyle?"); | ||
console.log(greeting === "Hello, Susan!"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use strict' | ||
|
||
function strBuilder(str) { | ||
return function next(v) { | ||
if (typeof v !== 'string') return str | ||
return strBuilder(str + v) | ||
} | ||
} | ||
|
||
var hello = strBuilder('Hello, ') | ||
var kyle = hello('Kyle') | ||
var susan = hello('Susan') | ||
var question = kyle('?')() | ||
var greeting = susan('!')() | ||
|
||
console.log(strBuilder('Hello, ')('')('Kyle')('.')('')() === 'Hello, Kyle.') | ||
console.log(hello() === 'Hello, ') | ||
console.log(kyle() === 'Hello, Kyle') | ||
console.log(susan() === 'Hello, Susan') | ||
console.log(question === 'Hello, Kyle?') | ||
console.log(greeting === 'Hello, Susan!') |
9 changes: 9 additions & 0 deletions
9
functional-programming/functional-light-v3/composition/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Composition | ||
|
||
This is an exercise to practice composition. | ||
|
||
## Instructions | ||
|
||
1. Define a `compose(..)` that takes any number of functions (as individual arguments) and composes them right-to-left. | ||
|
||
2. Define a `pipe(..)` that takes any number of functions (as individual arguments) and composes them left-to-right. |
33 changes: 33 additions & 0 deletions
33
functional-programming/functional-light-v3/composition/ex.fixed.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
"use strict"; | ||
|
||
function increment(x) { return x + 1; } | ||
function decrement(x) { return x - 1; } | ||
function double(x) { return x * 2; } | ||
function half(x) { return x / 2; } | ||
|
||
function compose(...fns) { | ||
return pipe(...fns.reverse()); | ||
} | ||
|
||
function pipe(...fns) { | ||
return function piped(result) { | ||
for (let fn of fns) { | ||
result = fn(result); | ||
} | ||
return result; | ||
}; | ||
} | ||
|
||
var f1 = compose(increment,decrement); | ||
var f2 = pipe(decrement,increment); | ||
var f3 = compose(decrement,double,increment,half); | ||
var f4 = pipe(half,increment,double,decrement); | ||
var f5 = compose(increment); | ||
var f6 = pipe(increment); | ||
|
||
console.log( f1(3) === 3 ); | ||
console.log( f1(3) === f2(3) ); | ||
console.log( f3(3) === 4 ); | ||
console.log( f3(3) === f4(3) ); | ||
console.log( f5(3) === 4 ); | ||
console.log( f5(3) === f6(3) ); |
45 changes: 45 additions & 0 deletions
45
functional-programming/functional-light-v3/composition/ex.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
'use strict' | ||
|
||
function increment(x) { | ||
return x + 1 | ||
} | ||
function decrement(x) { | ||
return x - 1 | ||
} | ||
function double(x) { | ||
return x * 2 | ||
} | ||
function half(x) { | ||
return x / 2 | ||
} | ||
|
||
function compose(fn4, fn3, fn2, fn1) { | ||
return function (v) { | ||
if (!fn1 && !fn2 && !fn3) return fn4(v) | ||
if (!fn1 && !fn2) return fn4(fn3(v)) | ||
if (!fn1) return fn4(fn3(fn2(v))) | ||
return fn4(fn3(fn2(fn1(v)))) | ||
} | ||
} | ||
function pipe(fn1, fn2, fn3, fn4) { | ||
return function (v) { | ||
if (!fn4 && !fn3 && !fn2) return fn1(v) | ||
if (!fn4 && !fn3) return fn2(fn1(v)) | ||
if (!fn4) return fn3(fn2(fn1(v))) | ||
return fn4(fn3(fn2(fn1(v)))) | ||
} | ||
} | ||
|
||
var f1 = compose(increment, decrement) | ||
var f2 = pipe(decrement, increment) | ||
var f3 = compose(decrement, double, increment, half) | ||
var f4 = pipe(half, increment, double, decrement) | ||
var f5 = compose(increment) | ||
var f6 = pipe(increment) | ||
|
||
console.log(f1(3) === 3) | ||
console.log(f1(3) === f2(3)) | ||
console.log(f3(3) === 4) | ||
console.log(f3(3) === f4(3)) | ||
console.log(f5(3) === 4) | ||
console.log(f5(3) === f6(3)) |
22 changes: 22 additions & 0 deletions
22
functional-programming/functional-light-v3/data-structures/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Data Structures (And FP Review!) | ||
|
||
This is an exercise to practice list-operations (map/filter/reduce) on more general data structures. We also revisit a variety of previous FP concepts (point-free, currying, etc). | ||
|
||
## Instructions (Part 1) | ||
|
||
1. Familiarize yourself with the provided utilities and helpers, like `listSum(..)`, `listProduct(..)`, `mapObj(..)`, etc. | ||
|
||
2. Find the `// TODO` comments to implement both `filterObj(..)` and `reduceObj(..)`. The fixed exercise should now print `38886` to the console. | ||
|
||
## Instructions (Part 2) | ||
|
||
1. Recall/review the following topics: | ||
- argument manipulation (`binary(..)`) | ||
- point-free style | ||
- composition (`compose(..)`, `pipe(..)`) | ||
- currying | ||
- list operations (`reduce(..)`) | ||
|
||
2. Using only the provided utilities in this exercise, refactor the three separate statements that invoke `mapObj(..)`, `filterObj(..)`, and `reduceObj(..)` into a single list operation that's completely point-free. | ||
|
||
3. Hint: `reduce(..)` and `pipe(..)`. |
Oops, something went wrong.