26
26
from .maptree import MapTree
27
27
28
28
29
- Key = TypeVar ("Key" , bound = SupportsLessThan )
30
- Value = TypeVar ("Value" )
31
- Result = TypeVar ("Result" )
29
+ _Key = TypeVar ("_Key" , bound = SupportsLessThan )
30
+ _Key_ = TypeVar ("_Key_" , bound = SupportsLessThan )
31
+ _Value = TypeVar ("_Value" )
32
+ _Result = TypeVar ("_Result" )
32
33
33
- T1 = TypeVar ("T1 " )
34
- T2 = TypeVar ("T2 " )
35
- T3 = TypeVar ("T3 " )
36
- T4 = TypeVar ("T4 " )
37
- T5 = TypeVar ("T5 " )
38
- T6 = TypeVar ("T6 " )
34
+ _T1 = TypeVar ("_T1 " )
35
+ _T2 = TypeVar ("_T2 " )
36
+ _T3 = TypeVar ("_T3 " )
37
+ _T4 = TypeVar ("_T4 " )
38
+ _T5 = TypeVar ("_T5 " )
39
+ _T6 = TypeVar ("_T6 " )
39
40
40
41
41
- class Map (Mapping [Key , Value ], PipeMixin ):
42
+ class Map (Mapping [_Key , _Value ], PipeMixin ):
42
43
"""The immutable map class."""
43
44
44
- def __init__ (self , __tree : MapTree [Key , Value ] | None = None ) -> None :
45
- self ._tree : MapTree [Key , Value ] = __tree if __tree else maptree .empty
45
+ def __init__ (self , __tree : MapTree [_Key , _Value ] | None = None ) -> None :
46
+ self ._tree : MapTree [_Key , _Value ] = __tree if __tree else maptree .empty
46
47
47
- def add (self , key : Key , value : Value ) -> Map [Key , Value ]:
48
+ def add (self , key : _Key , value : _Value ) -> Map [_Key , _Value ]:
48
49
return Map (maptree .add (key , value , self ._tree ))
49
50
50
51
@staticmethod
51
- def create (ie : Iterable [tuple [Key , Value ]]) -> Map [Key , Value ]:
52
+ def create (ie : Iterable [tuple [_Key , _Value ]]) -> Map [_Key , _Value ]:
52
53
return create (ie )
53
54
54
- def contains_key (self , key : Key ) -> bool :
55
+ def contains_key (self , key : _Key ) -> bool :
55
56
return maptree .mem (key , self ._tree )
56
57
57
- def change (self , key : Key , f : Callable [[Option [Value ]], Option [Value ]]) -> Map [Key , Value ]:
58
+ def change (self , key : _Key , f : Callable [[Option [_Value ]], Option [_Value ]]) -> Map [_Key , _Value ]:
58
59
return Map (maptree .change (key , f , self ._tree ))
59
60
60
61
@staticmethod
61
- def empty () -> Map [Key , Value ]:
62
+ def empty () -> Map [_Key , _Value ]:
62
63
return Map (maptree .empty )
63
64
64
65
def is_empty (self ) -> bool :
65
66
return maptree .is_empty (self ._tree )
66
67
67
- def exists (self , predicate : Callable [[Key , Value ], bool ]) -> bool :
68
+ def exists (self , predicate : Callable [[_Key , _Value ], bool ]) -> bool :
68
69
return maptree .exists (predicate , self ._tree )
69
70
70
- def filter (self , predicate : Callable [[Key , Value ], bool ]) -> Map [Key , Value ]:
71
+ def filter (self , predicate : Callable [[_Key , _Value ], bool ]) -> Map [_Key , _Value ]:
71
72
return Map (maptree .filter (predicate , self ._tree ))
72
73
73
- def for_all (self , predicate : Callable [[Key , Value ], bool ]) -> bool :
74
+ def for_all (self , predicate : Callable [[_Key , _Value ], bool ]) -> bool :
74
75
"""Test all elements in map.
75
76
76
77
Returns true if the given predicate returns true for all of
@@ -85,19 +86,19 @@ def for_all(self, predicate: Callable[[Key, Value], bool]) -> bool:
85
86
"""
86
87
return maptree .forall (predicate , self ._tree )
87
88
88
- def iterate (self , f : Callable [[Key , Value ], None ]) -> None :
89
+ def iterate (self , f : Callable [[_Key , _Value ], None ]) -> None :
89
90
return maptree .iter (f , self ._tree )
90
91
91
92
# def MapRange (f:'Value->'Result) =
92
93
# return Map<'Key, 'Result>(comparer, maptree.map f tree)
93
94
94
- def fold (self , folder : Callable [[Result , tuple [Key , Value ]], Result ], state : Result ) -> Result :
95
+ def fold (self , folder : Callable [[_Result , tuple [_Key , _Value ]], _Result ], state : _Result ) -> _Result :
95
96
return maptree .fold (folder , state , self ._tree )
96
97
97
- def fold_back (self , folder : Callable [[tuple [Key , Value ], Result ], Result ], state : Result ) -> Result :
98
+ def fold_back (self , folder : Callable [[tuple [_Key , _Value ], _Result ], _Result ], state : _Result ) -> _Result :
98
99
return maptree .fold_back (folder , self ._tree , state )
99
100
100
- def map (self , mapping : Callable [[Key , Value ], Result ]) -> Map [Key , Result ]:
101
+ def map (self , mapping : Callable [[_Key , _Value ], _Result ]) -> Map [_Key , _Result ]:
101
102
"""Map the mapping.
102
103
103
104
Builds a new collection whose elements are the results of
@@ -113,7 +114,7 @@ def map(self, mapping: Callable[[Key, Value], Result]) -> Map[Key, Result]:
113
114
"""
114
115
return Map (maptree .map (mapping , self ._tree ))
115
116
116
- def partition (self , predicate : Callable [[Key , Value ], bool ]) -> tuple [Map [Key , Value ], Map [Key , Value ]]:
117
+ def partition (self , predicate : Callable [[_Key , _Value ], bool ]) -> tuple [Map [_Key , _Value ], Map [_Key , _Value ]]:
117
118
r1 , r2 = maptree .partition (predicate , self ._tree )
118
119
return Map (r1 ), Map (r2 )
119
120
@@ -131,43 +132,43 @@ def partition(self, predicate: Callable[[Key, Value], bool]) -> tuple[Map[Key, V
131
132
132
133
# return default
133
134
134
- def items (self ) -> ItemsView [Key , Value ]:
135
+ def items (self ) -> ItemsView [_Key , _Value ]:
135
136
items = maptree .to_seq (self ._tree )
136
137
return ItemsView (dict (items ))
137
138
138
- def remove (self , key : Key ) -> Map [Key , Value ]:
139
+ def remove (self , key : _Key ) -> Map [_Key , _Value ]:
139
140
return Map (maptree .remove (key , self ._tree ))
140
141
141
- def to_list (self ) -> Block [tuple [Key , Value ]]:
142
+ def to_list (self ) -> Block [tuple [_Key , _Value ]]:
142
143
return maptree .to_list (self ._tree )
143
144
144
- def to_seq (self ) -> Iterable [tuple [Key , Value ]]:
145
+ def to_seq (self ) -> Iterable [tuple [_Key , _Value ]]:
145
146
"""Convert to sequence.
146
147
147
148
Returns:
148
149
Sequence of key, value tuples.
149
150
"""
150
151
return maptree .to_seq (self ._tree )
151
152
152
- def try_get_value (self , key : Key , value : list [Value ]):
153
+ def try_get_value (self , key : _Key , value : list [_Value ]):
153
154
for v in maptree .try_find (key , self ._tree ).to_list ():
154
155
value .append (v )
155
156
return True
156
157
else :
157
158
return False
158
159
159
- def try_find (self , key : Key ) -> Option [Value ]:
160
+ def try_find (self , key : _Key ) -> Option [_Value ]:
160
161
return maptree .try_find (key , self ._tree )
161
162
162
- def try_pick (self , chooser : Callable [[Key , Value ], Option [Result ]]) -> Option [Result ]:
163
+ def try_pick (self , chooser : Callable [[_Key , _Value ], Option [_Result ]]) -> Option [_Result ]:
163
164
return maptree .try_pick (chooser , self ._tree )
164
165
165
166
@staticmethod
166
- def of (** args : Value ) -> Map [str , Value ]:
167
+ def of (** args : _Result ) -> Map [str , _Result ]:
167
168
return Map (maptree .of_seq (args .items ()))
168
169
169
170
@staticmethod
170
- def of_block (lst : Block [tuple [Key , Value ]]) -> Map [Key , Value ]:
171
+ def of_block (lst : Block [tuple [_Key , _Value ]]) -> Map [_Key , _Value ]:
171
172
"""Generate map from list.
172
173
173
174
Returns:
@@ -176,7 +177,7 @@ def of_block(lst: Block[tuple[Key, Value]]) -> Map[Key, Value]:
176
177
return of_block (lst )
177
178
178
179
@staticmethod
179
- def of_list (lst : list [tuple [Key , Value ]]) -> Map [Key , Value ]:
180
+ def of_list (lst : list [tuple [_Key_ , _Result ]]) -> Map [_Key_ , _Result ]:
180
181
"""Generate map from list.
181
182
182
183
Returns:
@@ -185,7 +186,7 @@ def of_list(lst: list[tuple[Key, Value]]) -> Map[Key, Value]:
185
186
return of_list (lst )
186
187
187
188
@staticmethod
188
- def of_seq (sequence : Iterable [tuple [Key , Value ]]) -> Map [Key , Value ]:
189
+ def of_seq (sequence : Iterable [tuple [_Key_ , _Result ]]) -> Map [_Key_ , _Result ]:
189
190
"""Generate map from sequence.
190
191
191
192
Generates a new map from an iterable of key/value tuples. This
@@ -206,10 +207,10 @@ def combine_hash(x: int, y: int) -> int:
206
207
res = combine_hash (res , hash (y ))
207
208
return res
208
209
209
- def __getitem__ (self , k : Key ) -> Value :
210
+ def __getitem__ (self , k : _Key ) -> _Value :
210
211
return maptree .find (k , self ._tree )
211
212
212
- def __iter__ (self ) -> Iterator [Key ]:
213
+ def __iter__ (self ) -> Iterator [_Key ]:
213
214
xs = maptree .mk_iterator (self ._tree )
214
215
return (k for (k , _ ) in xs )
215
216
@@ -241,7 +242,7 @@ def __bool__(self) -> bool:
241
242
return not maptree .is_empty (self ._tree )
242
243
243
244
def __str__ (self ) -> str :
244
- def to_str (item : tuple [Key , Value ]) -> str :
245
+ def to_str (item : tuple [_Key , _Value ]) -> str :
245
246
key , value = item
246
247
if isinstance (key , str ):
247
248
return f'("{ key } ", { value } )'
@@ -255,7 +256,7 @@ def __repr__(self) -> str:
255
256
256
257
257
258
@curry_flip (1 )
258
- def add (table : Map [Key , Value ], key : Key , value : Value ) -> Map [Key , Value ]:
259
+ def add (table : Map [_Key , _Value ], key : _Key , value : _Value ) -> Map [_Key , _Value ]:
259
260
"""Add key with value to map.
260
261
261
262
Returns a new map with the binding added to the given map. If a
@@ -276,7 +277,7 @@ def add(table: Map[Key, Value], key: Key, value: Value) -> Map[Key, Value]:
276
277
277
278
278
279
@curry_flip (1 )
279
- def change (table : Map [Key , Value ], key : Key , fn : Callable [[Option [Value ]], Option [Value ]]) -> Map [Key , Value ]:
280
+ def change (table : Map [_Key , _Value ], key : _Key , fn : Callable [[Option [_Value ]], Option [_Value ]]) -> Map [_Key , _Value ]:
280
281
"""Change element in map.
281
282
282
283
Returns a new map with the value stored under key changed
@@ -294,7 +295,7 @@ def change(table: Map[Key, Value], key: Key, fn: Callable[[Option[Value]], Optio
294
295
295
296
296
297
@curry_flip (1 )
297
- def contains_key (table : Map [Key , Any ], key : Key ) -> bool :
298
+ def contains_key (table : Map [_Key , Any ], key : _Key ) -> bool :
298
299
return table .contains_key (key )
299
300
300
301
@@ -303,12 +304,12 @@ def count(table: Map[Any, Any]) -> int:
303
304
return len (table )
304
305
305
306
306
- def create (ie : Iterable [tuple [Key , Value ]]) -> Map [Key , Value ]:
307
+ def create (ie : Iterable [tuple [_Key , _Value ]]) -> Map [_Key , _Value ]:
307
308
return Map (maptree .of_seq (ie ))
308
309
309
310
310
311
@curry_flip (1 )
311
- def find (table : Map [Key , Value ], key : Key ) -> Value :
312
+ def find (table : Map [_Key , _Value ], key : _Key ) -> _Value :
312
313
"""Find element with key in map.
313
314
314
315
Lookup an element in the map, raising KeyNotFoundException if no
@@ -334,15 +335,15 @@ def is_empty(table: Map[Any, Any]) -> bool:
334
335
return table .is_empty ()
335
336
336
337
337
- def iterate (action : Callable [[Key , Value ], None ]) -> Callable [[Map [Key , Value ]], None ]:
338
- def _iterate (table : Map [Key , Value ]) -> None :
338
+ def iterate (action : Callable [[_Key , _Value ], None ]) -> Callable [[Map [_Key , _Value ]], None ]:
339
+ def _iterate (table : Map [_Key , _Value ]) -> None :
339
340
return table .iterate (action )
340
341
341
342
return _iterate
342
343
343
344
344
345
@curry_flip (1 )
345
- def try_pick (table : Map [Key , Value ], chooser : Callable [[Key , Value ], Option [Result ]]) -> Option [Result ]:
346
+ def try_pick (table : Map [_Key , _Value ], chooser : Callable [[_Key , _Value ], Option [_Result ]]) -> Option [_Result ]:
346
347
"""Pick element in map.
347
348
348
349
Searches the map looking for the first element where the given
@@ -361,15 +362,15 @@ def try_pick(table: Map[Key, Value], chooser: Callable[[Key, Value], Option[Resu
361
362
362
363
363
364
@curry_flip (1 )
364
- def pick (table : Map [Key , Value ], chooser : Callable [[Key , Value ], Option [Result ]]) -> Result :
365
+ def pick (table : Map [_Key , _Value ], chooser : Callable [[_Key , _Value ], Option [_Result ]]) -> _Result :
365
366
for res in table .try_pick (chooser ):
366
367
return res
367
368
else :
368
369
raise KeyError ()
369
370
370
371
371
372
@curry_flip (1 )
372
- def exists (table : Map [Key , Value ], predicate : Callable [[Key , Value ], bool ]) -> bool :
373
+ def exists (table : Map [_Key , _Value ], predicate : Callable [[_Key , _Value ], bool ]) -> bool :
373
374
"""Test if element exists in map.
374
375
375
376
Returns true if the given predicate returns true for one of the
@@ -388,47 +389,47 @@ def exists(table: Map[Key, Value], predicate: Callable[[Key, Value], bool]) -> b
388
389
389
390
390
391
@curry_flip (1 )
391
- def filter (table : Map [Key , Value ], predicate : Callable [[Key , Value ], bool ]) -> Map [Key , Value ]:
392
+ def filter (table : Map [_Key , _Value ], predicate : Callable [[_Key , _Value ], bool ]) -> Map [_Key , _Value ]:
392
393
return table .filter (predicate )
393
394
394
395
395
396
@curry_flip (1 )
396
- def for_all (table : Map [Key , Value ], predicate : Callable [[Key , Value ], bool ]) -> bool :
397
+ def for_all (table : Map [_Key , _Value ], predicate : Callable [[_Key , _Value ], bool ]) -> bool :
397
398
return table .for_all (predicate )
398
399
399
400
400
401
@curry_flip (1 )
401
- def map (table : Map [Key , Value ], mapping : Callable [[Key , Value ], Result ]) -> Map [Key , Result ]:
402
+ def map (table : Map [_Key , _Value ], mapping : Callable [[_Key , _Value ], _Result ]) -> Map [_Key , _Result ]:
402
403
return table .map (mapping )
403
404
404
405
405
406
@curry_flip (1 )
406
407
def fold (
407
- table : Map [Key , Value ],
408
- folder : Callable [[Result , tuple [Key , Value ]], Result ],
409
- state : Result ,
410
- ) -> Result :
408
+ table : Map [_Key , _Value ],
409
+ folder : Callable [[_Result , tuple [_Key , _Value ]], _Result ],
410
+ state : _Result ,
411
+ ) -> _Result :
411
412
return table .fold (folder , state )
412
413
413
414
414
415
@curry_flip (1 )
415
416
def fold_back (
416
- state : Result ,
417
- folder : Callable [[tuple [Key , Value ], Result ], Result ],
418
- table : Map [Key , Value ],
419
- ) -> Result :
417
+ state : _Result ,
418
+ folder : Callable [[tuple [_Key , _Value ], _Result ], _Result ],
419
+ table : Map [_Key , _Value ],
420
+ ) -> _Result :
420
421
return table .fold_back (folder , state )
421
422
422
423
423
424
@curry_flip (1 )
424
425
def partition (
425
- table : Map [Key , Value ], predicate : Callable [[Key , Value ], bool ]
426
- ) -> tuple [Map [Key , Value ], Map [Key , Value ]]:
426
+ table : Map [_Key , _Value ], predicate : Callable [[_Key , _Value ], bool ]
427
+ ) -> tuple [Map [_Key , _Value ], Map [_Key , _Value ]]:
427
428
return table .partition (predicate )
428
429
429
430
430
431
@curry_flip (1 )
431
- def remove (table : Map [Key , Value ], key : Key ) -> Map [Key , Value ]:
432
+ def remove (table : Map [_Key , _Value ], key : _Key ) -> Map [_Key , _Value ]:
432
433
"""Remove element from map.
433
434
434
435
Removes an element from the domain of the map. No exception is
@@ -444,33 +445,33 @@ def remove(table: Map[Key, Value], key: Key) -> Map[Key, Value]:
444
445
return table .remove (key )
445
446
446
447
447
- def of (** args : Value ) -> Map [str , Value ]:
448
+ def of (** args : _Value ) -> Map [str , _Value ]:
448
449
"""Create map from arguments."""
449
450
return Map (maptree .of_seq (args .items ()))
450
451
451
452
452
- def of_block (elements : Block [tuple [Key , Value ]]) -> Map [Key , Value ]:
453
+ def of_block (elements : Block [tuple [_Key , _Value ]]) -> Map [_Key , _Value ]:
453
454
return Map (maptree .of_list (elements ))
454
455
455
456
456
- def of_list (elements : list [tuple [Key , Value ]]) -> Map [Key , Value ]:
457
+ def of_list (elements : list [tuple [_Key , _Value ]]) -> Map [_Key , _Value ]:
457
458
return Map (maptree .of_list (Block (elements )))
458
459
459
460
460
- def of_seq (elements : Iterable [tuple [Key , Value ]]) -> Map [Key , Value ]:
461
+ def of_seq (elements : Iterable [tuple [_Key , _Value ]]) -> Map [_Key , _Value ]:
461
462
return Map (maptree .of_seq (elements ))
462
463
463
464
464
- def to_list (table : Map [Key , Value ]) -> Block [tuple [Key , Value ]]:
465
+ def to_list (table : Map [_Key , _Value ]) -> Block [tuple [_Key , _Value ]]:
465
466
return table .to_list ()
466
467
467
468
468
- def to_seq (table : Map [Key , Value ]) -> Iterable [tuple [Key , Value ]]:
469
+ def to_seq (table : Map [_Key , _Value ]) -> Iterable [tuple [_Key , _Value ]]:
469
470
return table .to_seq ()
470
471
471
472
472
473
@curry_flip (1 )
473
- def try_find (table : Map [Key , Value ], key : Key ) -> Option [Value ]:
474
+ def try_find (table : Map [_Key , _Value ], key : _Key ) -> Option [_Value ]:
474
475
"""Try to find element with key in map.
475
476
476
477
Lookup an element in the map, returning a `Some` value if the
0 commit comments