[314] | 1 | from typing import TypeVar, Generic, Callable
|
---|
| 2 | from tudelft.utilities.immutablelist.AbstractImmutableList import AbstractImmutableList
|
---|
| 3 | from tudelft.utilities.immutablelist.ImmutableList import ImmutableList
|
---|
| 4 | IN1 = TypeVar('IN1')
|
---|
| 5 | OUT = TypeVar('OUT')
|
---|
| 6 |
|
---|
| 7 | class 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 |
|
---|