Skip to content

Instantly share code, notes, and snippets.

@dcoles
Created June 17, 2019 07:19
Show Gist options
  • Save dcoles/02b7b73b1e4c4ea395f177b17e35f14f to your computer and use it in GitHub Desktop.
Save dcoles/02b7b73b1e4c4ea395f177b17e35f14f to your computer and use it in GitHub Desktop.
List implementation in Jsonnet
{
head(list)::
if list == [] then
error 'Can not take head of empty list'
else
list[0],
tail(list)::
if list == [] then
error 'Can not take tail of empty list'
else
list[1],
take(list, n)::
if n == 0 || list == [] then
[]
else
[self.head(list), self.take(self.tail(list), n - 1)],
length(list)::
local aux(list, count) =
if list == [] then
count
else
aux(self.tail(list), count + 1) tailstrict;
aux(list, 0),
toArray(list)::
local aux(list, acum) =
if list == [] then
acum
else
aux(self.tail(list), acum + [self.head(list)]) tailstrict;
aux(list, []),
repeat(x, n=-1)::
if n == 0 then
[]
else
[x, self.repeat(x, n - 1)],
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment