Skip to content

Commit

Permalink
Add list.isSortedBy(…), .isStrictly(Ascending|Descending)(…)
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasWanke committed Dec 12, 2024
1 parent d62f705 commit ee7055c
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages_v5/example.candy
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,16 @@ fun any[T](list: List[T], predicate: (T) Bool) Bool {
list.all((it: T) { predicate(it).not() }).not()
}

fun isStrictlyAscending[T: Compare](list: List[T]) Bool {
list.isSortedBy((a: T, b: T) { a.compareTo(b).equals(Ordering.less()) })
}
fun isStrictlyDescending[T: Compare](list: List[T]) Bool {
list.isSortedBy((a: T, b: T) { a.compareTo(b).equals(Ordering.greater()) })
}
fun isSortedBy[T](list: List[T], checkPair: (T, T) Bool) Bool {
list.pairs().all((pair: Pair[T, T]) { checkPair(pair.first, pair.second) })
}

fun map[T, R](list: List[T], transform: (T) R) List[R] {
list.fold(listOf[R](), (result: List[R], item: T) {
result.append(transform(item))
Expand Down

0 comments on commit ee7055c

Please sign in to comment.