Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. -- X filter
  2. -- -- maybe some tests with List.all and List.any
  3. --
  4. -- intersperse
  5. -- X head
  6. -- X isEmpty
  7. -- X tail
  8. -- X init
  9. -- X last
  10.  
  11. use .base
  12. use .test.v1
  13.  
  14. ---
  15.  
  16. filter : (a -> Boolean) -> [a] -> [a]
  17. filter f as =
  18. step acc a = if f a then acc :+ a else acc
  19. List.foldl step [] as
  20.  
  21. test> tests.filter.constTrueIsIdentity = runs 100 ' let
  22. xs = !(list nat)
  23. filter (const true) xs == xs |> expect
  24.  
  25. test> tests.filter.constFalseIsEmpty = runs 100 ' let
  26. filter (const false) !(list nat) |> isEmpty |> expect
  27.  
  28. ---
  29.  
  30. last : [a] -> Optional a
  31. last as = case as of
  32. [] -> None
  33. _ :+ a -> Some a
  34.  
  35. > last [1,2,3]
  36.  
  37. test> tests.last.lastOfEmpty = run (expect (last [] == None))
  38. test> tests.last.lastOfSnoc = runs 100 ' let
  39. x = !nat
  40. expect (last (!(list nat) :+ x) == Some x)
  41.  
  42. ---
  43.  
  44. init : [a] -> Optional [a]
  45. init as = case as of
  46. [] -> None
  47. as :+ _ -> Some as
  48.  
  49. > init [1,2,3]
  50. > init []
  51.  
  52. test> tests.init.initOfEmpty = run (expect (init [] == None))
  53. test> tests.init.initOfSnoc = runs 100 ' let
  54. xs = !(list nat)
  55. expect (init (xs :+ !nat) == Some xs)
  56.  
  57. ---
  58. tail : [a] -> Optional [a]
  59. tail as = case as of
  60. [] -> None
  61. _ +: as -> Some as
  62.  
  63. > tail [1,2,3]
  64. > tail []
  65.  
  66. test> tests.tail.tailOfEmpty = run (expect (tail [] == None))
  67. test> tests.head.tailOfCons = runs 100 ' let
  68. xs = !(list nat)
  69. expect (tail (!nat +: xs) == Some xs)
  70.  
  71. ---
  72.  
  73. isEmpty : [a] -> Boolean
  74. isEmpty as = size as Nat.== 0
  75.  
  76. test> tests.isEmpty.emptyIsEmpty = run (expect (isEmpty []))
  77.  
  78. tests.gens.nonEmpty : '{Gen} [Nat]
  79. tests.gens.nonEmpty = '(!nat +: !(list nat))
  80.  
  81. test> tests.isEmpty.consIsNonempty = runs 100 ' let
  82. expect (not (isEmpty !nonEmpty))
  83.  
  84. ---
  85. ---
  86.  
  87. head : [a] -> Optional a
  88. head a = List.at 0 a
  89.  
  90. > head [1,2,3]
  91. > head []
  92.  
  93. test> tests.head.headOfEmpty = run (expect (head [] == None))
  94.  
  95. test> tests.head.headOfCons = runs 100 ' let
  96. use .test
  97. x = !nat
  98. expect (head (x +: !(list nat)) == Some x)
  99.  
  100. ---- Anything below this line is ignored by Unison.
  101.  
  102. pull git@github.com:unisonweb/unisonbase.git
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement