Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the spread ... operator, other new functions, and indices on lambdas #13658

Merged
merged 5 commits into from Apr 23, 2024

Conversation

anthony-c-martin
Copy link
Member

@anthony-c-martin anthony-c-martin commented Mar 18, 2024

Adds the spread operator ... as well as various new functions + indexes on lambdas:

  1. Spread operator - usage is as follows:
    • In an object:
      var objA = { bar: 'bar' }
      var objB = { foo: 'foo', ...objA } // equivalent to { foo: 'foo', bar: 'bar' }
    • In an array:
      var arrA = [ 2, 3 ]
      var arrB = [ 1, ...arrA, 4 ] // equivalent to [ 1, 2, 3, 4 ]
  2. New functions + usage:
    • objectKeys: Returns the keys of an object parameter:
      var example = objectKeys({ a: 1, b: 2 }) // returns [ 'a', 'b' ]
    • mapValues: Create an object from an input object, using a custom lambda to map values:
      var example = mapValues({ foo: 'foo' }, val => toUpper(val)) // returns { foo: 'FOO' }
    • groupBy: Create an object with array values from an array, using a grouping condition:
      var example = groupBy(['foo', 'bar', 'baz'], x => substring(x, 0, 1)) // returns { f: [ 'foo' ], b: [ 'bar', 'baz' ]
    • shallowMerge: Perform a shallow merge of input object parameters:
      var example = shallowMerge([{ foo: 'foo' }, { bar: 'bar' }]) // returns { foo: 'foo', bar: 'bar' }
  3. Optional indices on lambdas + usage:
    • map:
      var example = map(['a', 'b'], (x, i) => { index: i, val: x }) // returns [ { index: 0, val: 'a' }, { index: 1 val: 'b' } ]
    • reduce:
      var example = reduce([ 2, 3, 7 ], 0, (cur, next, i) => (i % 2 == 0) ? cur + next : cur) // returns 9
    • filter:
      var example = filter([ 'foo', 'bar', 'baz' ], (val, i) => i < 2 && substring(val, 0, 1) == 'b') // returns [ 'bar' ]

Closes #13560
Closes #9244
Closes #1560
Addresses some of the issues described under the following: #2082, #1853, #387

Microsoft Reviewers: Open in CodeFlow

Copy link
Contributor

github-actions bot commented Mar 18, 2024

Test this change out locally with the following install scripts (Action run 8808567201)

VSCode
  • Mac/Linux
    bash <(curl -Ls https://aka.ms/bicep/nightly-vsix.sh) --run-id 8808567201
  • Windows
    iex "& { $(irm https://aka.ms/bicep/nightly-vsix.ps1) } -RunId 8808567201"
Azure CLI
  • Mac/Linux
    bash <(curl -Ls https://aka.ms/bicep/nightly-cli.sh) --run-id 8808567201
  • Windows
    iex "& { $(irm https://aka.ms/bicep/nightly-cli.ps1) } -RunId 8808567201"

Copy link
Contributor

github-actions bot commented Mar 18, 2024

Test Results

    66 files   -     33      66 suites   - 33   23m 20s ⏱️ - 15m 18s
10 868 tests  -      5  10 867 ✅  -      5  1 💤 ±0  0 ❌ ±0 
25 632 runs   - 12 767  25 630 ✅  - 12 766  2 💤  - 1  0 ❌ ±0 

Results for commit 880e364. ± Comparison against base commit 959dbbb.

♻️ This comment has been updated with latest results.

@anthony-c-martin anthony-c-martin force-pushed the ant/spread branch 2 times, most recently from 0116b3d to 3b87b56 Compare March 19, 2024 03:30
@anthony-c-martin anthony-c-martin changed the title [WIP - do not merge] Implement the spread ... operator [WIP - do not merge] Implement the spread ... operator, other new functions, and indices on lambdas Mar 19, 2024
@anthony-c-martin anthony-c-martin added the do not merge Do not merge this pull request yet. label Mar 19, 2024
@anthony-c-martin anthony-c-martin changed the title [WIP - do not merge] Implement the spread ... operator, other new functions, and indices on lambdas Implement the spread ... operator, other new functions, and indices on lambdas Apr 20, 2024
@anthony-c-martin anthony-c-martin removed the do not merge Do not merge this pull request yet. label Apr 20, 2024
Copy link
Contributor

@shenglol shenglol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@anthony-c-martin anthony-c-martin enabled auto-merge (squash) April 23, 2024 23:18
@anthony-c-martin anthony-c-martin merged commit 8be7869 into main Apr 23, 2024
44 checks passed
@anthony-c-martin anthony-c-martin deleted the ant/spread branch April 23, 2024 23:29
@BernieWhite
Copy link

@anthony-c-martin Is the syntax example of reduce above correct?

var example = reduce([ 2, 3, 7 ], (cur, next, i) => (i % 2 == 0) ? cur + next : cur) // returns 9

When testing this locally, I'm getting a syntax issue with the required initialValue argument missing.

@anthony-c-martin
Copy link
Member Author

@anthony-c-martin Is the syntax example of reduce above correct?

var example = reduce([ 2, 3, 7 ], (cur, next, i) => (i % 2 == 0) ? cur + next : cur) // returns 9

When testing this locally, I'm getting a syntax issue with the required initialValue argument missing.

Oops, this should be:

var example = reduce([ 2, 3, 7 ], 0, (cur, next, i) => (i % 2 == 0) ? cur + next : cur) // returns 9

I've updated the original comment.

@slavizh
Copy link
Contributor

slavizh commented May 17, 2024

@anthony-c-martin I could not find the documentation for the new function features published yet but there are examples here in this PR. However for one of the main reasons to have index for lambda functions was to be able to access resource arrays and I have found out that this is not possible. Any idea why this was not done? It seems only half of the job was done.

properties: {
      actionGroups: map(eventSubscription.destination.actionGroups, (actionGroup, i) => actionGroups[i].id)
    }

shows error:


Using lambda variables inside resource or module array access is not currently supported. Found the following lambda variable(s) being accessed: "i".bicep(BCP247)

@anthony-c-martin
Copy link
Member Author

@slavizh I've raised #14106 - will explain more there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants