Civet icon indicating copy to clipboard operation
Civet copied to clipboard

Non-null pipe `?|>`

Open edemaine opened this issue 1 year ago • 2 comments
trafficstars

@STRd6 suggests the ability to short-circuit the rest of a pipe when the left-hand side is null:

x ?|> f
---
x != null ? f(x) : x
//or
x == null ? x : f(x)

edemaine avatar Dec 28 '23 19:12 edemaine

A possible refinement/expansion could be ?> and !> as pipe-if and pipe-unless.

foo := try line1() !> return
bar := try line2() !> return
...
vvv
let ref; try(){ref = line1()}catch(e){}; if(!ref) return ref; const foo = ref;

It passes the value to the pipe depending on the condition, then returns the value if not passed or the result of the of the pipe if it was passed.

It may need some finessing but that's the general idea.

Something else to consider could be a error/non-error pipe as well. It could pass if the value extends Error.

Some further questions:

  • Are non-null and falsey different enough to warrant different pipes?
  • Can we combine these easily? (pipe null or error, falsey or error, etc.)

👨‍🍳

STRd6 avatar Mar 17 '24 16:03 STRd6

Are non-null and falsey different enough to warrant different pipes?

There are four distinct falsy values a number? may have, but only one of them is nullish. (The other three are 0, -0, and NaN.)

bbrk24 avatar Mar 17 '24 16:03 bbrk24