Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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