2020-02-14 02:57:17 +03:00
|
|
|
|
```ucm:hide
|
2024-06-25 21:16:51 +03:00
|
|
|
|
scratch/main> builtins.merge lib.builtins
|
2020-02-14 02:57:17 +03:00
|
|
|
|
```
|
|
|
|
|
```unison:hide:all
|
|
|
|
|
List.adjacentPairs : [a] -> [(a, a)]
|
|
|
|
|
List.adjacentPairs as =
|
|
|
|
|
go xs acc =
|
2020-02-14 18:17:53 +03:00
|
|
|
|
match xs with
|
2020-02-14 02:57:17 +03:00
|
|
|
|
[x, y] ++ t -> go t (acc :+ (x, y))
|
|
|
|
|
_ -> acc
|
|
|
|
|
go as []
|
|
|
|
|
|
|
|
|
|
List.all : (a -> Boolean) -> [a] -> Boolean
|
|
|
|
|
List.all p xs =
|
2020-02-14 18:17:53 +03:00
|
|
|
|
match xs with
|
2020-02-14 02:57:17 +03:00
|
|
|
|
[] -> true
|
|
|
|
|
x +: xs -> (p x) && (List.all p xs)
|
|
|
|
|
|
|
|
|
|
List.any : (a -> Boolean) -> [a] -> Boolean
|
|
|
|
|
List.any p xs =
|
2020-02-14 18:17:53 +03:00
|
|
|
|
match xs with
|
2020-02-14 02:57:17 +03:00
|
|
|
|
[] -> false
|
|
|
|
|
x +: xs -> (p x) || (List.any p xs)
|
|
|
|
|
|
|
|
|
|
List.chunk : Nat -> [a] -> [[a]]
|
|
|
|
|
List.chunk n as =
|
|
|
|
|
go acc rest =
|
2020-02-14 18:17:53 +03:00
|
|
|
|
match splitAt n rest with
|
2020-02-14 02:57:17 +03:00
|
|
|
|
(c, []) -> acc :+ c
|
|
|
|
|
(c, cs) -> go (acc :+ c) cs
|
|
|
|
|
go [] as
|
|
|
|
|
|
|
|
|
|
List.chunksOf : Nat -> [a] -> [[a]]
|
|
|
|
|
List.chunksOf n text =
|
|
|
|
|
go acc text =
|
|
|
|
|
p = splitAt n text
|
2020-02-14 18:17:53 +03:00
|
|
|
|
match p with
|
2020-02-14 02:57:17 +03:00
|
|
|
|
([], _) -> acc
|
|
|
|
|
(a, b) -> go (acc :+ a) b
|
|
|
|
|
go [] text
|
|
|
|
|
|
|
|
|
|
List.dropWhile : (a -> Boolean) -> [a] -> [a]
|
|
|
|
|
List.dropWhile p xs =
|
2020-02-14 18:17:53 +03:00
|
|
|
|
match xs with
|
2020-02-14 02:57:17 +03:00
|
|
|
|
i +: l -> if p i then List.dropWhile p l else xs
|
|
|
|
|
_ -> []
|
|
|
|
|
|
|
|
|
|
List.first : [a] -> Optional a
|
|
|
|
|
List.first a = List.at 0 a
|
|
|
|
|
|
|
|
|
|
List.init : [a] -> Optional [a]
|
|
|
|
|
List.init as =
|
2020-02-14 18:17:53 +03:00
|
|
|
|
match as with
|
2020-02-14 02:57:17 +03:00
|
|
|
|
[] -> None
|
|
|
|
|
as :+ _ -> Some as
|
|
|
|
|
|
|
|
|
|
List.intersperse : a -> [a] -> [a]
|
|
|
|
|
List.intersperse a as =
|
|
|
|
|
go acc as =
|
2020-02-14 18:17:53 +03:00
|
|
|
|
match as with
|
2020-02-14 02:57:17 +03:00
|
|
|
|
[] -> acc
|
|
|
|
|
[x] -> acc :+ x
|
|
|
|
|
x +: xs -> go (acc :+ x :+ a) xs
|
|
|
|
|
go [] as
|
|
|
|
|
|
|
|
|
|
List.isEmpty : [a] -> Boolean
|
|
|
|
|
List.isEmpty as = List.size as == 0
|
|
|
|
|
|
|
|
|
|
List.last : [a] -> Optional a
|
|
|
|
|
List.last as =
|
2020-02-14 18:17:53 +03:00
|
|
|
|
match as with
|
2020-02-14 02:57:17 +03:00
|
|
|
|
[] -> None
|
|
|
|
|
_ :+ a -> Some a
|
|
|
|
|
|
|
|
|
|
List.replicate : Nat -> a -> [a]
|
|
|
|
|
List.replicate n a =
|
|
|
|
|
go n acc = if n == 0 then acc else go (Nat.drop n 1) (a +: acc)
|
|
|
|
|
go n []
|
|
|
|
|
|
|
|
|
|
List.splitAt : Nat -> [a] -> ([a], [a])
|
|
|
|
|
List.splitAt n as = (List.take n as, List.drop n as)
|
|
|
|
|
|
|
|
|
|
List.tail : [a] -> Optional [a]
|
|
|
|
|
List.tail as =
|
2020-02-14 18:17:53 +03:00
|
|
|
|
match as with
|
2020-02-14 02:57:17 +03:00
|
|
|
|
[] -> None
|
|
|
|
|
_ +: as -> Some as
|
|
|
|
|
|
|
|
|
|
List.takeWhile : (a ->{𝕖} Boolean) -> [a] ->{𝕖} [a]
|
|
|
|
|
List.takeWhile p xs =
|
|
|
|
|
go xs acc =
|
2020-02-14 18:17:53 +03:00
|
|
|
|
match xs with
|
2020-02-14 02:57:17 +03:00
|
|
|
|
x +: xs -> if p x then go xs (acc :+ x) else acc
|
|
|
|
|
_ -> acc
|
|
|
|
|
go xs []
|
|
|
|
|
```
|
|
|
|
|
```ucm:hide
|
2024-06-25 21:16:51 +03:00
|
|
|
|
scratch/main> add
|
2020-02-14 02:57:17 +03:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
The `alias.many` command can be used to copy definitions from the current namespace into your curated one.
|
2020-02-14 03:01:42 +03:00
|
|
|
|
The names that will be used in the target namespace are the names you specify, relative to the current namespace:
|
2020-02-14 02:57:17 +03:00
|
|
|
|
|
|
|
|
|
```
|
2024-06-12 01:22:09 +03:00
|
|
|
|
scratch/main> help alias.many
|
2020-02-14 03:12:38 +03:00
|
|
|
|
|
2020-02-14 02:57:17 +03:00
|
|
|
|
alias.many (or copy)
|
2020-02-14 03:12:38 +03:00
|
|
|
|
`alias.many <relative1> [relative2...] <namespace>` creates aliases `relative1`, `relative2`, ...
|
|
|
|
|
in the namespace `namespace`.
|
|
|
|
|
`alias.many foo.foo bar.bar .quux` creates aliases `.quux.foo.foo` and `.quux.bar.bar`.
|
2020-02-14 02:57:17 +03:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Let's try it!
|
|
|
|
|
|
|
|
|
|
```ucm
|
2024-06-25 21:16:51 +03:00
|
|
|
|
scratch/main> alias.many List.adjacentPairs List.all List.any List.chunk List.chunksOf List.dropWhile List.first List.init List.intersperse List.isEmpty List.last List.replicate List.splitAt List.tail List.takeWhile mylib
|
2024-06-12 01:22:09 +03:00
|
|
|
|
scratch/main> find-in mylib
|
2020-02-14 02:57:17 +03:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Thanks, `alias.many`!
|