-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(getOrElse): Makes getOrElse lazy
BREAKING CHANGE: getOrElse is now lazy. To get the old getOrElse behavior, use getOrElseValue. Also, the types are now much more explicit, which should lead to fewer typing misses in client code.
- Loading branch information
Showing
9 changed files
with
175 additions
and
126 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,50 @@ | ||
import Catamorphism from "./Catamorphism"; | ||
import Result from "./Result"; | ||
import Catamorphism from './Catamorphism'; | ||
import Result from './Result'; | ||
|
||
class Err<E> extends Result<E, any> { | ||
class Err<E, A> extends Result<E, A> { | ||
private error: E; | ||
constructor(theError: E) { | ||
super(); | ||
this.error = theError; | ||
} | ||
|
||
public getOrElse<A>(defaultValue: A): A { | ||
public getOrElse(fn: () => A): A { | ||
return fn(); | ||
} | ||
|
||
public getOrElseValue(defaultValue: A): A { | ||
return defaultValue; | ||
} | ||
|
||
public map<B>(fn: (_: any) => B): Result<E, B> { | ||
return this as Result<E, B>; | ||
public map<B>(fn: (_: A) => B): Result<E, B> { | ||
return new Err<E, B>(this.error); | ||
} | ||
|
||
public mapError<X>(fn: (err: E) => X): Result<X, any> { | ||
public mapError<X>(fn: (err: E) => X): Result<X, A> { | ||
return new Err(fn(this.error)); | ||
} | ||
|
||
public andThen(fn: (_: any) => Result<E, any>): Result<E, any> { | ||
return this as Result<E, any>; | ||
public andThen<B>(fn: (_: A) => Result<E, B>): Result<E, B> { | ||
return new Err<E, B>(this.error); | ||
} | ||
|
||
public orElse<X>(fn: (err: E) => Result<X, any>): Result<X, any> { | ||
public orElse<X>(fn: (err: E) => Result<X, A>): Result<X, A> { | ||
return fn(this.error); | ||
} | ||
|
||
public cata<B>(matcher: Catamorphism<E, any, B>): B { | ||
public cata<B>(matcher: Catamorphism<E, A, B>): B { | ||
return matcher.Err(this.error); | ||
} | ||
|
||
public ap(result: Result<E, any>): Result<E, any> { | ||
return this as Result<E, any>; | ||
public ap<B, C>(result: Result<E, B>): Result<E, C> { | ||
return new Err<E, C>(this.error); | ||
} | ||
} | ||
|
||
/** | ||
* A convenience function for creating a new Err. | ||
*/ | ||
function err<E>(e: E) { return new Err(e); } | ||
const err = <E, A>(e: E): Result<E, A> => new Err<E, A>(e); | ||
|
||
export default Err; | ||
export { 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,54 @@ | ||
import Catamorphism from "./Catamorphism"; | ||
import Result from "./Result"; | ||
import Catamorphism from './Catamorphism'; | ||
import Result from './Result'; | ||
|
||
class Ok<A> extends Result<any, A> { | ||
class Ok<Err, A> extends Result<Err, A> { | ||
private value: A; | ||
constructor(theValue: A) { | ||
super(); | ||
this.value = theValue; | ||
} | ||
|
||
public getOrElse(_: A): A { | ||
public getOrElse(fn: () => A): A { | ||
return this.value; | ||
} | ||
|
||
public map<B>(fn: (a: A) => B): Result<any, B> { | ||
public getOrElseValue(_: A): A { | ||
return this.value; | ||
} | ||
|
||
public map<B>(fn: (a: A) => B): Result<Err, B> { | ||
return new Ok(fn(this.value)); | ||
} | ||
|
||
public mapError<X>(fn: (e: any) => any): Result<any, A> { | ||
return this as Result<any, A>; | ||
public mapError<X>(fn: (e: Err) => X): Result<X, A> { | ||
return new Ok<X, A>(this.value); | ||
} | ||
|
||
public andThen<B>(fn: (a: A) => Ok<B>): Result<any, B> { | ||
return fn(this.value) as Result<any, B>; | ||
public andThen<B>(fn: (a: A) => Result<Err, B>): Result<Err, B> { | ||
return fn(this.value); | ||
} | ||
|
||
public orElse(fn: (_: any) => Result<any, A>): Result<any, A> { | ||
return this as Result<any, A>; | ||
} | ||
|
||
public cata<B>(matcher: Catamorphism<any, A, B>): B { | ||
public cata<B>(matcher: Catamorphism<Err, A, B>): B { | ||
return matcher.Ok(this.value); | ||
} | ||
|
||
public ap<B, C>(result: Result<any, B>): Result<any, C> { | ||
if (typeof this.value !== "function") { | ||
public ap<B, C>(result: Result<Err, B>): Result<Err, C> { | ||
if (typeof this.value !== 'function') { | ||
throw new TypeError(`'ap' can only be applied to functions: ${JSON.stringify(this.value)}`); | ||
} | ||
|
||
return result.map(this.value) as Result<any, C>; | ||
return result.map(this.value); | ||
} | ||
} | ||
|
||
/** | ||
* A convenience function for create a new Ok. | ||
*/ | ||
function ok<T>(v: T) { return new Ok(v); } | ||
const ok = <Err, T>(v: T): Result<Err, T> => new Ok(v); | ||
|
||
export default Ok; | ||
export { ok }; |
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,6 +1,6 @@ | ||
import Catamorphism from "./Catamorphism"; | ||
import Err, { err } from "./Err"; | ||
import Ok, { ok } from "./Ok"; | ||
import Result from "./Result"; | ||
import Catamorphism from './Catamorphism'; | ||
import Err, { err } from './Err'; | ||
import Ok, { ok } from './Ok'; | ||
import Result from './Result'; | ||
|
||
export { Catamorphism, Err, err, Ok, ok, Result }; |
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 |
---|---|---|
@@ -1,13 +1,13 @@ | ||
{ | ||
"defaultSeverity": "error", | ||
"extends": [ | ||
"tslint:recommended" | ||
], | ||
"jsRules": {}, | ||
"rules": { | ||
"interface-name": false, | ||
"max-classes-per-file": false, | ||
"arrow-parens": [true, "ban-single-arg-parens"] | ||
}, | ||
"rulesDirectory": [] | ||
"defaultSeverity": "error", | ||
"extends": [ "tslint:recommended" ], | ||
"jsRules": {}, | ||
"rules": { | ||
"interface-name": false, | ||
"max-classes-per-file": false, | ||
"arrow-parens": [ true, "ban-single-arg-parens" ], | ||
"quotemark": [ true, "single", "avoid-escape" ], | ||
"ordered-imports": false | ||
}, | ||
"rulesDirectory": [] | ||
} |
Oops, something went wrong.