Skip to content

Commit

Permalink
feat: udpate links
Browse files Browse the repository at this point in the history
  • Loading branch information
gglee89 committed Apr 1, 2024
1 parent ceaed4b commit d100aaf
Show file tree
Hide file tree
Showing 81 changed files with 1,574 additions and 2 deletions.
42 changes: 42 additions & 0 deletions code-refactor/react-query/react-query-refactored.md
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
}
}
```
41 changes: 41 additions & 0 deletions code-refactor/react-query/react-query.md
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 not shown.
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 not shown.
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 not shown.
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 not shown.
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 not shown.
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 not shown.
Binary file not shown.
Binary file not shown.
Binary file added functional-programming/functional-light-v3.zip
Binary file not shown.
9 changes: 9 additions & 0 deletions functional-programming/functional-light-v3/async/README.md
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 functional-programming/functional-light-v3/async/ex.fixed.html
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 functional-programming/functional-light-v3/async/ex.fixed.js
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}`;
}
14 changes: 14 additions & 0 deletions functional-programming/functional-light-v3/async/ex.html
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>
32 changes: 32 additions & 0 deletions functional-programming/functional-light-v3/async/ex.js
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}`;
}

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions functional-programming/functional-light-v3/closure/README.md
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 functional-programming/functional-light-v3/closure/ex.fixed.js
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!");
21 changes: 21 additions & 0 deletions functional-programming/functional-light-v3/closure/ex.js
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!')
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 functional-programming/functional-light-v3/composition/ex.fixed.js
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 functional-programming/functional-light-v3/composition/ex.js
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))
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(..)`.

0 comments on commit d100aaf

Please sign in to comment.