Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ReaderEither & new Either utils
- Loading branch information
Showing
25 changed files
with
277 additions
and
61 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,29 @@ | ||
import { Either, left, right } from '../either' | ||
|
||
/** | ||
* Creates a function similar to {@link ./from-predicate.ts} but curried for better composition. | ||
* This function returns a new function that takes a value of type `T` and applies | ||
* the provided predicate function. If the predicate succeeds, it returns a right | ||
* containing the original value. If the predicate fails, it returns a left containing | ||
* the result of the provided error function `c`. | ||
* | ||
* @param predicate The predicate function to determine if the value | ||
* should result in a right or left. | ||
* @param c The function returning the error value in case the predicate fails. | ||
* @returns A curried function taking a value of type `T` and returning | ||
* an Either, with a right containing the original value if the predicate succeeds, or a left | ||
* containing the error value if the predicate fails. | ||
* | ||
* @example | ||
* const predicator = fromPredicateC( | ||
* (number: number) => number > 10 && number < 15, | ||
* (n) => `${n} is greater than 15 or lower than 10.`, | ||
* ) | ||
* | ||
* predicator(11) // Right containing 11 | ||
* predicator(100) // Left containing `100 is greater than 15 or lowe than 10` | ||
*/ | ||
export const fromPredicateC = | ||
<T, E>(predicate: (v: T) => boolean, c: (v: T) => E) => | ||
(v: T): Either<E, T> => | ||
predicate(v) ? right(v) : left(c(v)) |
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 @@ | ||
import { Either, left, right } from '../either' | ||
|
||
/** | ||
* Creates an Either based on the result of a predicate function. | ||
* If the predicate function returns true for the provided value, returns a right | ||
* containing the original value. If the predicate function returns false, | ||
* returns a left containing the result of the provided error function. | ||
* | ||
* @template T The type of the value being evaluated. | ||
* @template E The type of the error value. | ||
* @param {T} v The value to be evaluated by the predicate function. | ||
* @param {function(T): boolean} predicate The predicate function to determine if the value | ||
* should result in a right or left. | ||
* @param {function(T): E} c The function returning the error value in case the predicate fails. | ||
* @returns {Either<E, T>} An Either instance, with a right containing the original value | ||
* if the predicate succeeds, or a left containing the error value if the predicate fails. | ||
* | ||
* @example | ||
* // Example usage: | ||
* const either = fromPredicate( | ||
* 10, | ||
* (num) => num < 2, | ||
* (v) => `${v} is greater than 2.`, | ||
* ); | ||
* | ||
* either.fold(console.error, console.log); // Outputs: "10 is greater than 2." | ||
*/ | ||
export const fromPredicate = <T, E>( | ||
v: T, | ||
predicate: (v: T) => boolean, | ||
c: (v: T) => E, | ||
): Either<E, T> => (predicate(v) ? right(v) : left(c(v))) |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './multiple' | ||
export * from './side' |
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,6 @@ | ||
/** | ||
* helps to debug many .map calls | ||
*/ | ||
export const side = | ||
<T>(f: (v: T) => void) => | ||
(v: T) => (f(v), v) |
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,2 @@ | ||
export * from './reader-either' | ||
export * from './utils' |
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,24 @@ | ||
import { Either, isRight, left } from '->/either' | ||
|
||
export interface ReaderEither<E, L, R> { | ||
run: (e: E) => Either<L, R> | ||
map: <Re>(f: (v: R) => Re) => ReaderEither<E, L, Re> | ||
flatMap: <Re>( | ||
f: (v: R) => ReaderEither<E, L, Re>, | ||
) => ReaderEither<E, L, Re> | ||
} | ||
|
||
export const of = <E, L, R>( | ||
run: (e: E) => Either<L, R>, | ||
): ReaderEither<E, L, R> => ({ | ||
run, | ||
map: <Re>(f: (v: R) => Re): ReaderEither<E, L, Re> => | ||
of((e) => run(e).map(f)), | ||
flatMap: <Re>( | ||
f: (v: R) => ReaderEither<E, L, Re>, | ||
): ReaderEither<E, L, Re> => | ||
of((e) => { | ||
const a = run(e) | ||
return isRight(a) ? f(a.right).run(e) : left(a.left) | ||
}), | ||
}) |
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 @@ | ||
export * from './try-catch' |
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 @@ | ||
import { left, right } from '->/either' | ||
import { ReaderEither, of } from '->/reader-either' | ||
|
||
export const tryCatch = <E, T, M>( | ||
tryFn: (e: E) => T, | ||
catchFn: (e: unknown) => M, | ||
): ReaderEither<E, M, T> => | ||
of((e) => { | ||
try { | ||
return right(tryFn(e)) | ||
} catch (err) { | ||
return left(catchFn(err)) | ||
} | ||
}) |
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,3 @@ | ||
export interface Predicate<T> { | ||
(v: T): boolean | ||
} |
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,3 @@ | ||
export interface Refinement<T, B extends T> { | ||
(v: T): v is B | ||
} |
Oops, something went wrong.