tobogganSe connecter

louise

The Louise language: compiler, runtime, std, tooling

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

160 lignes · 2779 octetsBrut · Historique
1/// Collections of elements (ADR 0047): what every container in std
2/// offers, for code that works on any of them.
3///
4/// import std/collection
5///
6/// collection.size(dict) // as dict.size(dict)
7/// collection.sum([1, 2, 3]) // 6
8/// collection.contains(bytes, 0) // works on any collection
9///
10/// Each container's own module keeps its functions; this module adds
11/// the generic ones. A collection's element type is `Elem<c>`: `a` for
12/// `List<a>` and `Array<a>`, `(k, v)` for `Dict<k, v>`, `Int` for `Bytes`.
13
14import std/array.{type Array}
15import std/bytes
16import std/dict.{type Dict}
17import std/iter.{type Iter}
18import std/list
19
20pub trait Collection<c>
21{
22 /// What it holds.
23 type Elem
24 /// How many elements it holds.
25 fn size :: c: c -> Int
26 /// Combine the elements, in order.
27 fn fold :: c: c acc: b f: fn(b, Elem<c>) -> b / <e> -> b / <e>
28 /// The elements, in order.
29 fn to_list :: c: c -> List<Elem<c>>
30 /// The elements, in order, as a lazy iterator.
31 fn iter :: c: c -> Iter<Elem<c>, e>
32}
33
34impl Collection<List<a>>
35{
36 type Elem = a
37 fn size :: c
38 {
39 list.length c
40 }
41 fn fold :: c acc f
42 {
43 list.fold c acc f
44 }
45 fn to_list :: c
46 {
47 c
48 }
49 fn iter :: c
50 {
51 iter.from_list c
52 }
53}
54
55impl Collection<Array<a>>
56{
57 type Elem = a
58 fn size :: c
59 {
60 array.length c
61 }
62 fn fold :: c acc f
63 {
64 array.fold c acc f
65 }
66 fn to_list :: c
67 {
68 array.to_list c
69 }
70 fn iter :: c
71 {
72 iter.from_list (array.to_list c)
73 }
74}
75
76impl Collection<Dict<k, v>>
77{
78 type Elem = (k, v)
79 fn size :: c
80 {
81 dict.size c
82 }
83 fn fold :: c acc f
84 {
85 dict.fold c acc fn a k v { f a (k, v) }
86 }
87 fn to_list :: c
88 {
89 dict.to_list c
90 }
91 fn iter :: c
92 {
93 iter.from_list (dict.to_list c)
94 }
95}
96
97impl Collection<Bytes>
98{
99 type Elem = Int
100 fn size :: c
101 {
102 bytes.length c
103 }
104 fn fold :: c acc f
105 {
106 list.fold (bytes.to_list c) acc f
107 }
108 fn to_list :: c
109 {
110 bytes.to_list c
111 }
112 fn iter :: c
113 {
114 iter.from_list (bytes.to_list c)
115 }
116}
117
118/// Whether it holds no element.
119pub fn is_empty :: c: c -> Bool where Collection<c>
120{
121 size c == 0
122}
123
124/// How many elements satisfy `keep`.
125pub fn count ::
126 c: c
127 keep: fn(Elem<c>) -> Bool / <e>
128 -> Int / <e>
129 where Collection<c>
130{
131 fold
132 c
133 0
134 fn n x
135 {
136 if keep x
137 {
138 n + 1
139 }
140 else
141 {
142 n
143 }
144 }
145}
146
147/// Whether it holds `x`.
148pub fn contains :: c: c x: Elem<c> -> Bool where Collection<c>, Eq<Elem<c>>
149{
150 fold c False fn found y { found || y == x }
151}
152
153/// The sum of its elements; `default()` (0) when it is empty.
154pub fn sum ::
155 c: c
156 -> Elem<c>
157 where Collection<c>, Num<Elem<c>>, Default<Elem<c>>
158{
159 fold c (default ()) fn total x { total + x }
160}