Created
August 5, 2019 21:09
-
-
Save pchiusano/973a0c7cb43e390e319ab4749e6c0abc to your computer and use it in GitHub Desktop.
Some list functions for Unison, scratch file log
This file contains hidden or 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
-- X filter | |
-- -- maybe some tests with List.all and List.any | |
-- | |
-- intersperse | |
-- X head | |
-- X isEmpty | |
-- X tail | |
-- X init | |
-- X last | |
use .base | |
use .test.v1 | |
--- | |
filter : (a -> Boolean) -> [a] -> [a] | |
filter f as = | |
step acc a = if f a then acc :+ a else acc | |
List.foldl step [] as | |
test> tests.filter.constTrueIsIdentity = runs 100 ' let | |
xs = !(list nat) | |
filter (const true) xs == xs |> expect | |
test> tests.filter.constFalseIsEmpty = runs 100 ' let | |
filter (const false) !(list nat) |> isEmpty |> expect | |
--- | |
last : [a] -> Optional a | |
last as = case as of | |
[] -> None | |
_ :+ a -> Some a | |
> last [1,2,3] | |
test> tests.last.lastOfEmpty = run (expect (last [] == None)) | |
test> tests.last.lastOfSnoc = runs 100 ' let | |
x = !nat | |
expect (last (!(list nat) :+ x) == Some x) | |
--- | |
init : [a] -> Optional [a] | |
init as = case as of | |
[] -> None | |
as :+ _ -> Some as | |
> init [1,2,3] | |
> init [] | |
test> tests.init.initOfEmpty = run (expect (init [] == None)) | |
test> tests.init.initOfSnoc = runs 100 ' let | |
xs = !(list nat) | |
expect (init (xs :+ !nat) == Some xs) | |
--- | |
tail : [a] -> Optional [a] | |
tail as = case as of | |
[] -> None | |
_ +: as -> Some as | |
> tail [1,2,3] | |
> tail [] | |
test> tests.tail.tailOfEmpty = run (expect (tail [] == None)) | |
test> tests.head.tailOfCons = runs 100 ' let | |
xs = !(list nat) | |
expect (tail (!nat +: xs) == Some xs) | |
--- | |
isEmpty : [a] -> Boolean | |
isEmpty as = size as Nat.== 0 | |
test> tests.isEmpty.emptyIsEmpty = run (expect (isEmpty [])) | |
tests.gens.nonEmpty : '{Gen} [Nat] | |
tests.gens.nonEmpty = '(!nat +: !(list nat)) | |
test> tests.isEmpty.consIsNonempty = runs 100 ' let | |
expect (not (isEmpty !nonEmpty)) | |
--- | |
--- | |
head : [a] -> Optional a | |
head a = List.at 0 a | |
> head [1,2,3] | |
> head [] | |
test> tests.head.headOfEmpty = run (expect (head [] == None)) | |
test> tests.head.headOfCons = runs 100 ' let | |
use .test | |
x = !nat | |
expect (head (x +: !(list nat)) == Some x) | |
---- Anything below this line is ignored by Unison. | |
pull [email protected]:unisonweb/unisonbase.git |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment