nushell / vscode-nushell-lang

A Nushell grammar for Visual Studio Code with IDE support
https://www.nushell.sh/
MIT License
122 stars 27 forks source link

support new nushell syntax with input/output types and no need for parens on assignment #146

Closed fdncred closed 1 year ago

fdncred commented 1 year ago

need to adopt new nushell syntax with input output type parameters. Here's an example of a script.

# key points:
#     - 4-space indentation
#     - full type annotation
#     - no parentheses in `let` declaration
#     - no `for` loop
#     - no mutation
#     - use of `uniq`, `enumerate` and `insert` to build the replacement table at once
#     - use of `reduce` as proposed by @fdncred to simplify the last loop
#     - implicite return value
#     - unit test with the standard library
def anonymize [pattern: string]: string -> string {
    let input = $in

    let replacements = $input
        | parse --regex $"\(($pattern)\)"
        | get capture0
        | uniq
        | enumerate
        | insert new {|it| $"anon($it.index)"}

    $replacements | reduce --fold $input {|it, acc|
        $acc | str replace --all --string $it.item $it.new
    }
}
use std assert
assert equal ("hello *** world, world" | anonymize '\w+') "anon0 *** anon1, anon1"

@glcraft When you have time, would you please look into adding support for:

  1. input output parameters, which you see above as string -> string. they can also appear in a list like [string -> string, string -> int, string -> bool] i think the commas are optional and not the color : before the input output parameters. That's the trigger.
  2. notice the let replacements = line. we used to require parens like let replacements = (blah blah blah) and now we don't. not sure if there's specific syntax for that or not.
fdncred commented 1 year ago

closed by #160