Skip to content

Commit

Permalink
started refactoring based on ES6 Iterables
Browse files Browse the repository at this point in the history
  • Loading branch information
bgrieder committed Mar 20, 2017
1 parent 436d957 commit d80ff40
Show file tree
Hide file tree
Showing 13 changed files with 1,198 additions and 146 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"chai": "^3.5.0",
"mocha": "^3.2.0",
"source-map-support": "^0.4.12",
"ts-node": "^2.1.0",
"typescript": "^2.2.1"
}
}
5 changes: 4 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
"sourceMap": true,
"experimentalDecorators": true,
"suppressImplicitAnyIndexErrors": true,
"alwaysStrict": true,
// "strictNullChecks":true,
"lib": [
"dom",
"es2015.promise",
"es2015.Promise",
"es2015.Symbol",
"es5"
]
},
Expand Down
128 changes: 67 additions & 61 deletions typescript/API/Option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,25 @@ import {Seq} from './Seq'
export interface Option<A> extends Seq<A> {

/**
* Returns the Option value or throws an exception if there is none
* Identical to run()
*/
get(): A

/**
* Returns true is the Option is None, false otherwise
*/
isEmpty(): boolean

/**
* Returns the <code>Option</code>'s value if the option is nonempty, otherwise
* return the result of evaluating `elseVal`.
*/
getOrElse<U>( elseVal: () => U ): A|U

/**
* Returns the <code>Option</code>'s value if it is nonempty,
* or <code>null</code> if it is empty.
* Although the use of null is discouraged, code written to use
* <code>Option</code> must often interface with code that expects and returns nulls.
*/
getOrNull(): A

/**
* Returns the <code>Option</code>'s value if it is nonempty,
* or <code>undefined</code> if it is empty.
* Although the use of undefined is discouraged, code written to use
* <code>Option</code> must often interface with code that expects and returns undefined.
* Returns a <code>Some</code> containing the result of
* applying <code>someFn</code> to this <code>Option</code>'s contained
* value, '''if''' this option is
* nonempty '''and''' <code>someFn</code> is defined
* Returns <code>None</code> otherwise.
*/
getOrUndefined(): A
collect<U>( partialFunction: {someFn?: ( value: A ) => U} ): Option<U>

/**
* Returns a <code>Some</code> containing the result of applying <code>f</code> to this <code>Option</code>'s
* value if this <code>Option</code> is nonempty.
* Otherwise return <code>None</code>.
*
* @note This is similar to `flatMap` except here,
* <code>f</code> does not need to wrap its result in an <code>Option</code>.
* Returns true is these two options are equal
*/
map<U>( f: ( value: A ) => U ): Option<U>
equals( option: Option<A> ): boolean

/**
* Returns the result of applying <code>f</code>empty to this <code>Option</code>'s
* value if the <code>Option</code> is empty; otherwise, applies
* `f` to the value.
*
* @note This is equivalent to <code>Option.map(f).getOrElse(ifEmpty)</code>
* Returns true if this option is nonempty '''and''' the function
* <code>test</code> returns true when applied to this <code>Option</code>'s value.
* Otherwise, returns false.
*/
fold<U>( ifEmpty: () => U, f: ( value: A ) => U ): U
exists( test: ( value: A ) => boolean ): boolean

/**
* Returns this <code>Option</code> if it is nonempty '''and''' applying the function <code>test</code> to
Expand All @@ -64,10 +33,10 @@ export interface Option<A> extends Seq<A> {
filter( f: ( value: A ) => boolean ): Option<A>

/**
* Flattens two layers of <code>Option</code> into one
* More precisely flattens an Option<Iterable<A>> into an Option<A>
* Returns this <code>Option</code> if it is nonempty '''and''' applying the function <code>test</code> to
* this <code>Option</code>'s value returns false. Otherwise, return <code>None</code>.
*/
flatten<U>(): Option<U>
filterNot( test: ( value: A ) => boolean ): Option<A>

/**
* Returns the result of applying <code>f</code> to this <code>Option</code>'s value if
Expand All @@ -84,17 +53,19 @@ export interface Option<A> extends Seq<A> {
flatMap<U>( f: ( value: A ) => Option<U> ): Option<U>

/**
* Returns this <code>Option</code> if it is nonempty '''and''' applying the function <code>test</code> to
* this <code>Option</code>'s value returns false. Otherwise, return <code>None</code>.
* Flattens two layers of <code>Option</code> into one
* More precisely flattens an Option<Iterable<A>> into an Option<A>
*/
filterNot( test: ( value: A ) => boolean ): Option<A>
flatten<U>(): Option<U>

/**
* Returns true if this option is nonempty '''and''' the function
* <code>test</code> returns true when applied to this <code>Option</code>'s value.
* Otherwise, returns false.
* Returns the result of applying <code>f</code>empty to this <code>Option</code>'s
* value if the <code>Option</code> is empty; otherwise, applies
* `f` to the value.
*
* @note This is equivalent to <code>Option.map(f).getOrElse(ifEmpty)</code>
*/
exists( test: ( value: A ) => boolean ): boolean
fold<U>( ifEmpty: () => U, f: ( value: A ) => U ): U

/**
* Returns true if this option is empty '''or''' the function
Expand All @@ -113,13 +84,47 @@ export interface Option<A> extends Seq<A> {
forEach<U>( f: ( value: A ) => U ): void

/**
* Returns a <code>Some</code> containing the result of
* applying <code>someFn</code> to this <code>Option</code>'s contained
* value, '''if''' this option is
* nonempty '''and''' <code>someFn</code> is defined
* Returns <code>None</code> otherwise.
* Returns the Option value or throws an exception if there is none
* Identical to run()
*/
collect<U>( partialFunction: {someFn?: ( value: A ) => U} ): Option<U>
get(): A

/**
* Returns the <code>Option</code>'s value if the option is nonempty, otherwise
* return the result of evaluating `elseVal`.
*/
getOrElse<U>( elseVal: () => U ): A|U

/**
* Returns the <code>Option</code>'s value if it is nonempty,
* or <code>null</code> if it is empty.
* Although the use of null is discouraged, code written to use
* <code>Option</code> must often interface with code that expects and returns nulls.
*/
getOrNull(): A

/**
* Returns the <code>Option</code>'s value if it is nonempty,
* or <code>undefined</code> if it is empty.
* Although the use of undefined is discouraged, code written to use
* <code>Option</code> must often interface with code that expects and returns undefined.
*/
getOrUndefined(): A

/**
* Returns true is the Option is None, false otherwise
*/
isEmpty(): boolean

/**
* Returns a <code>Some</code> containing the result of applying <code>f</code> to this <code>Option</code>'s
* value if this <code>Option</code> is nonempty.
* Otherwise return <code>None</code>.
*
* @note This is similar to `flatMap` except here,
* <code>f</code> does not need to wrap its result in an <code>Option</code>.
*/
map<U>( f: ( value: A ) => U ): Option<U>

/**
* Returns this <code>Option</code> if it is nonempty,
Expand All @@ -128,9 +133,10 @@ export interface Option<A> extends Seq<A> {
orElse( alternative: () => Option<A> ): Option<A>

/**
* Returns true is these two options are equal
* Convert this Option to a Promise
* so that it can be chained using await
*/
equals( option: Option<A> ): boolean
toPromise: Promise<A>
}


