Civet
Civet copied to clipboard
Non-null pipe `?|>`
@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)
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.)
👨🍳
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.)