Skip to content

Commit 7d56a15

Browse files
committed
feat(builtin): add partition function
1 parent 4788872 commit 7d56a15

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-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+
ValNull
530+
}
531+
}
532+
)
533+
521534
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,4 +570,17 @@ class BuiltinListFunctionsTest
570570
evaluateExpression(" is empty([1,2,3]) ") should returnResult(false)
571571
evaluateExpression(" is empty(list: [1]) ") should returnResult(false)
572572
}
573+
574+
"A partition() function" should "return list partitioned by _" in {
575+
evaluateExpression(" partition([1,2,3,4,5], 2) ") should returnResult(
576+
List(List(1, 2), List(3, 4), List(5))
577+
)
578+
evaluateExpression(" partition([], 2) ") should returnResult(List())
579+
evaluateExpression(" partition([1], 2) ") should returnResult(List(List(1)))
580+
}
581+
582+
it should "return null if the size is invalid" in {
583+
evaluateExpression(" partition([1,2], 0) ") should returnNull()
584+
evaluateExpression(" partition([1,2], -1) ") should returnNull()
585+
}
573586
}

0 commit comments

Comments
 (0)