Skip to content

Commit

Permalink
rewrite fp-ts-to-the-max using pipe and latest APIs, closes #1065
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Dec 25, 2019
1 parent 435f763 commit 5e664ad
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 104 deletions.
98 changes: 59 additions & 39 deletions tutorials/fp-ts-to-the-max-I.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import { createInterface } from 'readline'
import { sequenceS } from '../src/Apply'
import { log } from '../src/Console'
import { fold, none, Option, some } from '../src/Option'
import { flow } from '../src/function'
import * as O from '../src/Option'
import { pipe } from '../src/pipeable'
import { randomInt } from '../src/Random'
import { Task, task } from '../src/Task'
import * as T from '../src/Task'

//
// helpers
//

const getStrLn: Task<string> = () =>
// read from standard input
const getStrLn: T.Task<string> = () =>
new Promise(resolve => {
const rl = createInterface({
input: process.stdin,
Expand All @@ -20,60 +24,76 @@ const getStrLn: Task<string> = () =>
})
})

const putStrLn = (message: string): Task<void> => task.fromIO(log(message))
// write to standard output
const putStrLn = flow(log, T.fromIO)

const random = task.fromIO(randomInt(1, 5))
// ask something and get the answer
function ask(question: string): T.Task<string> {
return pipe(
putStrLn(question),
T.chain(() => getStrLn)
)
}

// get a random int between 1 and 5
const random = T.fromIO(randomInt(1, 5))

