Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to interrupt, exit a compose or pipe?

What's the proper way to interrupt a long chain of compose or pipe functions ?

Let's say the chain doesn't need to run after the second function because it found an invalid value and it doesn't need to continue the next 5 functions as long as user submitted value is invalid.

Do you return an undefined / empty parameter, so the rest of the functions just check if there is no value returned, and in this case just keep passing the empty param ?

like image 939
Robert Brax Avatar asked Jul 29 '16 16:07

Robert Brax


2 Answers

I don't think there is a generic way of dealing with that.

Often when working with algebraic data types, things are defined so that you can continue to run functions even when the data you would prefer is not present. This is one of the extremely useful features of types such as Maybe and Either for instance.

But most versions of compose or related functions don't give you an early escape mechanism. Ramda's certainly doesn't.

like image 100
Scott Sauyet Avatar answered Sep 21 '22 14:09

Scott Sauyet


While you can't technically exit a pipeline, you can use pipe() within a pipeline, so there's no reason why you couldn't do something like below to 'exit' (return from a pipeline or kick into another):

pipe(
  // ... your pipeline 
  propOr(undefined, 'foo'), // - where your value is missing
  ifElse(isNil, always(undefined), pipe(
    // ...the rest of your pipeline
  ))
)
like image 27
MorganIsBatman Avatar answered Sep 20 '22 14:09

MorganIsBatman