Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add fn compostion fns, avg & array reverse fns
- Loading branch information
1 parent
ab7152e
commit 3d6e4d9
Showing
46 changed files
with
1,531 additions
and
179 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,5 @@ | ||
--- | ||
"@opentf/std": minor | ||
--- | ||
|
||
Added functions composition related functions, Math avg & Array reverse fns. |
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 @@ | ||
{ "*.md": "prettier --write" } |
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
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
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
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
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,46 @@ | ||
import { Callout } from "nextra/components"; | ||
import REPL from "../../components/REPL"; | ||
|
||
> Reverses the given list of elements order. | ||
<Callout emoji="✅" type="info"> | ||
Immutable: This does not mutate the given array. | ||
</Callout> | ||
|
||
## Syntax | ||
|
||
```ts | ||
import { reverse } from '@opentf/std'; | ||
|
||
reverse<T>(arr: T[] = []): T[] | ||
``` | ||
|
||
## Examples | ||
|
||
```ts | ||
reverse([1, 2, 3]) //=> [3, 2, 1] | ||
|
||
reverse([{ a: 1 }, { b: 2 }, { c: 3 }]) | ||
//=> [ | ||
// { | ||
// c: 3, | ||
// }, | ||
// { | ||
// b: 2, | ||
// }, | ||
// { | ||
// a: 1, | ||
// }, | ||
// ] | ||
|
||
reverse('Apple')//=> ['e', 'l', 'p', 'p', 'A'] | ||
|
||
reverse([1, , 3])//=> [3, undefined, 1] | ||
``` | ||
|
||
## Try | ||
|
||
<REPL code={`const { reverse } = require('@opentf/std'); | ||
reverse([1, 2, 3]); | ||
`} /> |
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 |
---|---|---|
@@ -1,5 +1,9 @@ | ||
{ | ||
"aCompose": "aCompose", | ||
"aComposeFn": "aComposeFn", | ||
"aFilter": "aFilter", | ||
"aForEach": "aForEach", | ||
"aMap": "aMap" | ||
"aMap": "aMap", | ||
"aPipe": "aPipe", | ||
"aPipeFn": "aPipeFn" | ||
} |
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,68 @@ | ||
import { Callout } from "nextra/components"; | ||
import REPL from "../../components/REPL"; | ||
|
||
> Performs functions composition from right to left asynchronously. | ||
<Callout type="default"> | ||
The `compose` function is equivalent to `a(b(c(val)))`. | ||
</Callout> | ||
|
||
**Related** | ||
|
||
- [compose](/Function/compose) | ||
- [composeFn](/Function/composeFn) | ||
- [aComposeFn](/Async/aComposeFn) | ||
- [pipe](/Function/pipe) | ||
- [pipeFn](/Function/pipeFn) | ||
- [aPipe](/Async/aPipe) | ||
- [aPipeFn](/Async/aPipeFn) | ||
|
||
## Syntax | ||
|
||
```ts | ||
import { aCompose } from '@opentf/std'; | ||
|
||
aCompose( | ||
val: unknown, | ||
...fns: Function[] | ||
): Promise<unknown> | ||
``` | ||
|
||
## Examples | ||
|
||
```ts | ||
await aCompose( | ||
1, | ||
(x) => Promise.resolve(x + 1), | ||
(x) => Promise.resolve(x * 5) | ||
); //=> 6 | ||
``` | ||
|
||
## Try | ||
|
||
<REPL code={`const { aCompose } = require('@opentf/std'); | ||
async function main() { | ||
const out = await aCompose(1, | ||
(x) => Promise.resolve(x + 1), | ||
(x) => Promise.resolve(x \* 5) | ||
); | ||
log(out); | ||
} | ||
main(); | ||
`} /> | ||
|
||
## Learn | ||
|
||
Why we need function composition? | ||
|
||
- The deep nesting of functions is hard to read. | ||
- It eliminates temporary variables. | ||
- Method chaining is limited, for Eg: await, yeild, etc. | ||
|
||
### Resources | ||
|
||
- [tc39/proposal-pipeline-operator](https://github.com/tc39/proposal-pipeline-operator) | ||
|
||
- [Hack Pipe Operator](https://docs.hhvm.com/hack/expressions-and-operators/pipe) |
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,64 @@ | ||
import { Callout } from "nextra/components"; | ||
import REPL from "../../components/REPL"; | ||
|
||
> Returns a function that performs functions composition from right to left asynchronously. | ||
<Callout type="default"> | ||
The `compose` function is equivalent to `a(b(c(val)))`. | ||
</Callout> | ||
|
||
**Related** | ||
|
||
- [compose](/Function/compose) | ||
- [composeFn](/Function/composeFn) | ||
- [aCompose](/Async/aCompose) | ||
- [pipe](/Function/pipe) | ||
- [pipeFn](/Function/pipeFn) | ||
- [aPipe](/Async/aPipe) | ||
- [aPipeFn](/Async/aPipeFn) | ||
|
||
## Syntax | ||
|
||
```ts | ||
import { aComposeFn } from '@opentf/std'; | ||
|
||
aComposeFn( | ||
...fns: Function[] | ||
): (...args: unknown[]) => unknown | ||
``` | ||
|
||
## Examples | ||
|
||
```ts | ||
const transform = aComposeFn((x) => Promise.resolve(Math.ceil(x)), Math.pow); | ||
await transform(1.5, 2.5); //=> 3 | ||
``` | ||
|
||
## Try | ||
|
||
<REPL code={`const { aComposeFn } = require('@opentf/std'); | ||
async function main() { | ||
const transform = aComposeFn( | ||
(x) => Promise.resolve(Math.ceil(x)), | ||
Math.pow | ||
); | ||
log(await transform(1.5, 2.5)); | ||
} | ||
main(); | ||
`} /> | ||
|
||
## Learn | ||
|
||
Why we need function composition? | ||
|
||
- The deep nesting of functions is hard to read. | ||
- It eliminates temporary variables. | ||
- Method chaining is limited, for Eg: await, yeild, etc. | ||
|
||
### Resources | ||
|
||
- [tc39/proposal-pipeline-operator](https://github.com/tc39/proposal-pipeline-operator) | ||
|
||
- [Hack Pipe Operator](https://docs.hhvm.com/hack/expressions-and-operators/pipe) |
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,68 @@ | ||
import { Callout } from "nextra/components"; | ||
import REPL from "../../components/REPL"; | ||
|
||
> Performs functions composition from left to right asynchronously. | ||
<Callout type="default"> | ||
The `pipe` function is equivalent to `c(b(a(val)))`. | ||
</Callout> | ||
|
||
**Related** | ||
|
||
- [pipe](/Function/pipe) | ||
- [pipeFn](/Function/pipeFn) | ||
- [aPipeFn](/Async/aPipeFn) | ||
- [compose](/Function/compose) | ||
- [composeFn](/Function/composeFn) | ||
- [aCompose](/Async/aCompose) | ||
- [aComposeFn](/Async/aComposeFn) | ||
|
||
## Syntax | ||
|
||
```ts | ||
import { aPipe } from '@opentf/std'; | ||
|
||
aPipe( | ||
val: unknown, | ||
...fns: Function[] | ||
): Promise<unknown> | ||
``` | ||
|
||
## Examples | ||
|
||
```ts | ||
await aPipe( | ||
"guest", | ||
(s) => capitalize(s), | ||
(x) => Promise.resolve(`Welcome ${x}`) | ||
); //=> 'Welcome Guest' | ||
``` | ||
|
||
## Try | ||
|
||
<REPL code={`const { aPipe, capitalize } = require('@opentf/std'); | ||
async function main() { | ||
const out = await aPipe('guest', | ||
(s) => capitalize(s), | ||
(x) => Promise.resolve(\`Welcome \${x}\`) | ||
); | ||
log(out); | ||
} | ||
main(); | ||
`} /> | ||
|
||
## Learn | ||
|
||
Why we need function composition? | ||
|
||
- The deep nesting of functions is hard to read. | ||
- It eliminates temporary variables. | ||
- Method chaining is limited, for Eg: await, yeild, etc. | ||
|
||
### Resources | ||
|
||
- [tc39/proposal-pipeline-operator](https://github.com/tc39/proposal-pipeline-operator) | ||
|
||
- [Hack Pipe Operator](https://docs.hhvm.com/hack/expressions-and-operators/pipe) |
Oops, something went wrong.