const parse = (s: string): Option<number> => {
// parse a string to an integer
function parse(s: string): O.Option<number> {
const i = +s
return isNaN(i) || i % 1 !== 0 ? none : some(i)
return isNaN(i) || i % 1 !== 0 ? O.none : O.some(i)
}

//
// game
//

const checkContinue = (name: string): Task<boolean> => {
const put = putStrLn(`Do you want to continue, ${name}?`)
const get = task.chain(put, () => getStrLn)
const answer = task.chain(get, answer => {
switch (answer.toLowerCase()) {
case 'y':
return task.of(true)
case 'n':
return task.of(false)
default:
return checkContinue(name)
}
})
return answer
function shouldContinue(name: string): T.Task<boolean> {
return pipe(
ask(`Do you want to continue, ${name} (y/n)?`),
T.chain(answer => {
switch (answer.toLowerCase()) {
case 'y':
return T.of(true)
case 'n':
return T.of(false)
default:
return shouldContinue(name)
}
})
)
}

const parseFailureMessage = putStrLn('You did not enter an integer!')
// run `n` tasks in parallel
const ado = sequenceS(T.task)

const gameLoop = (name: string): Task<void> =>
task.chain(random, secret => {
const game = task.chain(putStrLn(`Dear ${name}, please guess a number from 1 to 5`), () =>
task.chain(getStrLn, guess =>
fold(
() => parseFailureMessage,
function gameLoop(name: string): T.Task<void> {
return pipe(
ado({
secret: random,
guess: ask(`Dear ${name}, please guess a number from 1 to 5`)
}),
T.chain(({ secret, guess }) =>
pipe(
parse(guess),
O.fold(
() => putStrLn('You did not enter an integer!'),
x =>
x === secret
? putStrLn(`You guessed right, ${name}!`)
: putStrLn(`You guessed wrong, ${name}! The number was: ${secret}`)
)(parse(guess))
)
)
)
const doContinue = task.chain(game, () => checkContinue(name))
return task.chain(doContinue, shouldContinue => (shouldContinue ? gameLoop(name) : task.of(undefined)))
})

const nameMessage = putStrLn('What is your name?')

const askName = task.chain(nameMessage, () => getStrLn)
),
T.chain(() => shouldContinue(name)),
T.chain(b => (b ? gameLoop(name) : T.of(undefined)))
)
}

const main: Task<void> = task.chain(askName, name =>
task.chain(putStrLn(`Hello, ${name} welcome to the game!`), () => gameLoop(name))
const main: T.Task<void> = pipe(
ask('What is your name?'),
T.chainFirst(name => putStrLn(`Hello, ${name} welcome to the game!`)),
T.chain(gameLoop)
)

// tslint:disable-next-line: no-floating-promises
Expand Down
158 changes: 93 additions & 65 deletions tutorials/fp-ts-to-the-max-II.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,14 @@
import { log } from '../src/Console'
import { Kind, URIS } from '../src/HKT'
import { none, Option, some, fold } from '../src/Option'
import * as O from '../src/Option'
import { randomInt } from '../src/Random'
import * as T from '../src/Task'
import { createInterface } from 'readline'
import { State, state } from '../src/State'
import { Monad1 } from '../src/Monad'

//
// helpers
//

const getStrLn: T.Task<string> = () =>
new Promise(resolve => {
const rl = createInterface({
input: process.stdin,
output: process.stdout
})
rl.question('> ', answer => {
rl.close()
resolve(answer)
})
})

const putStrLn = (message: string): T.Task<void> => T.task.fromIO(log(message))

const parse = (s: string): Option<number> => {
const i = +s
return isNaN(i) || i % 1 !== 0 ? none : some(i)
}
import { flow } from '../src/function'
import { pipe, pipeable } from '../src/pipeable'
import { sequenceS } from '../src/Apply'

//
// type classes
Expand All @@ -55,65 +35,108 @@ interface Main<F extends URIS> extends Program<F>, Console<F>, Random<F> {}

const programTask: Program<T.URI> = {
...T.task,
finish: T.task.of
finish: T.of
}

// read from standard input
const getStrLn: T.Task<string> = () =>
new Promise(resolve => {
const rl = createInterface({
input: process.stdin,
output: process.stdout
})
rl.question('> ', answer => {
rl.close()
resolve(answer)
})
})

// write to standard output
const putStrLn = flow(log, T.fromIO)

const consoleTask: Console<T.URI> = {
putStrLn,
getStrLn
}

const randomTask: Random<T.URI> = {
nextInt: upper => T.task.fromIO(randomInt(1, upper))
nextInt: upper => T.fromIO(randomInt(1, upper))
}

//
// game
//

const checkContinue = <F extends URIS>(F: Program<F> & Console<F>) => (name: string): Kind<F, boolean> => {
const put = F.putStrLn(`Do you want to continue, ${name}?`)
const get = F.chain(put, () => F.getStrLn)
const answer = F.chain(get, answer => {
switch (answer.toLowerCase()) {
case 'y':
return F.of(true)
case 'n':
return F.of(false)
default:
return checkContinue(F)(name)
}
})
return answer
// parse a string to an integer
function parse(s: string): O.Option<number> {
const i = +s
return isNaN(i) || i % 1 !== 0 ? O.none : O.some(i)
}

const gameLoop = <F extends URIS>(F: Main<F>) => (name: string): Kind<F, void> => {
const parseFailureMessage = F.putStrLn('You did not enter an integer!')

return F.chain(F.nextInt(5), secret => {
const game = F.chain(F.putStrLn(`Dear ${name}, please guess a number from 1 to 5`), () =>
F.chain(F.getStrLn, guess =>
fold(
() => parseFailureMessage,
x =>
x === secret
? F.putStrLn(`You guessed right, ${name}!`)
: F.putStrLn(`You guessed wrong, ${name}! The number was: ${secret}`)
)(parse(guess))
)
function main<F extends URIS>(F: Main<F>): Kind<F, void> {
// run `n` tasks in parallel
const ado = sequenceS(F)

const { chain, chainFirst } = pipeable(F)

// ask something and get the answer
const ask = (question: string): Kind<F, string> =>
pipe(
F.putStrLn(question),
chain(() => F.getStrLn)
)
const doContinue = F.chain(game, () => checkContinue(F)(name))
return F.chain(doContinue, shouldContinue => (shouldContinue ? gameLoop(F)(name) : F.of<void>(undefined)))
})
}

const main = <F extends URIS>(F: Main<F>): Kind<F, void> => {
const nameMessage = F.putStrLn('What is your name?')
const askName = F.chain(nameMessage, () => F.getStrLn)
return F.chain(askName, name => F.chain(F.putStrLn(`Hello, ${name} welcome to the game!`), () => gameLoop(F)(name)))
const shouldContinue = (name: string): Kind<F, boolean> => {
return pipe(
ask(`Do you want to continue, ${name} (y/n)?`),
chain(answer => {
switch (answer.toLowerCase()) {
case 'y':
return F.of<boolean>(true)
case 'n':
return F.of<boolean>(false)
default:
return shouldContinue(name)
}
})
)
}

const gameLoop = (name: string): Kind<F, void> => {
return pipe(
ado({
secret: F.nextInt(5),
guess: ask(`Dear ${name}, please guess a number from 1 to 5`)
}),
chain(({ secret, guess }) =>
pipe(
parse(guess),
O.fold(
() => F.putStrLn('You did not enter an integer!'),
x =>
x === secret
? F.putStrLn(`You guessed right, ${name}!`)
: F.putStrLn(`You guessed wrong, ${name}! The number was: ${secret}`)
)
)
),
chain(() => shouldContinue(name)),
chain(b => (b ? gameLoop(name) : F.of<void>(undefined)))
)
}

return pipe(
ask('What is your name?'),
chainFirst(name => F.putStrLn(`Hello, ${name} welcome to the game!`)),
chain(gameLoop)
)
}

export const mainTask = main({ ...programTask, ...consoleTask, ...randomTask })
export const mainTask = main({
...programTask,
...consoleTask,
...randomTask
})

// tslint:disable-next-line: no-floating-promises
// mainTask.run()
Expand Down Expand Up @@ -171,7 +194,12 @@ const randomTest: Random<URI> = {
}
}

const mainTestTask = main({ ...programTest, ...consoleTest, ...randomTest })
const mainTestTask = main({
...programTest,
...consoleTest,
...randomTest
})

const testExample = new TestData(['Giulio', '1', 'n'], [], [1])

import * as assert from 'assert'
Expand All @@ -185,7 +213,7 @@ assert.deepStrictEqual(mainTestTask(testExample), [
'Hello, Giulio welcome to the game!',
'Dear Giulio, please guess a number from 1 to 5',
'You guessed right, Giulio!',
'Do you want to continue, Giulio?'
'Do you want to continue, Giulio (y/n)?'
],
[]
)
Expand Down

0 comments on commit 5e664ad

Please sign in to comment.