Source code for pyk.prelude.collections
1from __future__ import annotations
2
3from typing import TYPE_CHECKING
4
5from ..kast.inner import KApply, KLabel, KSort, build_assoc
6
7if TYPE_CHECKING:
8 from collections.abc import Iterable
9 from typing import Final
10
11 from ..kast import KInner
12
13SET: Final = KSort('Set')
14LIST: Final = KSort('List')
15MAP: Final = KSort('Map')
16BAG: Final = KSort('Bag')
17
18
[docs]
19def set_empty() -> KInner:
20 return KApply('.Set')
21
22
[docs]
23def set_item(k: KInner) -> KInner:
24 return KApply('SetItem', [k])
25
26
[docs]
27def set_of(ks: Iterable[KInner]) -> KInner:
28 return build_assoc(set_empty(), KLabel('_Set_'), map(set_item, ks))
29
30
[docs]
31def list_empty() -> KInner:
32 return KApply('.List')
33
34
[docs]
35def list_item(k: KInner) -> KInner:
36 return KApply('ListItem', [k])
37
38
[docs]
39def list_of(ks: Iterable[KInner]) -> KInner:
40 return build_assoc(list_empty(), KLabel('_List_'), map(list_item, ks))
41
42
[docs]
43def map_empty() -> KInner:
44 return KApply('.Map')
45
46
[docs]
47def map_item(k: KInner, v: KInner) -> KInner:
48 return KApply('_|->_', [k, v])
49
50
[docs]
51def map_of(ks: dict[KInner, KInner] | Iterable[tuple[KInner, KInner]]) -> KInner:
52 ks = dict(ks)
53 return build_assoc(map_empty(), KLabel('_Map_'), (map_item(k, v) for k, v in ks.items()))