Created
June 17, 2019 07:19
-
-
Save dcoles/02b7b73b1e4c4ea395f177b17e35f14f to your computer and use it in GitHub Desktop.
List implementation in Jsonnet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
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