-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from SWAT-engineering/utility-modules-for-list…
…s-and-maps Utility modules for lists and maps
- Loading branch information
Showing
7 changed files
with
53 additions
and
59 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module util::ListUtil | ||
|
||
import List; | ||
|
||
@synopsis{ | ||
Representation of a traversal direction along a list | ||
} | ||
|
||
data Direction // Traverse lists... | ||
= forward() // - ...from left to right; | ||
| backward() // - ...from right to left. | ||
; | ||
|
||
@synopsis{ | ||
Reorder a list according to the specified direction | ||
} | ||
|
||
list[&T] reorder(list[&T] l, forward()) = l; | ||
list[&T] reorder(list[&T] l, backward()) = reverse(l); | ||
|
||
@synopsis{ | ||
Removes multiple occurrences of elements in a list. The last occurrence | ||
remains (cf. `List::dup`). | ||
} | ||
|
||
list[&T] dupLast(list[&T] l) = reverse(dup(reverse(l))); // TODO: Optimize/avoid `reverse`-ing? | ||
|
||
@synopsis{ | ||
Checks if list `l1` is a strict prefix of list `l2` | ||
} | ||
|
||
bool isStrictPrefix(list[&T] l1, list[&T] l2) | ||
= size(l1) < size(l2) && l1 == l2[..size(l1)]; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module util::MapUtil | ||
|
||
@synopsis{ | ||
Updates the mapping of each key `k` in map of lists `m` to be the union of: | ||
(1) the existing list `m[k]`, and (2) the new elements-to-be-inserted | ||
`values[k]`. For instance: | ||
- m = ("foo": [1, 2, 3], "bar": [], "baz": [1, 2]) | ||
- values = ("foo": [4, 5], "bar": [123], "qux": [3, 4]) | ||
- return = ("foo": [1, 2, 3, 4, 5], "bar": [123], "baz": [1, 2]) | ||
} | ||
|
||
map[&K, list[&V]] insertIn(map[&K, list[&V]] m, map[&K, &V] values) | ||
= (k: m[k] + (k in values ? [values[k]] : []) | k <- m); |