git.delta.rocks / jrsonnet / refs/commits / 702547533591

difftreelog

feat std.{sin,cos,tan,asin,acos,atan}

Yaroslav Bolyukin2021-07-12parent: #64b86a5.patch.diff
in: master

2 files changed

modifiedcrates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/builtin/mod.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs
@@ -90,6 +90,12 @@
 			("log".into(), builtin_log),
 			("pow".into(), builtin_pow),
 			("sqrt".into(), builtin_sqrt),
+			("sin".into(), builtin_sin),
+			("cos".into(), builtin_cos),
+			("tan".into(), builtin_tan),
+			("asin".into(), builtin_asin),
+			("acos".into(), builtin_acos),
+			("atan".into(), builtin_atan),
 			("extVar".into(), builtin_ext_var),
 			("native".into(), builtin_native),
 			("filter".into(), builtin_filter),
@@ -313,6 +319,54 @@
 	})
 }
 
+fn builtin_sin(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {
+	parse_args!(context, "sin", args, 1, [
+		0, x: ty!(number) => Val::Num;
+	], {
+		Ok(Val::Num(x.sin()))
+	})
+}
+
+fn builtin_cos(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {
+	parse_args!(context, "cos", args, 1, [
+		0, x: ty!(number) => Val::Num;
+	], {
+		Ok(Val::Num(x.cos()))
+	})
+}
+
+fn builtin_tan(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {
+	parse_args!(context, "tan", args, 1, [
+		0, x: ty!(number) => Val::Num;
+	], {
+		Ok(Val::Num(x.tan()))
+	})
+}
+
+fn builtin_asin(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {
+	parse_args!(context, "asin", args, 1, [
+		0, x: ty!(number) => Val::Num;
+	], {
+		Ok(Val::Num(x.asin()))
+	})
+}
+
+fn builtin_acos(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {
+	parse_args!(context, "acos", args, 1, [
+		0, x: ty!(number) => Val::Num;
+	], {
+		Ok(Val::Num(x.acos()))
+	})
+}
+
+fn builtin_atan(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {
+	parse_args!(context, "atan", args, 1, [
+		0, x: ty!(number) => Val::Num;
+	], {
+		Ok(Val::Num(x.atan()))
+	})
+}
+
 fn builtin_ext_var(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {
 	parse_args!(context, "extVar", args, 1, [
 		0, x: ty!(string) => Val::Str;
modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
before · crates/jrsonnet-stdlib/src/std.jsonnet
1{2  local std = self,3  local id = std.id,45  # Those functions aren't normally located in stdlib6  length:: $intrinsic(length),7  type:: $intrinsic(type),8  makeArray:: $intrinsic(makeArray),9  codepoint:: $intrinsic(codepoint),10  objectFieldsEx:: $intrinsic(objectFieldsEx),11  objectHasEx:: $intrinsic(objectHasEx),12  primitiveEquals:: $intrinsic(primitiveEquals),13  modulo:: $intrinsic(modulo),14  floor:: $intrinsic(floor),15  ceil:: $intrinsic(ceil),16  extVar:: $intrinsic(extVar),17  native:: $intrinsic(native),18  filter:: $intrinsic(filter),19  char:: $intrinsic(char),20  encodeUTF8:: $intrinsic(encodeUTF8),21  md5:: $intrinsic(md5),22  trace:: $intrinsic(trace),23  id:: $intrinsic(id),24  parseJson:: $intrinsic(parseJson),2526  log:: $intrinsic(log),27  pow:: $intrinsic(pow),28  sqrt:: $intrinsic(sqrt),2930  isString(v):: std.type(v) == 'string',31  isNumber(v):: std.type(v) == 'number',32  isBoolean(v):: std.type(v) == 'boolean',33  isObject(v):: std.type(v) == 'object',34  isArray(v):: std.type(v) == 'array',35  isFunction(v):: std.type(v) == 'function',3637  toString(a)::38    if std.type(a) == 'string' then a else '' + a,3940  substr(str, from, len)::41    assert std.isString(str) : 'substr first parameter should be a string, got ' + std.type(str);42    assert std.isNumber(from) : 'substr second parameter should be a string, got ' + std.type(from);43    assert std.isNumber(len) : 'substr third parameter should be a string, got ' + std.type(len);44    assert len >= 0 : 'substr third parameter should be greater than zero, got ' + len;45    std.join('', std.makeArray(std.max(0, std.min(len, std.length(str) - from)), function(i) str[i + from])),4647  startsWith(a, b)::48    if std.length(a) < std.length(b) then49      false50    else51      std.substr(a, 0, std.length(b)) == b,5253  endsWith(a, b)::54    if std.length(a) < std.length(b) then55      false56    else57      std.substr(a, std.length(a) - std.length(b), std.length(b)) == b,5859  lstripChars(str, chars)::60    if std.length(str) > 0 && std.member(chars, str[0]) then61      std.lstripChars(str[1:], chars)62    else63      str,6465  rstripChars(str, chars)::66    local len = std.length(str);67    if len > 0 && std.member(chars, str[len - 1]) then68      std.rstripChars(str[:len - 1], chars)69    else70      str,7172  stripChars(str, chars)::73    std.lstripChars(std.rstripChars(str, chars), chars),7475  stringChars(str)::76    std.makeArray(std.length(str), function(i) str[i]),7778  local parse_nat(str, base) =79    assert base > 0 && base <= 16 : 'integer base %d invalid' % base;80    // These codepoints are in ascending order:81    local zero_code = std.codepoint('0');82    local upper_a_code = std.codepoint('A');83    local lower_a_code = std.codepoint('a');84    local addDigit(aggregate, char) =85      local code = std.codepoint(char);86      local digit = if code >= lower_a_code then87        code - lower_a_code + 1088      else if code >= upper_a_code then89        code - upper_a_code + 1090      else91        code - zero_code;92      assert digit >= 0 && digit < base : '%s is not a base %d integer' % [str, base];93      base * aggregate + digit;94    std.foldl(addDigit, std.stringChars(str), 0),9596  parseInt(str)::97    assert std.isString(str) : 'Expected string, got ' + std.type(str);98    assert std.length(str) > 0 && str != '-' : 'Not an integer: "%s"' % [str];99    if str[0] == '-' then100      -parse_nat(str[1:], 10)101    else102      parse_nat(str, 10),103104  parseOctal(str)::105    assert std.isString(str) : 'Expected string, got ' + std.type(str);106    assert std.length(str) > 0 : 'Not an octal number: ""';107    parse_nat(str, 8),108109  parseHex(str)::110    assert std.isString(str) : 'Expected string, got ' + std.type(str);111    assert std.length(str) > 0 : 'Not hexadecimal: ""';112    parse_nat(str, 16),113114  split(str, c)::115    assert std.isString(str) : 'std.split first parameter should be a string, got ' + std.type(str);116    assert std.isString(c) : 'std.split second parameter should be a string, got ' + std.type(c);117    assert std.length(c) == 1 : 'std.split second parameter should have length 1, got ' + std.length(c);118    std.splitLimit(str, c, -1),119120  splitLimit(str, c, maxsplits)::121    assert std.isString(str) : 'std.splitLimit first parameter should be a string, got ' + std.type(str);122    assert std.isString(c) : 'std.splitLimit second parameter should be a string, got ' + std.type(c);123    assert std.length(c) == 1 : 'std.splitLimit second parameter should have length 1, got ' + std.length(c);124    assert std.isNumber(maxsplits) : 'std.splitLimit third parameter should be a number, got ' + std.type(maxsplits);125    local aux(str, delim, i, arr, v) =126      local c = str[i];127      local i2 = i + 1;128      if i >= std.length(str) then129        arr + [v]130      else if c == delim && (maxsplits == -1 || std.length(arr) < maxsplits) then131        aux(str, delim, i2, arr + [v], '') tailstrict132      else133        aux(str, delim, i2, arr, v + c) tailstrict;134    aux(str, c, 0, [], ''),135136  strReplace:: $intrinsic(strReplace),137138  asciiUpper(str)::139    local cp = std.codepoint;140    local up_letter(c) = if cp(c) >= 97 && cp(c) < 123 then141      std.char(cp(c) - 32)142    else143      c;144    std.join('', std.map(up_letter, std.stringChars(str))),145146  asciiLower(str)::147    local cp = std.codepoint;148    local down_letter(c) = if cp(c) >= 65 && cp(c) < 91 then149      std.char(cp(c) + 32)150    else151      c;152    std.join('', std.map(down_letter, std.stringChars(str))),153154  range:: $intrinsic(range),155156  repeat(what, count)::157    local joiner =158      if std.isString(what) then ''159      else if std.isArray(what) then []160      else error 'std.repeat first argument must be an array or a string';161    std.join(joiner, std.makeArray(count, function(i) what)),162163  slice:: $intrinsic(slice),164165  member(arr, x)::166    if std.isArray(arr) then167      std.count(arr, x) > 0168    else if std.isString(arr) then169      std.length(std.findSubstr(x, arr)) > 0170    else error 'std.member first argument must be an array or a string',171172  count(arr, x):: std.length(std.filter(function(v) v == x, arr)),173174  mod:: $intrinsic(mod),175176  map:: $intrinsic(map),177178  mapWithIndex(func, arr)::179    if !std.isFunction(func) then180      error ('std.mapWithIndex first param must be function, got ' + std.type(func))181    else if !std.isArray(arr) && !std.isString(arr) then182      error ('std.mapWithIndex second param must be array, got ' + std.type(arr))183    else184      std.makeArray(std.length(arr), function(i) func(i, arr[i])),185186  mapWithKey(func, obj)::187    if !std.isFunction(func) then188      error ('std.mapWithKey first param must be function, got ' + std.type(func))189    else if !std.isObject(obj) then190      error ('std.mapWithKey second param must be object, got ' + std.type(obj))191    else192      { [k]: func(k, obj[k]) for k in std.objectFields(obj) },193194  flatMap:: $intrinsic(flatMap),195196  join:: $intrinsic(join),197198  lines(arr)::199    std.join('\n', arr + ['']),200201  deepJoin(arr)::202    if std.isString(arr) then203      arr204    else if std.isArray(arr) then205      std.join('', [std.deepJoin(x) for x in arr])206    else207      error 'Expected string or array, got %s' % std.type(arr),208209210  format:: $intrinsic(format),211212  foldr:: $intrinsic(foldr),213214  foldl:: $intrinsic(foldl),215216  filterMap(filter_func, map_func, arr)::217    if !std.isFunction(filter_func) then218      error ('std.filterMap first param must be function, got ' + std.type(filter_func))219    else if !std.isFunction(map_func) then220      error ('std.filterMap second param must be function, got ' + std.type(map_func))221    else if !std.isArray(arr) then222      error ('std.filterMap third param must be array, got ' + std.type(arr))223    else224      std.map(map_func, std.filter(filter_func, arr)),225226  assertEqual(a, b)::227    if a == b then228      true229    else230      error 'Assertion failed. ' + a + ' != ' + b,231232  abs(n)::233    if !std.isNumber(n) then234      error 'std.abs expected number, got ' + std.type(n)235    else236      if n > 0 then n else -n,237238  sign(n)::239    if !std.isNumber(n) then240      error 'std.sign expected number, got ' + std.type(n)241    else242      if n > 0 then243        1244      else if n < 0 then245        -1246      else 0,247248  max(a, b)::249    if !std.isNumber(a) then250      error 'std.max first param expected number, got ' + std.type(a)251    else if !std.isNumber(b) then252      error 'std.max second param expected number, got ' + std.type(b)253    else254      if a > b then a else b,255256  min(a, b)::257    if !std.isNumber(a) then258      error 'std.min first param expected number, got ' + std.type(a)259    else if !std.isNumber(b) then260      error 'std.min second param expected number, got ' + std.type(b)261    else262      if a < b then a else b,263264  clamp(x, minVal, maxVal)::265    if x < minVal then minVal266    else if x > maxVal then maxVal267    else x,268269  flattenArrays(arrs)::270    std.foldl(function(a, b) a + b, arrs, []),271272  manifestIni(ini)::273    local body_lines(body) =274      std.join([], [275        local value_or_values = body[k];276        if std.isArray(value_or_values) then277          ['%s = %s' % [k, value] for value in value_or_values]278        else279          ['%s = %s' % [k, value_or_values]]280281        for k in std.objectFields(body)282      ]);283284    local section_lines(sname, sbody) = ['[%s]' % [sname]] + body_lines(sbody),285          main_body = if std.objectHas(ini, 'main') then body_lines(ini.main) else [],286          all_sections = [287      section_lines(k, ini.sections[k])288      for k in std.objectFields(ini.sections)289    ];290    std.join('\n', main_body + std.flattenArrays(all_sections) + ['']),291292  manifestToml(value):: std.manifestTomlEx(value, '  '),293294  manifestTomlEx(value, indent)::295    local296      escapeStringToml = std.escapeStringJson,297      escapeKeyToml(key) =298        local bare_allowed = std.set(std.stringChars('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-'));299        if std.setUnion(std.set(std.stringChars(key)), bare_allowed) == bare_allowed then key else escapeStringToml(key),300      isTableArray(v) = std.isArray(v) && std.length(v) > 0 && std.foldl(function(a, b) a && std.isObject(b), v, true),301      isSection(v) = std.isObject(v) || isTableArray(v),302      renderValue(v, indexedPath, inline, cindent) =303        if v == true then304          'true'305        else if v == false then306          'false'307        else if v == null then308          error 'Tried to manifest "null" at ' + indexedPath309        else if std.isNumber(v) then310          '' + v311        else if std.isString(v) then312          escapeStringToml(v)313        else if std.isFunction(v) then314          error 'Tried to manifest function at ' + indexedPath315        else if std.isArray(v) then316          if std.length(v) == 0 then317            '[]'318          else319            local range = std.range(0, std.length(v) - 1);320            local new_indent = if inline then '' else cindent + indent;321            local separator = if inline then ' ' else '\n';322            local lines = ['[' + separator]323                          + std.join([',' + separator],324                                     [325                                       [new_indent + renderValue(v[i], indexedPath + [i], true, '')]326                                       for i in range327                                     ])328                          + [separator + (if inline then '' else cindent) + ']'];329            std.join('', lines)330        else if std.isObject(v) then331          local lines = ['{ ']332                        + std.join([', '],333                                   [334                                     [escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], true, '')]335                                     for k in std.objectFields(v)336                                   ])337                        + [' }'];338          std.join('', lines),339      renderTableInternal(v, path, indexedPath, cindent) =340        local kvp = std.flattenArrays([341          [cindent + escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], false, cindent)]342          for k in std.objectFields(v)343          if !isSection(v[k])344        ]);345        local sections = [std.join('\n', kvp)] + [346          (347            if std.isObject(v[k]) then348              renderTable(v[k], path + [k], indexedPath + [k], cindent)349            else350              renderTableArray(v[k], path + [k], indexedPath + [k], cindent)351          )352          for k in std.objectFields(v)353          if isSection(v[k])354        ];355        std.join('\n\n', sections),356      renderTable(v, path, indexedPath, cindent) =357        cindent + '[' + std.join('.', std.map(escapeKeyToml, path)) + ']'358        + (if v == {} then '' else '\n')359        + renderTableInternal(v, path, indexedPath, cindent + indent),360      renderTableArray(v, path, indexedPath, cindent) =361        local range = std.range(0, std.length(v) - 1);362        local sections = [363          (cindent + '[[' + std.join('.', std.map(escapeKeyToml, path)) + ']]'364           + (if v[i] == {} then '' else '\n')365           + renderTableInternal(v[i], path, indexedPath + [i], cindent + indent))366          for i in range367        ];368        std.join('\n\n', sections);369    if std.isObject(value) then370      renderTableInternal(value, [], [], '')371    else372      error 'TOML body must be an object. Got ' + std.type(value),373374  escapeStringJson:: $intrinsic(escapeStringJson),375376  escapeStringPython(str)::377    std.escapeStringJson(str),378379  escapeStringBash(str_)::380    local str = std.toString(str_);381    local trans(ch) =382      if ch == "'" then383        "'\"'\"'"384      else385        ch;386    "'%s'" % std.join('', [trans(ch) for ch in std.stringChars(str)]),387388  escapeStringDollars(str_)::389    local str = std.toString(str_);390    local trans(ch) =391      if ch == '$' then392        '$$'393      else394        ch;395    std.foldl(function(a, b) a + trans(b), std.stringChars(str), ''),396397  manifestJson(value):: std.manifestJsonEx(value, '    '),398399  manifestJsonEx:: $intrinsic(manifestJsonEx),400401  manifestYamlDoc(value, indent_array_in_object=false)::402    local aux(v, path, cindent) =403      if v == true then404        'true'405      else if v == false then406        'false'407      else if v == null then408        'null'409      else if std.isNumber(v) then410        '' + v411      else if std.isString(v) then412        local len = std.length(v);413        if len == 0 then414          '""'415        else if v[len - 1] == '\n' then416          local split = std.split(v, '\n');417          std.join('\n' + cindent + '  ', ['|'] + split[0:std.length(split) - 1])418        else419          std.escapeStringJson(v)420      else if std.isFunction(v) then421        error 'Tried to manifest function at ' + path422      else if std.isArray(v) then423        if std.length(v) == 0 then424          '[]'425        else426          local params(value) =427            if std.isArray(value) && std.length(value) > 0 then {428              // While we could avoid the new line, it yields YAML that is429              // hard to read, e.g.:430              // - - - 1431              //     - 2432              //   - - 3433              //     - 4434              new_indent: cindent + '  ',435              space: '\n' + self.new_indent,436            } else if std.isObject(value) && std.length(value) > 0 then {437              new_indent: cindent + '  ',438              // In this case we can start on the same line as the - because the indentation439              // matches up then.  The converse is not true, because fields are not always440              // 1 character long.441              space: ' ',442            } else {443              // In this case, new_indent is only used in the case of multi-line strings.444              new_indent: cindent,445              space: ' ',446            };447          local range = std.range(0, std.length(v) - 1);448          local parts = [449            '-' + param.space + aux(v[i], path + [i], param.new_indent)450            for i in range451            for param in [params(v[i])]452          ];453          std.join('\n' + cindent, parts)454      else if std.isObject(v) then455        if std.length(v) == 0 then456          '{}'457        else458          local params(value) =459            if std.isArray(value) && std.length(value) > 0 then {460              // Not indenting allows e.g.461              // ports:462              // - 80463              // instead of464              // ports:465              //   - 80466              new_indent: if indent_array_in_object then cindent + '  ' else cindent,467              space: '\n' + self.new_indent,468            } else if std.isObject(value) && std.length(value) > 0 then {469              new_indent: cindent + '  ',470              space: '\n' + self.new_indent,471            } else {472              // In this case, new_indent is only used in the case of multi-line strings.473              new_indent: cindent,474              space: ' ',475            };476          local lines = [477            std.escapeStringJson(k) + ':' + param.space + aux(v[k], path + [k], param.new_indent)478            for k in std.objectFields(v)479            for param in [params(v[k])]480          ];481          std.join('\n' + cindent, lines);482    aux(value, [], ''),483484  manifestYamlStream(value, indent_array_in_object=false, c_document_end=true)::485    if !std.isArray(value) then486      error 'manifestYamlStream only takes arrays, got ' + std.type(value)487    else488      '---\n' + std.join(489        '\n---\n', [std.manifestYamlDoc(e, indent_array_in_object) for e in value]490      ) + if c_document_end then '\n...\n' else '\n',491492493  manifestPython(v)::494    if std.isObject(v) then495      local fields = [496        '%s: %s' % [std.escapeStringPython(k), std.manifestPython(v[k])]497        for k in std.objectFields(v)498      ];499      '{%s}' % [std.join(', ', fields)]500    else if std.isArray(v) then501      '[%s]' % [std.join(', ', [std.manifestPython(v2) for v2 in v])]502    else if std.isString(v) then503      '%s' % [std.escapeStringPython(v)]504    else if std.isFunction(v) then505      error 'cannot manifest function'506    else if std.isNumber(v) then507      std.toString(v)508    else if v == true then509      'True'510    else if v == false then511      'False'512    else if v == null then513      'None',514515  manifestPythonVars(conf)::516    local vars = ['%s = %s' % [k, std.manifestPython(conf[k])] for k in std.objectFields(conf)];517    std.join('\n', vars + ['']),518519  manifestXmlJsonml(value)::520    if !std.isArray(value) then521      error 'Expected a JSONML value (an array), got %s' % std.type(value)522    else523      local aux(v) =524        if std.isString(v) then525          v526        else527          local tag = v[0];528          local has_attrs = std.length(v) > 1 && std.isObject(v[1]);529          local attrs = if has_attrs then v[1] else {};530          local children = if has_attrs then v[2:] else v[1:];531          local attrs_str =532            std.join('', [' %s="%s"' % [k, attrs[k]] for k in std.objectFields(attrs)]);533          std.deepJoin(['<', tag, attrs_str, '>', [aux(x) for x in children], '</', tag, '>']);534535      aux(value),536537  local base64_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',538  local base64_inv = { [base64_table[i]]: i for i in std.range(0, 63) },539540  base64:: $intrinsic(base64),541542  base64DecodeBytes(str)::543    if std.length(str) % 4 != 0 then544      error 'Not a base64 encoded string "%s"' % str545    else546      local aux(str, i, r) =547        if i >= std.length(str) then548          r549        else550          // all 6 bits of i, 2 MSB of i+1551          local n1 = [base64_inv[str[i]] << 2 | (base64_inv[str[i + 1]] >> 4)];552          // 4 LSB of i+1, 4MSB of i+2553          local n2 =554            if str[i + 2] == '=' then []555            else [(base64_inv[str[i + 1]] & 15) << 4 | (base64_inv[str[i + 2]] >> 2)];556          // 2 LSB of i+2, all 6 bits of i+3557          local n3 =558            if str[i + 3] == '=' then []559            else [(base64_inv[str[i + 2]] & 3) << 6 | base64_inv[str[i + 3]]];560          aux(str, i + 4, r + n1 + n2 + n3) tailstrict;561      aux(str, 0, []),562563  base64Decode(str)::564    local bytes = std.base64DecodeBytes(str);565    std.join('', std.map(function(b) std.char(b), bytes)),566567  reverse:: $intrinsic(reverse),568569  sortImpl:: $intrinsic(sortImpl),570571  sort(arr, keyF=id)::572    std.sortImpl(arr, keyF),573574  uniq(arr, keyF=id)::575    local f(a, b) =576      if std.length(a) == 0 then577        [b]578      else if keyF(a[std.length(a) - 1]) == keyF(b) then579        a580      else581        a + [b];582    std.foldl(f, arr, []),583584  set(arr, keyF=id)::585    std.uniq(std.sort(arr, keyF), keyF),586587  setMember(x, arr, keyF=id)::588    // TODO(dcunnin): Binary chop for O(log n) complexity589    std.length(std.setInter([x], arr, keyF)) > 0,590591  setUnion(a, b, keyF=id)::592    // NOTE: order matters, values in `a` win593    local aux(a, b, i, j, acc) =594      if i >= std.length(a) then595        acc + b[j:]596      else if j >= std.length(b) then597        acc + a[i:]598      else599        local ak = keyF(a[i]);600        local bk = keyF(b[j]);601        if ak == bk then602          aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict603        else if ak < bk then604          aux(a, b, i + 1, j, acc + [a[i]]) tailstrict605        else606          aux(a, b, i, j + 1, acc + [b[j]]) tailstrict;607    aux(a, b, 0, 0, []),608609  setInter(a, b, keyF=id)::610    local aux(a, b, i, j, acc) =611      if i >= std.length(a) || j >= std.length(b) then612        acc613      else614        if keyF(a[i]) == keyF(b[j]) then615          aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict616        else if keyF(a[i]) < keyF(b[j]) then617          aux(a, b, i + 1, j, acc) tailstrict618        else619          aux(a, b, i, j + 1, acc) tailstrict;620    aux(a, b, 0, 0, []) tailstrict,621622  setDiff(a, b, keyF=id)::623    local aux(a, b, i, j, acc) =624      if i >= std.length(a) then625        acc626      else if j >= std.length(b) then627        acc + a[i:]628      else629        if keyF(a[i]) == keyF(b[j]) then630          aux(a, b, i + 1, j + 1, acc) tailstrict631        else if keyF(a[i]) < keyF(b[j]) then632          aux(a, b, i + 1, j, acc + [a[i]]) tailstrict633        else634          aux(a, b, i, j + 1, acc) tailstrict;635    aux(a, b, 0, 0, []) tailstrict,636637  mergePatch(target, patch)::638    if std.isObject(patch) then639      local target_object =640        if std.isObject(target) then target else {};641642      local target_fields =643        if std.isObject(target_object) then std.objectFields(target_object) else [];644645      local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];646      local both_fields = std.setUnion(target_fields, std.objectFields(patch));647648      {649        [k]:650          if !std.objectHas(patch, k) then651            target_object[k]652          else if !std.objectHas(target_object, k) then653            std.mergePatch(null, patch[k]) tailstrict654          else655            std.mergePatch(target_object[k], patch[k]) tailstrict656        for k in std.setDiff(both_fields, null_fields)657      }658    else659      patch,660661  objectFields(o)::662    std.objectFieldsEx(o, false),663664  objectFieldsAll(o)::665    std.objectFieldsEx(o, true),666667  objectHas(o, f)::668    std.objectHasEx(o, f, false),669670  objectHasAll(o, f)::671    std.objectHasEx(o, f, true),672673  objectValues(o)::674    [o[k] for k in std.objectFields(o)],675676  objectValuesAll(o)::677    [o[k] for k in std.objectFieldsAll(o)],678679  equals:: $intrinsic(equals),680681  resolvePath(f, r)::682    local arr = std.split(f, '/');683    std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),684685  prune(a)::686    local isContent(b) =687      if b == null then688        false689      else if std.isArray(b) then690        std.length(b) > 0691      else if std.isObject(b) then692        std.length(b) > 0693      else694        true;695    if std.isArray(a) then696      [std.prune(x) for x in a if isContent($.prune(x))]697    else if std.isObject(a) then {698      [x]: $.prune(a[x])699      for x in std.objectFields(a)700      if isContent(std.prune(a[x]))701    } else702      a,703704  findSubstr(pat, str)::705    if !std.isString(pat) then706      error 'findSubstr first parameter should be a string, got ' + std.type(pat)707    else if !std.isString(str) then708      error 'findSubstr second parameter should be a string, got ' + std.type(str)709    else710      local pat_len = std.length(pat);711      local str_len = std.length(str);712      if pat_len == 0 || str_len == 0 || pat_len > str_len then713        []714      else715        std.filter(function(i) str[i:i + pat_len] == pat, std.range(0, str_len - pat_len)),716717  find(value, arr)::718    if !std.isArray(arr) then719      error 'find second parameter should be an array, got ' + std.type(arr)720    else721      std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),722}
after · crates/jrsonnet-stdlib/src/std.jsonnet
1{2  local std = self,3  local id = std.id,45  # Those functions aren't normally located in stdlib6  length:: $intrinsic(length),7  type:: $intrinsic(type),8  makeArray:: $intrinsic(makeArray),9  codepoint:: $intrinsic(codepoint),10  objectFieldsEx:: $intrinsic(objectFieldsEx),11  objectHasEx:: $intrinsic(objectHasEx),12  primitiveEquals:: $intrinsic(primitiveEquals),13  modulo:: $intrinsic(modulo),14  floor:: $intrinsic(floor),15  ceil:: $intrinsic(ceil),16  extVar:: $intrinsic(extVar),17  native:: $intrinsic(native),18  filter:: $intrinsic(filter),19  char:: $intrinsic(char),20  encodeUTF8:: $intrinsic(encodeUTF8),21  md5:: $intrinsic(md5),22  trace:: $intrinsic(trace),23  id:: $intrinsic(id),24  parseJson:: $intrinsic(parseJson),2526  log:: $intrinsic(log),27  pow:: $intrinsic(pow),28  sqrt:: $intrinsic(sqrt),2930  sin:: $intrinsic(sin),31  cos:: $intrinsic(cos),32  tan:: $intrinsic(tan),33  asin:: $intrinsic(asin),34  acos:: $intrinsic(acos),35  atan:: $intrinsic(atan),3637  isString(v):: std.type(v) == 'string',38  isNumber(v):: std.type(v) == 'number',39  isBoolean(v):: std.type(v) == 'boolean',40  isObject(v):: std.type(v) == 'object',41  isArray(v):: std.type(v) == 'array',42  isFunction(v):: std.type(v) == 'function',4344  toString(a)::45    if std.type(a) == 'string' then a else '' + a,4647  substr(str, from, len)::48    assert std.isString(str) : 'substr first parameter should be a string, got ' + std.type(str);49    assert std.isNumber(from) : 'substr second parameter should be a string, got ' + std.type(from);50    assert std.isNumber(len) : 'substr third parameter should be a string, got ' + std.type(len);51    assert len >= 0 : 'substr third parameter should be greater than zero, got ' + len;52    std.join('', std.makeArray(std.max(0, std.min(len, std.length(str) - from)), function(i) str[i + from])),5354  startsWith(a, b)::55    if std.length(a) < std.length(b) then56      false57    else58      std.substr(a, 0, std.length(b)) == b,5960  endsWith(a, b)::61    if std.length(a) < std.length(b) then62      false63    else64      std.substr(a, std.length(a) - std.length(b), std.length(b)) == b,6566  lstripChars(str, chars)::67    if std.length(str) > 0 && std.member(chars, str[0]) then68      std.lstripChars(str[1:], chars)69    else70      str,7172  rstripChars(str, chars)::73    local len = std.length(str);74    if len > 0 && std.member(chars, str[len - 1]) then75      std.rstripChars(str[:len - 1], chars)76    else77      str,7879  stripChars(str, chars)::80    std.lstripChars(std.rstripChars(str, chars), chars),8182  stringChars(str)::83    std.makeArray(std.length(str), function(i) str[i]),8485  local parse_nat(str, base) =86    assert base > 0 && base <= 16 : 'integer base %d invalid' % base;87    // These codepoints are in ascending order:88    local zero_code = std.codepoint('0');89    local upper_a_code = std.codepoint('A');90    local lower_a_code = std.codepoint('a');91    local addDigit(aggregate, char) =92      local code = std.codepoint(char);93      local digit = if code >= lower_a_code then94        code - lower_a_code + 1095      else if code >= upper_a_code then96        code - upper_a_code + 1097      else98        code - zero_code;99      assert digit >= 0 && digit < base : '%s is not a base %d integer' % [str, base];100      base * aggregate + digit;101    std.foldl(addDigit, std.stringChars(str), 0),102103  parseInt(str)::104    assert std.isString(str) : 'Expected string, got ' + std.type(str);105    assert std.length(str) > 0 && str != '-' : 'Not an integer: "%s"' % [str];106    if str[0] == '-' then107      -parse_nat(str[1:], 10)108    else109      parse_nat(str, 10),110111  parseOctal(str)::112    assert std.isString(str) : 'Expected string, got ' + std.type(str);113    assert std.length(str) > 0 : 'Not an octal number: ""';114    parse_nat(str, 8),115116  parseHex(str)::117    assert std.isString(str) : 'Expected string, got ' + std.type(str);118    assert std.length(str) > 0 : 'Not hexadecimal: ""';119    parse_nat(str, 16),120121  split(str, c)::122    assert std.isString(str) : 'std.split first parameter should be a string, got ' + std.type(str);123    assert std.isString(c) : 'std.split second parameter should be a string, got ' + std.type(c);124    assert std.length(c) == 1 : 'std.split second parameter should have length 1, got ' + std.length(c);125    std.splitLimit(str, c, -1),126127  splitLimit(str, c, maxsplits)::128    assert std.isString(str) : 'std.splitLimit first parameter should be a string, got ' + std.type(str);129    assert std.isString(c) : 'std.splitLimit second parameter should be a string, got ' + std.type(c);130    assert std.length(c) == 1 : 'std.splitLimit second parameter should have length 1, got ' + std.length(c);131    assert std.isNumber(maxsplits) : 'std.splitLimit third parameter should be a number, got ' + std.type(maxsplits);132    local aux(str, delim, i, arr, v) =133      local c = str[i];134      local i2 = i + 1;135      if i >= std.length(str) then136        arr + [v]137      else if c == delim && (maxsplits == -1 || std.length(arr) < maxsplits) then138        aux(str, delim, i2, arr + [v], '') tailstrict139      else140        aux(str, delim, i2, arr, v + c) tailstrict;141    aux(str, c, 0, [], ''),142143  strReplace:: $intrinsic(strReplace),144145  asciiUpper(str)::146    local cp = std.codepoint;147    local up_letter(c) = if cp(c) >= 97 && cp(c) < 123 then148      std.char(cp(c) - 32)149    else150      c;151    std.join('', std.map(up_letter, std.stringChars(str))),152153  asciiLower(str)::154    local cp = std.codepoint;155    local down_letter(c) = if cp(c) >= 65 && cp(c) < 91 then156      std.char(cp(c) + 32)157    else158      c;159    std.join('', std.map(down_letter, std.stringChars(str))),160161  range:: $intrinsic(range),162163  repeat(what, count)::164    local joiner =165      if std.isString(what) then ''166      else if std.isArray(what) then []167      else error 'std.repeat first argument must be an array or a string';168    std.join(joiner, std.makeArray(count, function(i) what)),169170  slice:: $intrinsic(slice),171172  member(arr, x)::173    if std.isArray(arr) then174      std.count(arr, x) > 0175    else if std.isString(arr) then176      std.length(std.findSubstr(x, arr)) > 0177    else error 'std.member first argument must be an array or a string',178179  count(arr, x):: std.length(std.filter(function(v) v == x, arr)),180181  mod:: $intrinsic(mod),182183  map:: $intrinsic(map),184185  mapWithIndex(func, arr)::186    if !std.isFunction(func) then187      error ('std.mapWithIndex first param must be function, got ' + std.type(func))188    else if !std.isArray(arr) && !std.isString(arr) then189      error ('std.mapWithIndex second param must be array, got ' + std.type(arr))190    else191      std.makeArray(std.length(arr), function(i) func(i, arr[i])),192193  mapWithKey(func, obj)::194    if !std.isFunction(func) then195      error ('std.mapWithKey first param must be function, got ' + std.type(func))196    else if !std.isObject(obj) then197      error ('std.mapWithKey second param must be object, got ' + std.type(obj))198    else199      { [k]: func(k, obj[k]) for k in std.objectFields(obj) },200201  flatMap:: $intrinsic(flatMap),202203  join:: $intrinsic(join),204205  lines(arr)::206    std.join('\n', arr + ['']),207208  deepJoin(arr)::209    if std.isString(arr) then210      arr211    else if std.isArray(arr) then212      std.join('', [std.deepJoin(x) for x in arr])213    else214      error 'Expected string or array, got %s' % std.type(arr),215216217  format:: $intrinsic(format),218219  foldr:: $intrinsic(foldr),220221  foldl:: $intrinsic(foldl),222223  filterMap(filter_func, map_func, arr)::224    if !std.isFunction(filter_func) then225      error ('std.filterMap first param must be function, got ' + std.type(filter_func))226    else if !std.isFunction(map_func) then227      error ('std.filterMap second param must be function, got ' + std.type(map_func))228    else if !std.isArray(arr) then229      error ('std.filterMap third param must be array, got ' + std.type(arr))230    else231      std.map(map_func, std.filter(filter_func, arr)),232233  assertEqual(a, b)::234    if a == b then235      true236    else237      error 'Assertion failed. ' + a + ' != ' + b,238239  abs(n)::240    if !std.isNumber(n) then241      error 'std.abs expected number, got ' + std.type(n)242    else243      if n > 0 then n else -n,244245  sign(n)::246    if !std.isNumber(n) then247      error 'std.sign expected number, got ' + std.type(n)248    else249      if n > 0 then250        1251      else if n < 0 then252        -1253      else 0,254255  max(a, b)::256    if !std.isNumber(a) then257      error 'std.max first param expected number, got ' + std.type(a)258    else if !std.isNumber(b) then259      error 'std.max second param expected number, got ' + std.type(b)260    else261      if a > b then a else b,262263  min(a, b)::264    if !std.isNumber(a) then265      error 'std.min first param expected number, got ' + std.type(a)266    else if !std.isNumber(b) then267      error 'std.min second param expected number, got ' + std.type(b)268    else269      if a < b then a else b,270271  clamp(x, minVal, maxVal)::272    if x < minVal then minVal273    else if x > maxVal then maxVal274    else x,275276  flattenArrays(arrs)::277    std.foldl(function(a, b) a + b, arrs, []),278279  manifestIni(ini)::280    local body_lines(body) =281      std.join([], [282        local value_or_values = body[k];283        if std.isArray(value_or_values) then284          ['%s = %s' % [k, value] for value in value_or_values]285        else286          ['%s = %s' % [k, value_or_values]]287288        for k in std.objectFields(body)289      ]);290291    local section_lines(sname, sbody) = ['[%s]' % [sname]] + body_lines(sbody),292          main_body = if std.objectHas(ini, 'main') then body_lines(ini.main) else [],293          all_sections = [294      section_lines(k, ini.sections[k])295      for k in std.objectFields(ini.sections)296    ];297    std.join('\n', main_body + std.flattenArrays(all_sections) + ['']),298299  manifestToml(value):: std.manifestTomlEx(value, '  '),300301  manifestTomlEx(value, indent)::302    local303      escapeStringToml = std.escapeStringJson,304      escapeKeyToml(key) =305        local bare_allowed = std.set(std.stringChars('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-'));306        if std.setUnion(std.set(std.stringChars(key)), bare_allowed) == bare_allowed then key else escapeStringToml(key),307      isTableArray(v) = std.isArray(v) && std.length(v) > 0 && std.foldl(function(a, b) a && std.isObject(b), v, true),308      isSection(v) = std.isObject(v) || isTableArray(v),309      renderValue(v, indexedPath, inline, cindent) =310        if v == true then311          'true'312        else if v == false then313          'false'314        else if v == null then315          error 'Tried to manifest "null" at ' + indexedPath316        else if std.isNumber(v) then317          '' + v318        else if std.isString(v) then319          escapeStringToml(v)320        else if std.isFunction(v) then321          error 'Tried to manifest function at ' + indexedPath322        else if std.isArray(v) then323          if std.length(v) == 0 then324            '[]'325          else326            local range = std.range(0, std.length(v) - 1);327            local new_indent = if inline then '' else cindent + indent;328            local separator = if inline then ' ' else '\n';329            local lines = ['[' + separator]330                          + std.join([',' + separator],331                                     [332                                       [new_indent + renderValue(v[i], indexedPath + [i], true, '')]333                                       for i in range334                                     ])335                          + [separator + (if inline then '' else cindent) + ']'];336            std.join('', lines)337        else if std.isObject(v) then338          local lines = ['{ ']339                        + std.join([', '],340                                   [341                                     [escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], true, '')]342                                     for k in std.objectFields(v)343                                   ])344                        + [' }'];345          std.join('', lines),346      renderTableInternal(v, path, indexedPath, cindent) =347        local kvp = std.flattenArrays([348          [cindent + escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], false, cindent)]349          for k in std.objectFields(v)350          if !isSection(v[k])351        ]);352        local sections = [std.join('\n', kvp)] + [353          (354            if std.isObject(v[k]) then355              renderTable(v[k], path + [k], indexedPath + [k], cindent)356            else357              renderTableArray(v[k], path + [k], indexedPath + [k], cindent)358          )359          for k in std.objectFields(v)360          if isSection(v[k])361        ];362        std.join('\n\n', sections),363      renderTable(v, path, indexedPath, cindent) =364        cindent + '[' + std.join('.', std.map(escapeKeyToml, path)) + ']'365        + (if v == {} then '' else '\n')366        + renderTableInternal(v, path, indexedPath, cindent + indent),367      renderTableArray(v, path, indexedPath, cindent) =368        local range = std.range(0, std.length(v) - 1);369        local sections = [370          (cindent + '[[' + std.join('.', std.map(escapeKeyToml, path)) + ']]'371           + (if v[i] == {} then '' else '\n')372           + renderTableInternal(v[i], path, indexedPath + [i], cindent + indent))373          for i in range374        ];375        std.join('\n\n', sections);376    if std.isObject(value) then377      renderTableInternal(value, [], [], '')378    else379      error 'TOML body must be an object. Got ' + std.type(value),380381  escapeStringJson:: $intrinsic(escapeStringJson),382383  escapeStringPython(str)::384    std.escapeStringJson(str),385386  escapeStringBash(str_)::387    local str = std.toString(str_);388    local trans(ch) =389      if ch == "'" then390        "'\"'\"'"391      else392        ch;393    "'%s'" % std.join('', [trans(ch) for ch in std.stringChars(str)]),394395  escapeStringDollars(str_)::396    local str = std.toString(str_);397    local trans(ch) =398      if ch == '$' then399        '$$'400      else401        ch;402    std.foldl(function(a, b) a + trans(b), std.stringChars(str), ''),403404  manifestJson(value):: std.manifestJsonEx(value, '    '),405406  manifestJsonEx:: $intrinsic(manifestJsonEx),407408  manifestYamlDoc(value, indent_array_in_object=false)::409    local aux(v, path, cindent) =410      if v == true then411        'true'412      else if v == false then413        'false'414      else if v == null then415        'null'416      else if std.isNumber(v) then417        '' + v418      else if std.isString(v) then419        local len = std.length(v);420        if len == 0 then421          '""'422        else if v[len - 1] == '\n' then423          local split = std.split(v, '\n');424          std.join('\n' + cindent + '  ', ['|'] + split[0:std.length(split) - 1])425        else426          std.escapeStringJson(v)427      else if std.isFunction(v) then428        error 'Tried to manifest function at ' + path429      else if std.isArray(v) then430        if std.length(v) == 0 then431          '[]'432        else433          local params(value) =434            if std.isArray(value) && std.length(value) > 0 then {435              // While we could avoid the new line, it yields YAML that is436              // hard to read, e.g.:437              // - - - 1438              //     - 2439              //   - - 3440              //     - 4441              new_indent: cindent + '  ',442              space: '\n' + self.new_indent,443            } else if std.isObject(value) && std.length(value) > 0 then {444              new_indent: cindent + '  ',445              // In this case we can start on the same line as the - because the indentation446              // matches up then.  The converse is not true, because fields are not always447              // 1 character long.448              space: ' ',449            } else {450              // In this case, new_indent is only used in the case of multi-line strings.451              new_indent: cindent,452              space: ' ',453            };454          local range = std.range(0, std.length(v) - 1);455          local parts = [456            '-' + param.space + aux(v[i], path + [i], param.new_indent)457            for i in range458            for param in [params(v[i])]459          ];460          std.join('\n' + cindent, parts)461      else if std.isObject(v) then462        if std.length(v) == 0 then463          '{}'464        else465          local params(value) =466            if std.isArray(value) && std.length(value) > 0 then {467              // Not indenting allows e.g.468              // ports:469              // - 80470              // instead of471              // ports:472              //   - 80473              new_indent: if indent_array_in_object then cindent + '  ' else cindent,474              space: '\n' + self.new_indent,475            } else if std.isObject(value) && std.length(value) > 0 then {476              new_indent: cindent + '  ',477              space: '\n' + self.new_indent,478            } else {479              // In this case, new_indent is only used in the case of multi-line strings.480              new_indent: cindent,481              space: ' ',482            };483          local lines = [484            std.escapeStringJson(k) + ':' + param.space + aux(v[k], path + [k], param.new_indent)485            for k in std.objectFields(v)486            for param in [params(v[k])]487          ];488          std.join('\n' + cindent, lines);489    aux(value, [], ''),490491  manifestYamlStream(value, indent_array_in_object=false, c_document_end=true)::492    if !std.isArray(value) then493      error 'manifestYamlStream only takes arrays, got ' + std.type(value)494    else495      '---\n' + std.join(496        '\n---\n', [std.manifestYamlDoc(e, indent_array_in_object) for e in value]497      ) + if c_document_end then '\n...\n' else '\n',498499500  manifestPython(v)::501    if std.isObject(v) then502      local fields = [503        '%s: %s' % [std.escapeStringPython(k), std.manifestPython(v[k])]504        for k in std.objectFields(v)505      ];506      '{%s}' % [std.join(', ', fields)]507    else if std.isArray(v) then508      '[%s]' % [std.join(', ', [std.manifestPython(v2) for v2 in v])]509    else if std.isString(v) then510      '%s' % [std.escapeStringPython(v)]511    else if std.isFunction(v) then512      error 'cannot manifest function'513    else if std.isNumber(v) then514      std.toString(v)515    else if v == true then516      'True'517    else if v == false then518      'False'519    else if v == null then520      'None',521522  manifestPythonVars(conf)::523    local vars = ['%s = %s' % [k, std.manifestPython(conf[k])] for k in std.objectFields(conf)];524    std.join('\n', vars + ['']),525526  manifestXmlJsonml(value)::527    if !std.isArray(value) then528      error 'Expected a JSONML value (an array), got %s' % std.type(value)529    else530      local aux(v) =531        if std.isString(v) then532          v533        else534          local tag = v[0];535          local has_attrs = std.length(v) > 1 && std.isObject(v[1]);536          local attrs = if has_attrs then v[1] else {};537          local children = if has_attrs then v[2:] else v[1:];538          local attrs_str =539            std.join('', [' %s="%s"' % [k, attrs[k]] for k in std.objectFields(attrs)]);540          std.deepJoin(['<', tag, attrs_str, '>', [aux(x) for x in children], '</', tag, '>']);541542      aux(value),543544  local base64_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',545  local base64_inv = { [base64_table[i]]: i for i in std.range(0, 63) },546547  base64:: $intrinsic(base64),548549  base64DecodeBytes(str)::550    if std.length(str) % 4 != 0 then551      error 'Not a base64 encoded string "%s"' % str552    else553      local aux(str, i, r) =554        if i >= std.length(str) then555          r556        else557          // all 6 bits of i, 2 MSB of i+1558          local n1 = [base64_inv[str[i]] << 2 | (base64_inv[str[i + 1]] >> 4)];559          // 4 LSB of i+1, 4MSB of i+2560          local n2 =561            if str[i + 2] == '=' then []562            else [(base64_inv[str[i + 1]] & 15) << 4 | (base64_inv[str[i + 2]] >> 2)];563          // 2 LSB of i+2, all 6 bits of i+3564          local n3 =565            if str[i + 3] == '=' then []566            else [(base64_inv[str[i + 2]] & 3) << 6 | base64_inv[str[i + 3]]];567          aux(str, i + 4, r + n1 + n2 + n3) tailstrict;568      aux(str, 0, []),569570  base64Decode(str)::571    local bytes = std.base64DecodeBytes(str);572    std.join('', std.map(function(b) std.char(b), bytes)),573574  reverse:: $intrinsic(reverse),575576  sortImpl:: $intrinsic(sortImpl),577578  sort(arr, keyF=id)::579    std.sortImpl(arr, keyF),580581  uniq(arr, keyF=id)::582    local f(a, b) =583      if std.length(a) == 0 then584        [b]585      else if keyF(a[std.length(a) - 1]) == keyF(b) then586        a587      else588        a + [b];589    std.foldl(f, arr, []),590591  set(arr, keyF=id)::592    std.uniq(std.sort(arr, keyF), keyF),593594  setMember(x, arr, keyF=id)::595    // TODO(dcunnin): Binary chop for O(log n) complexity596    std.length(std.setInter([x], arr, keyF)) > 0,597598  setUnion(a, b, keyF=id)::599    // NOTE: order matters, values in `a` win600    local aux(a, b, i, j, acc) =601      if i >= std.length(a) then602        acc + b[j:]603      else if j >= std.length(b) then604        acc + a[i:]605      else606        local ak = keyF(a[i]);607        local bk = keyF(b[j]);608        if ak == bk then609          aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict610        else if ak < bk then611          aux(a, b, i + 1, j, acc + [a[i]]) tailstrict612        else613          aux(a, b, i, j + 1, acc + [b[j]]) tailstrict;614    aux(a, b, 0, 0, []),615616  setInter(a, b, keyF=id)::617    local aux(a, b, i, j, acc) =618      if i >= std.length(a) || j >= std.length(b) then619        acc620      else621        if keyF(a[i]) == keyF(b[j]) then622          aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict623        else if keyF(a[i]) < keyF(b[j]) then624          aux(a, b, i + 1, j, acc) tailstrict625        else626          aux(a, b, i, j + 1, acc) tailstrict;627    aux(a, b, 0, 0, []) tailstrict,628629  setDiff(a, b, keyF=id)::630    local aux(a, b, i, j, acc) =631      if i >= std.length(a) then632        acc633      else if j >= std.length(b) then634        acc + a[i:]635      else636        if keyF(a[i]) == keyF(b[j]) then637          aux(a, b, i + 1, j + 1, acc) tailstrict638        else if keyF(a[i]) < keyF(b[j]) then639          aux(a, b, i + 1, j, acc + [a[i]]) tailstrict640        else641          aux(a, b, i, j + 1, acc) tailstrict;642    aux(a, b, 0, 0, []) tailstrict,643644  mergePatch(target, patch)::645    if std.isObject(patch) then646      local target_object =647        if std.isObject(target) then target else {};648649      local target_fields =650        if std.isObject(target_object) then std.objectFields(target_object) else [];651652      local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];653      local both_fields = std.setUnion(target_fields, std.objectFields(patch));654655      {656        [k]:657          if !std.objectHas(patch, k) then658            target_object[k]659          else if !std.objectHas(target_object, k) then660            std.mergePatch(null, patch[k]) tailstrict661          else662            std.mergePatch(target_object[k], patch[k]) tailstrict663        for k in std.setDiff(both_fields, null_fields)664      }665    else666      patch,667668  objectFields(o)::669    std.objectFieldsEx(o, false),670671  objectFieldsAll(o)::672    std.objectFieldsEx(o, true),673674  objectHas(o, f)::675    std.objectHasEx(o, f, false),676677  objectHasAll(o, f)::678    std.objectHasEx(o, f, true),679680  objectValues(o)::681    [o[k] for k in std.objectFields(o)],682683  objectValuesAll(o)::684    [o[k] for k in std.objectFieldsAll(o)],685686  equals:: $intrinsic(equals),687688  resolvePath(f, r)::689    local arr = std.split(f, '/');690    std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),691692  prune(a)::693    local isContent(b) =694      if b == null then695        false696      else if std.isArray(b) then697        std.length(b) > 0698      else if std.isObject(b) then699        std.length(b) > 0700      else701        true;702    if std.isArray(a) then703      [std.prune(x) for x in a if isContent($.prune(x))]704    else if std.isObject(a) then {705      [x]: $.prune(a[x])706      for x in std.objectFields(a)707      if isContent(std.prune(a[x]))708    } else709      a,710711  findSubstr(pat, str)::712    if !std.isString(pat) then713      error 'findSubstr first parameter should be a string, got ' + std.type(pat)714    else if !std.isString(str) then715      error 'findSubstr second parameter should be a string, got ' + std.type(str)716    else717      local pat_len = std.length(pat);718      local str_len = std.length(str);719      if pat_len == 0 || str_len == 0 || pat_len > str_len then720        []721      else722        std.filter(function(i) str[i:i + pat_len] == pat, std.range(0, str_len - pat_len)),723724  find(value, arr)::725    if !std.isArray(arr) then726      error 'find second parameter should be an array, got ' + std.type(arr)727    else728      std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),729}