Expand Down
113 changes: 113 additions & 0 deletions typescript/API/Try.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/**
* Created by Bruno Grieder.
*/
import {Seq} from './Seq'
import {Option} from './Option'

export interface Try<A> extends Seq<A | Error> {

/**
* Applies the given partial function to the value from this Success or returns this if this is a Failure.
*/
collect<U>( partialFunction: { someFn?: ( value: A ) => U } ): Try<U | A>

/**
* Returns true is these two options are equal
*/
equals( value: Try<A> ): boolean

/**
* Inverts this Try.
*/
failed: Try<Error>

/**
* Converts this to a Failure if the predicate is not satisfied.
*/
filter( f: ( value: A ) => boolean ): Try<A | Error>

/**
* Returns the given function applied to the value from this Success or returns this if this is a Failure.
*/
flatMap<U>( f: ( value: A ) => Try<U> ): Try<U | Error>

/**
* Transforms a nested Try, ie, a Try of type Try<Try<A>>, into an un-nested Try, ie, a Try of type Try<A>.
*/
flatten<U>(): Try<U | Error>

/**
* Applies fa if this is a Failure or fb if this is a Success.
*/
fold<U>( ifEmpty: () => U, f: ( value: A ) => U ): U

/**
* Applies the given function f if this is a Success, otherwise returns Unit if this is a Failure.
*/
forEach<U>( f: ( value: A ) => U ): void

/**
* Returns the value from this Success or throws the exception if this is a Failure.
*/
get(): A

/**
* Returns the value from this Success or the given default argument if this is a Failure.
*/
getOrElse<U>( elseVal: () => U ): A | U

/**
* Returns true if the Try is a Failure, false otherwise.
*/
isFailure: Boolean

/**
* Returns true if the Try is a Success, false otherwise.
*/
isSuccess: Boolean

/**
* Maps the given function to the value from this Success or returns this if this is a Failure.
*/
map<U>( f: ( value: A ) => U ): Try<U>

/**
* Returns this Try if it's a Success or the given default argument if this is a Failure.
*/
orElse( alternative: () => Try<A> ): Try<A>

/**
* Applies the given function f if this is a Failure, otherwise returns this if this is a Success.
*/
recover<U>( partialFunction: { someFn?: ( value: A ) => U } ): Try<U>

/**
* Applies the given function f if this is a Failure, otherwise returns this if this is a Success.
*/
recoverWith<U>( partialFunction: { someFn?: ( value: A ) => Try<U> } ): Try<U>

// /**
// * Returns Left with Throwable if this is a Failure, otherwise returns Right with Success value.
// */
// toEither: Either<Error, A>

/**
* Returns None if this is a Failure or a Some containing the value if this is a Success.
*/
toOption: Option<A>

/**
* Completes this Try by applying the function f to this if this is of type Failure, or conversely, by applying s if this is a Success.
*/
transform<U>( s: ( value: A ) => Try<U>, f: ( error: Error ) => Try<U> ): Try<U>

/**
* Convert this Option to a Promise
* so that it can be chained using await
*/
toPromise: Promise<A>

}

export interface Success<A> extends Try<A> {}
export interface Failure extends Try<Error> {}
Loading

0 comments on commit d80ff40

Please sign in to comment.