diff options
author | Vincent Ambo <tazjin@google.com> | 2019-07-04T10·18+0100 |
---|---|---|
committer | Vincent Ambo <tazjin@google.com> | 2019-07-04T10·18+0100 |
commit | f723b8b878a3c4a4687b9e337a875500bebb39b1 (patch) | |
tree | e85204cf042c355e90cff61c111e7d8cd15df311 /third_party/bazel/rules_haskell/haskell/private/set.bzl | |
parent | 2eb1dc26e42ffbdc168f05ef744bd4b4f3e4c36f (diff) |
feat(third_party/bazel): Check in rules_haskell from Tweag r/17
Diffstat (limited to 'third_party/bazel/rules_haskell/haskell/private/set.bzl')
-rw-r--r-- | third_party/bazel/rules_haskell/haskell/private/set.bzl | 150 |
1 files changed, 150 insertions, 0 deletions
diff --git a/third_party/bazel/rules_haskell/haskell/private/set.bzl b/third_party/bazel/rules_haskell/haskell/private/set.bzl new file mode 100644 index 000000000000..f5c6220f79d6 --- /dev/null +++ b/third_party/bazel/rules_haskell/haskell/private/set.bzl @@ -0,0 +1,150 @@ +"""Immutable sets that support efficient merging, traversal, and membership +check. +""" + +def _empty(): + """Create an empty set. + + Returns: + set, new empty set. + """ + return struct(_set_items = dict()) + +def _singleton(e): + """Create a set with single element `e` inside. + + Args: + e: The element to put in the set. + + Returns: + set, new set. + """ + r = dict() + r[e] = None + return struct(_set_items = r) + +def _is_member(s, e): + """Return true if `e` is in the set `s`. + + Args: + s: The set to inspect. + e: The element to search for. + + Result: + Bool, true if `e` is in `s`, false otherwise. + """ + return e in s._set_items + +def _insert(s, e): + """Insert an element into the set. + + Args: + s: Set to insert new element into. + e: The element to insert. + + Result: + A copy of set `s` with `s` element added. + """ + r = dict(s._set_items) + r[e] = None + return struct(_set_items = r) + +def _mutable_insert(s, e): + """The same as `set.insert`, but modifies the first argument in place. + + Args: + s: Set to insert new element into. + e: The element to insert. + + Result: + set `s` with `s` element added. + """ + s._set_items[e] = None + return s + +def _union(s0, s1): + """Return union of two sets. + + Args: + s0: One set. + s1: Another set. + + Result: + set, union of the two sets. + """ + r = dict(s0._set_items) + r.update(s1._set_items) + return struct(_set_items = r) + +def _mutable_union(s0, s1): + """Modify set `s0` adding elements from `s1` to it. + + Args: + s0: One set. + s1: Another set. + + Result: + set, union of the two sets. + """ + s0._set_items.update(s1._set_items) + return s0 + +def _map(s, f): + """Map elements of given set using a function. + + Args: + s: Original set. + f: Function to apply to elements of the set. + + Result: + set with elements obtained by application of function `f` to the + elements of `s`. + """ + return struct(_set_items = {f(x): None for x in s._set_items.keys()}) + +def _from_list(l): + """Create a set containing elements from given list. + + Args: + l: List, source of the elements for the new set. + + Result: + set containing elements from given list. + """ + return (struct(_set_items = {x: None for x in l})) + +def _to_list(s): + """Convert set into a list of its elements. + + Args: + s: Set to convert. + + Returns: + List of elements of the set. + """ + return s._set_items.keys() + +def _to_depset(s): + """Similar to `set.to_list`, but produces a depset. + + Args: + s: Set to convert. + + Returns: + Depset of elements from the set. + """ + return depset(_to_list(s)) + +set = struct( + empty = _empty, + singleton = _singleton, + is_member = _is_member, + insert = _insert, + mutable_insert = _mutable_insert, + union = _union, + mutable_union = _mutable_union, + map = _map, + from_list = _from_list, + to_list = _to_list, + to_depset = _to_depset, +) |