Skip to content

Commit

Permalink
refactor(qio): rename flattenM to tryM
Browse files Browse the repository at this point in the history
affects: @qio/core, @qio/example

BREAKING CHANGE:
rename \`flattenM\` to \`tryM\`
  • Loading branch information
tusharmath committed Nov 25, 2019
1 parent 0d1d23d commit b5aa169
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 29 deletions.
2 changes: 1 addition & 1 deletion packages/core/__tests__/Program.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('Program', () => {

return {
getStrLn: (question: string) =>
QIO.flattenM(() => {
QIO.tryM(() => {
const popped = input.hasOwnProperty(question)
? input[question].shift()
: undefined
Expand Down
4 changes: 2 additions & 2 deletions packages/core/lib/main/Await.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class Await<A, E> {
private readonly Q = DoublyLinkedList.of<[CB<A>, CB<E>]>()
private result: Option<Either<E, A>> = Option.none()
public get get(): QIO<A, E> {
return QIO.flattenM(() =>
return QIO.tryM(() =>
this.result
.map(S => S.reduce<QIO<A, E>>(QIO.reject, XX => QIO.resolve(XX)))
.getOrElse(this.wait)
Expand All @@ -29,7 +29,7 @@ export class Await<A, E> {
return QIO.lift(() => this.flag)
}
public set(io: QIO<A, E>): QIO<boolean> {
return QIO.flattenM(() => {
return QIO.tryM(() => {
if (this.flag) {
return QIO.resolve(false)
}
Expand Down
44 changes: 21 additions & 23 deletions packages/core/lib/main/QIO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,29 +204,6 @@ export class QIO<A1 = unknown, E1 = never, R1 = unknown> {
): QIO<A2, E1 | E2, R1 & R2> {
return qio.chain(Id)
}
/**
* Takes in a effect-ful function that return a c and unwraps it.
* This is an alias to `c.flatten(QIO.lift(fn))`
*
* ```ts
* // An impure function that creates mutable state but also returns a c.
* const FN = () => {
* let count = 0
*
* return c.try(() => count++)
* }
* // Using flatten
* c.flatten(QIO.lift(FN))
*
* // Using flattenM
* c.flattenM(FN)
* ```
*/
public static flattenM<A1, E1, R1>(
qio: () => QIO<A1, E1, R1>
): QIO<A1, E1, R1> {
return QIO.flatten(QIO.lift(qio))
}
/**
* Creates a new [[Fiber]] to run the given [[QIO]].
*/
Expand Down Expand Up @@ -430,6 +407,27 @@ export class QIO<A1 = unknown, E1 = never, R1 = unknown> {
public static try<A>(cb: () => A): QIO<A, Error> {
return QIO.lift(cb)
}
/**
* Takes in a effect-ful function that return a c and unwraps it.
* This is an alias to `c.flatten(QIO.lift(fn))`
*
* ```ts
* // An impure function that creates mutable state but also returns a c.
* const FN = () => {
* let count = 0
*
* return c.try(() => count++)
* }
* // Using flatten
* c.flatten(QIO.lift(FN))
*
* // Using flattenM
* c.tryM(FN)
* ```
*/
public static tryM<A1, E1, R1>(qio: () => QIO<A1, E1, R1>): QIO<A1, E1, R1> {
return QIO.flatten(QIO.lift(qio))
}
/**
* Tries to run an function that returns a promise.
*/
Expand Down
4 changes: 2 additions & 2 deletions packages/core/lib/main/Queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class Queue<A = never> {
* Pulls an item from the queue
*/
public get take(): QIO<A> {
return QIO.flattenM(() => {
return QIO.tryM(() => {
const sz = this.Q.shift()
if (Option.isSome(sz)) {
return QIO.resolve(sz.value)
Expand Down Expand Up @@ -60,7 +60,7 @@ export class Queue<A = never> {
* Inserts an item into the queue
*/
public offer(a: A): QIO<void> {
return QIO.flattenM(
return QIO.tryM(
(): QIO<void> => {
if (this.T.length === 0) {
this.Q.add(a)
Expand Down
2 changes: 1 addition & 1 deletion packages/example/__tests__/Program.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {canContinue, program} from '../guess-the-number/src/Program'

describe('Program', () => {
const input = <T>(...arr: T[]): QIO<T> =>
QIO.flattenM(
QIO.tryM(
(): QIO<T> => {
const elm = arr.shift()

Expand Down

0 comments on commit b5aa169

Please sign in to comment.