Skip to content

Commit

Permalink
add std.reverse
Browse files Browse the repository at this point in the history
  • Loading branch information
lihaoyi-databricks committed Jun 12, 2023
1 parent 74d6ce3 commit a425342
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.sc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import mill._, scalalib._, publish._, scalajslib._, scalanativelib._, scalanativelib.api._
val sjsonnetVersion = "0.4.5-RC3"
val sjsonnetVersion = "0.4.5-RC4"

object sjsonnet extends Cross[SjsonnetModule]("2.12.13", "2.13.4")
class SjsonnetModule(val crossScalaVersion: String) extends Module {
Expand Down
6 changes: 6 additions & 0 deletions sjsonnet/src/sjsonnet/Std.scala
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,11 @@ class Std {
new Val.Arr(pos, out.toArray)
}
}
private object Reverse extends Val.Builtin1("arrs") {
def evalRhs(arrs: Val, ev: EvalScope, pos: Position): Val = {
new Val.Arr(pos, arrs.asArr.asLazyArray.reverse)
}
}

private object Split extends Val.Builtin2("str", "c") {
def evalRhs(_str: Val, _c: Val, ev: EvalScope, pos: Position): Val = {
Expand Down Expand Up @@ -946,6 +951,7 @@ class Std {
},

"flattenArrays" -> FlattenArrays,
"reverse" -> Reverse,

builtin("manifestIni", "v"){ (pos, ev, v: Val) =>
val materialized = Materializer(v)(ev)
Expand Down
5 changes: 5 additions & 0 deletions sjsonnet/test/src/sjsonnet/Std0150FunctionsTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,10 @@ object Std0150FunctionsTests extends TestSuite {
eval("""std.foldl(function (acc, it) acc + " " + it, "jsonnet", "this is")""") ==>
ujson.Str("this is j s o n n e t")
}
test("reverse"){
eval("""std.reverse([])""") ==> ujson.Arr()
eval("""std.reverse([1])""") ==> ujson.Arr(1)
eval("""std.reverse(["1", true, null])""") ==> ujson.Arr(ujson.Null, true, "1")
}
}
}

0 comments on commit a425342

Please sign in to comment.