Comments (4)
I'm not sure about that paren operator, but I'd definitely like to support standard JS destructuring:
[1,2] |[a,b]> a + b // 3
["head","tail","tail"] |[h,...t]> [h,t] // ["head",["tail","tail"]]
[1,2,10] |$> $[0]+$[2] // 11
{a:1,b:2,c:10} |{a,b}> a+b // 3
Admittedly |x>
looks a little unwieldly when it grows to a destructuring pattern.
This notation is mentioned in tc39/proposal-pipeline-operator#91 (comment) (more examples here: tc39/proposal-pipeline-operator#91 (comment), tc39/proposal-pipeline-operator#91 (comment)) so it's plausibly being considered for the TC39 proposal. Although there are hundreds of proposals in that issue, so who knows what they will decide.
@STRd6 What do you think of this notation? Worth trying? It is a little funny that |x>
is just shorthand for |> (x) =>
, but it is shorter, and it will be easy to implement.
from civet.
The TC39 pipe proposal actually uses %
currently, though there's various other notation mentioned. (I think ^^
is an older proposal. Note that it currently means logical xor
in Civet, though that might be fine parsing wise, just confusing.)
TC39's %
placeholder also has a different meaning than our current placeholders .
and &
: it can only be used in pipelines, and the arrow function gets pulled all the way out to just before the pipeline. See #75 (comment)
It's interesting to look at the examples from the proposal. All of them currently can be written using .
and/or &
, but it depends on the example. Here's one example that needs both:
return links
|> Object.keys(.).map(function (rel) {
return '<' + links[rel] + '>; rel="' + rel + '"';
})
|> link + &.join(', ')
|> this.set('Link', .);
Now that we have good general support for placeholders, I wonder if we should add a pipeline-specific one, in particular to align with the TC39 pipe proposal as you say. Simple example:
data |> f(%) + g(%)
↓↓↓
f(data) + g(data)
This example can't be written with &
(it won't pull outside the function calls) nor with .
(each call makes its own function). And in general it could be nice to work with pipelines without having to think about the intricacies of &
vs. .
, and just have a placeholder that refers to the previous pipeline result.
I think the only hard part is we need to choose a placeholder syntax. %
seems OK. ^
and <
, or ^^
or <<
, make some sense in the ways they point, though we've discussed using ^
for other things (implicit imports). I wonder if there's something obviously syntactically related to the pipeline |>
itself.
Related, another option we've discussed (also mentioned in the TC39 bikeshedding issue) is
data |x> f(x) + g(x)
↓↓↓
f(data) + g(data)
where you can put any identifier (e.g. $
) in between |
and >
.
from civet.
I think the last suggestion is really nice - it sidesteps the issue of needing to remember an explicit symbol out of the mix of candidates. $ (or x) could be the suggested placeholder.
It could even unpack an array or an object:
[1,2] |(a,b)> a + b //3
["head","tail","tail"] |(h,t)> [h,t] // ["head",["tail","tail"]]
[1,2,10] |$> $[0]+$[2] //11
{a:1,b:2,c:10} |(a,b)> a+b //3
Of course this gets away from the TC39 a bit...
from civet.
@edemaine if you're excited about trying |id>
it go for it! Destructuring in there is fine as well. We probably won't really know the pros and cons until we try out using it ourselves.
from civet.
Related Issues (20)
- Need full example setup civet on sveltekit HOT 2
- Regression in 0.7.11: No more implicit return in synchronous function that creates a Promise<void> HOT 1
- Pipe after implicit object is incorrect
- Trailing `or` on newline after callback function in implicit application parses as nested implicit function call instead of method body HOT 1
- bun + jsx syntax HOT 3
- vscode ext, add option to disable diagnostics HOT 1
- import extension gets rewritten but wrong HOT 1
- `[…asyncIterator]` sugar for `Array.fromAsync`? HOT 4
- `civet *.civet` file execution bug when reading config from any file
- import vs multiplication
- Indented chained ternarys fail to parse
- Array destructuring into @vars doesn't declare temporary variables HOT 1
- Problems with `node --test`s written in Civet (or possibly wider issue with ESM reading config)
- Generate normal `.d.ts` extension HOT 2
- Correctly load civet config file inner a workspace
- Solid Start file router
- Support for neovim/vim HOT 2
- `autoConst` doesn’t work with destructuring HOT 1
- Nested type annotation fails to parse HOT 2
- VSCode Intellisense not working HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from civet.