Skip to content

Commit

Permalink
fix vec fun bug on objects, improve vec fun docs
Browse files Browse the repository at this point in the history
Vectorized functions are supposed to be vectorized
    across their first argument.
This was previously true in general, except in the
    specific case where *the first argument was an
    object that was a function of input and at
    least one non-first argument was also a function
    of input.*
As is now noted in the docs, a good example of this
    is where the input is {"foo": "a", "bar": "cd"}
    and the query is s_mul(@, len(@))
    This query should return {"foo": "aa", "bar": "cdcd"}
    but previously this was bugged.
  • Loading branch information
molsonkiko committed Dec 31, 2023
1 parent de530f1 commit 889628d
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 46 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

1. The [`ifelse` vectorized function in RemesPath](/docs/RemesPath.md#vectorized-functions) now uses conditional execution.

### Fixed

1. Fixed issue where [vectorized functions in RemesPath](/docs/RemesPath.md#vectorized-functions) were not vectorized across objects if the first argument was a function of input and at least one of the non-first arguments was also a function of input.

## [6.1.1] - 2023-12-28

### Fixed
Expand Down
1 change: 1 addition & 0 deletions JsonToolsNppPlugin/JSONTools/RemesPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,7 @@ JNode arg_outfunc(JNode inp)
var dic = new Dictionary<string, JNode>(otbl.Length);
foreach (KeyValuePair<string, JNode> okv in otbl.children)
{
all_args[0] = okv.Value;
dic[okv.Key] = func.function.Call(all_args);
}
return new JObject(0, dic);
Expand Down
6 changes: 3 additions & 3 deletions JsonToolsNppPlugin/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Mark Johnston Olson")]
[assembly: AssemblyProduct("JSON tools plugin for Notepad++")]
[assembly: AssemblyCopyright("Copyright © molsonkiko 2022-2023")]
[assembly: AssemblyCopyright("Copyright © molsonkiko 2022-2024")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

Expand All @@ -28,5 +28,5 @@
// Build Number
// Revision
//
[assembly: AssemblyVersion("6.1.1.1")]
[assembly: AssemblyFileVersion("6.1.1.1")]
[assembly: AssemblyVersion("6.1.1.2")]
[assembly: AssemblyFileVersion("6.1.1.2")]
1 change: 1 addition & 0 deletions JsonToolsNppPlugin/Tests/RemesPathTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ public static bool Test()
// ufunction tests
new Query_DesiredResult("len(@)", fooLen.ToString()),
new Query_DesiredResult("s_mul(@.bar.b, 2)", "[\"a`ga`g\", \"bahbah\"]"),
new Query_DesiredResult("s_mul(@.bar.b{foo: s_slice(@[0], 2), bar: s_slice(@[1], :2), baz: a}, len(@.foo))", "{\"foo\": \"ggg\", \"bar\": \"bababa\", \"baz\": \"aaa\"}"), // vectorized arg functions on objects where second and subsequent args are functions of input
new Query_DesiredResult("s_lpad(@{ab, aba, d*7}, cd, 5)", "[\"cdcdab\", \"cdaba\", \"ddddddd\"]"),
new Query_DesiredResult("s_lpad(@{ab, c*4}, c, 4)", "[\"ccab\", \"cccc\"]"),
new Query_DesiredResult("s_rpad(@{ab, aba, d*7}, cd, 5)", "[\"abcdcd\", \"abacd\", \"ddddddd\"]"),
Expand Down
15 changes: 14 additions & 1 deletion docs/RemesPath.md
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,20 @@ __Example:__

### Vectorized functions ###

All of these functions are applied separately to each element in an array or value in an object.
__All of these functions are vectorized across their first argument,__ meaning that when one of these functions is called on an array or object, *any functions in the second and subsequent arguments reference the entire array/object, but the first argument is set to one element at a time.*

For example, consider the vectorized function `s_mul(s: string, n: int) -> string`. This function concatenates `n` instances of string `s`.
* With __array__ input `["a", "cd", "b"]`, `s_mul(@, len(@))` returns `["aaa", "cdcdcd", "bbb"]`
* The *first argument* references each element of the array separately.
* The *second argument `len(@)` references the entire array*, and is thus `3`, because the array has three elements.
* Because the *first element of the first argument* is `"a"`, the *first element of the output* is `s_mul(a, 3)`, or `"aaa"`
* Because the *second element of the first argument* is `"cd"`, the *second element of the output* is `s_mul(cd, 3)`, or `"cdcdcd"`
* With __object__ input `{"foo": "a", "bar": "cd"}`, `s_mul(@, len(@))` returns `{"foo": "aa", "bar": "cdcd"}` (__NOTE__: this example will fail on JsonTools earlier than [v6.2](/CHANGELOG.md#620---unreleased-yyyy-mm-dd))
* The *first argument* references each element of the object separately.
* The *second argument `len(@)` references the entire object*, and is thus `2`, because the object has two children.
* Because the *child of key `foo` of the first argument* is `"a"`, the *child of key `foo` of the output* is `s_mul(a, 2)`, or `"aa"`
* Because the *child of key `bar` of the first argument* is `"cd"`, the *child of key `bar` of the output* is `s_mul(cd, 2)`, or `"cdcd"`


All the vectorized string functions have names beginning with `s_`.

Expand Down
Loading

0 comments on commit 889628d

Please sign in to comment.