From 9ec7bc277c45bc22c1c839abe5bec3651e7a5ecd Mon Sep 17 00:00:00 2001 From: Jonas Wanke Date: Thu, 21 Nov 2024 21:48:10 +0100 Subject: [PATCH] Add list.isEmpty(), .lastIndex() --- packages_v5/example.candy | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/packages_v5/example.candy b/packages_v5/example.candy index cf4bbbe06..6f8ae94e2 100644 --- a/packages_v5/example.candy +++ b/packages_v5/example.candy @@ -116,6 +116,7 @@ struct List[T] = builtin fun listFilled[T](length: Int, item: T) List[T] { builtinListFilled(length, item) } +# TODO: listGenerate(…) fun listOf[T]() List[T] { builtinListOf[T]() } @@ -137,6 +138,15 @@ fun listOf[T](item0: T, item1: T, item2: T, item3: T, item4: T) List[T] { fun length[T](list: List[T]) Int { builtinListLength(list) } +fun isEmpty[T](list: List[T]) Bool { + list.length().equals(0) +} +fun lastIndex[T](list: List[T]) Maybe[Int] { + switch list.isEmpty() { + true => none[Int](), + false => some(list.length().subtract(1)), + } +} fun get[T](list: List[T], index: Int) Maybe[T] { builtinListGet(list, index) } @@ -164,11 +174,11 @@ fun append[T](list: List[T], item: T) List[T] { fun replace[T](list: List[T], index: Int, item: T) List[T] { builtinListReplace(list, index, item) } -# TODO: list.update +# TODO: list.update(…) fun removeAt[T](list: List[T], index: Int) List[T] { builtinListRemoveAt(list, index) } - +# TODO: list.getRange(…), .concatenate(…), .firstIndexWhere(…), .firstWhere(…), .firstIndexOf(…), .lastIndexWhere(…), .lastWhere(…), .lastIndexOf(…) fun print[T: ToText](t: T) { builtinPrint(t.toText()) }