Skip to content

Instantly share code, notes, and snippets.

@pchiusano
Created August 5, 2019 21:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pchiusano/973a0c7cb43e390e319ab4749e6c0abc to your computer and use it in GitHub Desktop.
Save pchiusano/973a0c7cb43e390e319ab4749e6c0abc to your computer and use it in GitHub Desktop.
Some list functions for Unison, scratch file log
-- 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 git@github.com:unisonweb/unisonbase.git
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment