camunda / feel-scala

FEEL parser and interpreter written in Scala
https://camunda.github.io/feel-scala/
Apache License 2.0
120 stars 50 forks source link

Add `partition` function that allows to partitioning of collections in an easy way #881

Open sbuettner opened 1 month ago

sbuettner commented 1 month ago

Is your feature request related to a problem? Please describe.

We are missing a simple way of partitioning a collection (an array) into several sublists by simply providing the number of elements the collection should be partitioned by. There is the sublist function but its hard to use and a simple way would be very handy to create batches when one wants to split workloads for multi instance processing purposes:

list = [1, 2, 3, "4", "5", 6]
[[1,2], [3, "4"], ["5", 6]] = partition(list, 2)

Describe the solution you'd like

A new partition(array, size) function that splits an array into equals sub arrays each with the maximum number of elements defined by the size argument. This function should handle null values and empty lists gracefully.

// function signature
sliding(list: List, size: Number): List 

// examples
sliding([1,2,3,4,5], 2)
// output: [[1,2], [3,4], [5]]

sliding([], 2)
// output: []

sliding([1], 2)
// output: [[1]]

sliding([1,2], 0)
// output: null

sliding([1,2], -1)
// output: null

Related issues

During a customer interview we found out that there is currently not easy way to split workloads to adhere to API rate limits in the context of: https://github.com/camunda/connectors/issues/2695

saig0 commented 1 month ago

@sbuettner sounds like a valuable new function. :+1:

Feel free to contribute it. :rocket: