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
}
}
@glcraft When you have time, would you please look into adding support for:
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.
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.
need to adopt new nushell syntax with input output type parameters. Here's an example of a script.
@glcraft When you have time, would you please look into adding support for:
[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.let replacements =
line. we used to require parens likelet replacements = (blah blah blah)
and now we don't. not sure if there's specific syntax for that or not.