seasonedcc / composable-functions

Types and functions to make composition easy and safe
MIT License
649 stars 13 forks source link

New sequenceAll combinator #127

Closed diogob closed 3 months ago

diogob commented 8 months ago

Use case

This fits a similar use case as the passthrough proposed on #96 It can be used to add validation functions to a pipeline that should abort execution on error but don't necessarly produce a result that we want to pipe to the next callee.

For instance, let's say you want to check the state of a record in the database and send a notification only when the validation function is successful. In this case the resulting type of the validation function should not be taken into account, since you don't want to write the function for the composition.

With the new combinator something like this could be done:

const isValidInDatabase = composable(id: string) => { ... }
const sendNotification = composable(id: string) => { ... }
const sendNotificationWhenValid = sequenceAll(isValidInDatabase, sendNotification)

It also makes it easier to reuse existing functions just for the sake of their side-effects. On the previous example, let's say you want add a function that records a log of messages sent on the database (and fail to send the message in case the log fails).

I was hoping to merge this before we have the full composable documentation.

TODO

gustavoguichard commented 8 months ago

@diogob , I'm good with having it but the name doesn't do it for me... hard to tell what it does, especially because we have sequence and all and it has nothing to do with them. WDYT?

diogob commented 8 months ago

@diogob , I'm good with having it but the name doesn't do it for me... hard to tell what it does, especially because we have sequence and all and it has nothing to do with them. WDYT?

I actually derived the name from both, since it takes the typing of all but most of the implementation from sequence. You could say that this is a serial version of the all combinator.

Other possible names along these lines would be:

gustavoguichard commented 5 months ago

tap?