Skip to content

Commit 18a41b7

Browse files
authored
Merge pull request #950 from lzgabel/feature/881-lz-partition-function
feat(builtin): add partition function
2 parents 6493af2 + 75aa418 commit 18a41b7

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/main/scala/org/camunda/feel/impl/builtin/ListBuiltinFunctions.scala

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ class ListBuiltinFunctions(private val valueMapper: ValueMapper) {
6969
joinWithDelimiterFunction,
7070
joinWithDelimiterAndPrefixAndSuffixFunction
7171
),
72-
"is empty" -> List(emptyFunction)
72+
"is empty" -> List(emptyFunction),
73+
"partition" -> List(partitionFunction)
7374
)
7475

7576
private def listContainsFunction =
@@ -518,4 +519,16 @@ class ListBuiltinFunctions(private val valueMapper: ValueMapper) {
518519
}
519520
)
520521

522+
private def partitionFunction =
523+
builtinFunction(
524+
params = List("list", "size"),
525+
invoke = { case List(ValList(list), ValNumber(size)) =>
526+
if (size.intValue > 0) {
527+
ValList(list.grouped(size.intValue).map(l => ValList(l)).toList)
528+
} else {
529+
ValError(s"'size' should be greater than zero but was '$size'")
530+
}
531+
}
532+
)
533+
521534
}

src/test/scala/org/camunda/feel/impl/builtin/BuiltinListFunctionsTest.scala

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package org.camunda.feel.impl.builtin
1919
import org.scalatest.matchers.should.Matchers
2020
import org.scalatest.flatspec.AnyFlatSpec
2121
import org.camunda.feel._
22+
import org.camunda.feel.api.EvaluationFailureType.FUNCTION_INVOCATION_FAILURE
2223
import org.camunda.feel.impl.{EvaluationResultMatchers, FeelEngineTest, FeelIntegrationTest}
2324
import org.camunda.feel.syntaxtree._
2425

@@ -570,4 +571,32 @@ class BuiltinListFunctionsTest
570571
evaluateExpression(" is empty([1,2,3]) ") should returnResult(false)
571572
evaluateExpression(" is empty(list: [1]) ") should returnResult(false)
572573
}
574+
575+
"A partition() function" should "return list partitioned by _" in {
576+
evaluateExpression(" partition([1,2,3,4,5], 2) ") should returnResult(
577+
List(List(1, 2), List(3, 4), List(5))
578+
)
579+
evaluateExpression(" partition(list: [1,2,3,4,5], size: 2) ") should returnResult(
580+
List(List(1, 2), List(3, 4), List(5))
581+
)
582+
583+
evaluateExpression(" partition([], 2) ") should returnResult(List())
584+
evaluateExpression(" partition([1], 2) ") should returnResult(List(List(1)))
585+
}
586+
587+
it should "return null if the size is invalid" in {
588+
evaluateExpression(" partition([1,2], 0) ") should (
589+
returnNull() and reportFailure(
590+
FUNCTION_INVOCATION_FAILURE,
591+
"Failed to invoke function 'partition': 'size' should be greater than zero but was '0'"
592+
)
593+
)
594+
595+
evaluateExpression(" partition([1,2], -1) ") should (
596+
returnNull() and reportFailure(
597+
FUNCTION_INVOCATION_FAILURE,
598+
"Failed to invoke function 'partition': 'size' should be greater than zero but was '-1'"
599+
)
600+
)
601+
}
573602
}

0 commit comments

Comments
 (0)