Skip to content

Commit

Permalink
feat(builtin): add partition function
Browse files Browse the repository at this point in the history
  • Loading branch information
lzgabel committed Nov 11, 2024
1 parent 4788872 commit 33ff252
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ class ListBuiltinFunctions(private val valueMapper: ValueMapper) {
joinWithDelimiterFunction,
joinWithDelimiterAndPrefixAndSuffixFunction
),
"is empty" -> List(emptyFunction)
"is empty" -> List(emptyFunction),
"partition" -> List(partitionFunction)
)

private def listContainsFunction =
Expand Down Expand Up @@ -518,4 +519,16 @@ class ListBuiltinFunctions(private val valueMapper: ValueMapper) {
}
)

private def partitionFunction =
builtinFunction(
params = List("list", "size"),
invoke = { case List(ValList(list), ValNumber(size)) =>
if (size.intValue > 0) {
ValList(list.grouped(size.intValue).map(l => ValList(l)).toList)
} else {
ValNull
}
}
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -570,4 +570,14 @@ class BuiltinListFunctionsTest
evaluateExpression(" is empty([1,2,3]) ") should returnResult(false)
evaluateExpression(" is empty(list: [1]) ") should returnResult(false)
}

"A partition() function" should "return list partitioned by _" in {
evaluateExpression(" partition([1,2,3,4,5], 2) ") should returnResult(
List(List(1, 2), List(3, 4), List(5))
)
evaluateExpression(" partition([], 2) ") should returnResult(List())
evaluateExpression(" partition([1], 2) ") should returnResult(List(List(1)))
evaluateExpression(" partition([1,2], 0) ") should returnNull()
evaluateExpression(" partition([1,2], -1) ") should returnNull()
}
}

0 comments on commit 33ff252

Please sign in to comment.