source: utilitiespy/tudelft/utilities/immutablelist/MapList.py@ 1310

Last change on this file since 1310 was 314, checked in by wouter, 3 years ago

#105 added JoinedList, MapList, Tuple, release 1.0.5

File size: 1.2 KB
RevLine 
[314]1from typing import TypeVar, Generic, Callable
2from tudelft.utilities.immutablelist.AbstractImmutableList import AbstractImmutableList
3from tudelft.utilities.immutablelist.ImmutableList import ImmutableList
4IN1 = TypeVar('IN1')
5OUT = TypeVar('OUT')
6
7class MapList(Generic[IN1, OUT], AbstractImmutableList[OUT]):
8 '''
9 @param <IN1> the type of the elements inside the argument list
10 @param <OUT> the output type of the function
11
12 '''
13
14 def __init__(self, f:Callable[[IN1], OUT], list1: ImmutableList[IN1]):
15 '''
16 creates a list [f(a1), f(a2) ,. ..., f(an)].
17
18 @param f {@link Function}. Note, previously this was our own Function
19 class, which was identical to the new one built in in Java.
20 @param list1 a list of items [a1,a2,..., an]
21 '''
22 if f == None or list1 == None:
23 raise ValueError("null argument")
24 self._list1 = list1
25 self._f = f
26
27 #Override
28 def get(self, index:int) -> OUT :
29 return self._f(self._list1.get(index))
30
31 #Override
32 def size(self)->int:
33 return self._list1.size()
34
35 #Override
36 def __hash__(self):
37 return hash((self._list1, self._f))
38
39 #Override
40 def __eq__(self, other):
41 return isinstance(other, self.__class__) \
42 and self._list1 == other._list1 \
43 and self._f == other._f
44
Note: See TracBrowser for help on using the repository browser.