Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

function ideas #184

Open
richytong opened this issue Mar 10, 2021 · 3 comments
Open

function ideas #184

richytong opened this issue Mar 10, 2021 · 3 comments

Comments

@richytong
Copy link
Collaborator

richytong commented Mar 10, 2021

flatMap.series
flatMap.pool
transform.parallel
transform.pool
transform.while
reduce.parallel
reduce.pool
reduce.while
pipe.chain

Your suggestions are welcome!

@richytong richytong pinned this issue Mar 10, 2021
@richytong richytong changed the title 1.7 1.8 Jul 7, 2021
@richytong richytong changed the title 1.8 function ideas Sep 10, 2021
@phhu
Copy link

phhu commented Mar 25, 2022

unfold would be nice. (see https://stackoverflow.com/questions/71605121/implementation-of-async-unfold-function-in-javascript / https://stackoverflow.com/questions/49542944/loop-until-with-ramda )

const res = await asyncUnfoldExclusive(x=>Promise.resolve(x+1), x => x<6, 2) 
console.log(res)  // [3,4,5]

@ivklgn
Copy link
Contributor

ivklgn commented Jul 20, 2022

@richytong yo!
after fp-ts in rubico i dont fully understand alternation in pipe.

in fp-ts we can generalize computation by Either monad. for example:

pipe(
  E.right(1),
  E.map((val) => 1), // output: 1
  E.mapLeft(() => 'err value')), // not achievable in this case
)

and this is good way for handling errors and ellegant break computation, but its about special data structure.

in rubico i have when/unless/switchCase and raw structure without error signs (tags). and my code without either structure start expand with additional logic.

@richytong what you think about controlling flow in rubico and either monad in fp? maybe you can highlight solutions for this cases. thanks!

@richytong
Copy link
Collaborator Author

@kalagin Either monad is not a replacement for alternation, it is only a replacement for error handling with imperative try/catch syntax. rubico's tryCatch is the function equivalent to the pure functional programming Either monad. With tryCatch you can still throw an error and have it be caught in an error handler, which is similar to calling E.left on an Either monad and then calling E.match to handle the error.

To your example:

// rubico
tryCatch(1, val => 1, () => 'err value')

// fp-ts
pipe(
  E.right(1),
  E.map((val) => 1), // output: 1
  E.mapLeft(() => 'err value')), // not achievable in this case
)

A toy http handler with tryCatch

const myHandler = (request, response) => tryCatch(request, pipe([
  request => parseRequestBody(request),
  data => myDb.get(data.userId),
  user => {
    response.end(JSON.stringify(user))
  },
]), error => {
  console.error(error)
  response.end(error.message)
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants