This commit is contained in:
2025-10-22 21:34:42 +02:00
parent 33ca89a05e
commit 2fe784c4df
2 changed files with 88 additions and 6 deletions

View File

@ -385,4 +385,42 @@ module basicSpells {
assert(repeat(5,1) == [1,1,1,1,1]),
assert(repeat(2,true) == [true,true])
}
/// `listToSet(l)` is set of elements, generated from a list l.
///
/// - @param n: list of elements
/// - @returns a set of elements.
pure def listToSet(l:List[a]): Set[a] = l.foldl(Set(), (acc,e) => acc.union(Set(e)))
run listToSetTest = all {
assert(listToSet([1,2,3]) == Set(1,2,3)),
assert(listToSet([1,1,1]) == Set(1)),
}
pure def dropFirst(l: List[a], cond: (a) => bool): List[a] = {
pure val result = l.foldl(([], true), (acc, e) => {
if (acc._2 and not(cond(e))) {
(acc._1.append(e), true)
} else if (not(acc._2)) {
(acc._1.append(e), acc._2)
} else {
(acc._1, false)
}
})
result._1
}
run dropFirstTest = all {
assert(dropFirst([1, 5, 4, 3], (x) => x % 2 != 1) == [1, 5, 3]),
assert(dropFirst([1,2,3,4,5], (x) => x == 12) == [1,2,3,4,5])
}
pure def mapToSet(m: a -> b):Set[(a,b)] = m.keys().fold(Set(),(acc, k) => acc.union(Set((k,m.get(k)))))
run mapToSetTest = {
assert(mapToSet(Map(1 -> 2, 3 -> 6)) == Set((1,2),(3,6)))
}
}