3 ms·
abstract class Fruit: prop name: string prop rotten: bool class Apple (Fruit): prop name: string = "apple" prop rotten: bool = Fals
by p4bl0 6y ago
abstract class Fruit:
prop name: string
prop rotten: bool
class Apple (Fruit):
prop name: string = "apple"
prop rotten: bool = False
class Orange (Fruit):
prop name: string = "orange"
prop rotten: bool = False
func add (a: List<Fruit>, b: List<Fruit>) -> List<Fruit>:
return foldl(lambda l, f: l.append(f), a, b)
func filter (p: Func<[Fruit], bool>, l: List<Fruit>) -> List<Fruit>:
return [f for f in l if p(f)]
func is_rotten (f: Fruit) -> bool:
return f.rotten
a1 = Apple()
a2 = Apple()
a3 = Apple()
a3.rotten = True
apples = [a1, a2, a3]
o1 = Orange()
o2 = Orange()
o3 = Orange()
o3.rotten = True
oranges = [o1, o2, o3]
fruits = add(apples, oranges)
print(len(filter(is_rotten, apples)), "/", len(apples))
> "1/3"
print(len(filter(is_rotten, oranges)), "/", len(oranges))
> "1/3"
print(len(filter(is_rotten, fruits)), "/", len(fruits))
> "2/6"
- tsimionescu 6y agoThis does nothing to elucidate the problem. 1/3 apples + 1/3 oranges could be 2/6 fruits, or 2/781 fruits, or any other number you want. In normal mathematical notation, 1/3 is interpreted as 1/3 of 1, and in that case 1/3 apple + 1/3 orange is 2/3 fruit. And of course this only works if 1 apple = 1 orange = 1 fruit, which means that they are the same unit of measure, or, equivalently they are of substitutable types. It's even debatable of it's correct to say that 'if I have 1/3 of an apple and 1/3 of an orange, I have 2/3 of a fruit', so depending on what you want to do with your apples, oranges and fruit, your hierarchy may in fact break Liskov substitution. For example, if 1 apple + 1 orange = 2 fruit, so 2 fruit - 1 apple = 1 orange, so 1 apple = 1 orange, which is not true.
- p4bl0 6y agoYou're being dishonest in your reasoning. > In normal mathematical notation, 1/3 is interpreted as 1/3 of 1 Why would you deny the possibility of 1 being 1 list of fruits rather than 1 fruit? In the original example, it was 1 table of students, not 1 student. Everything you say after that is based on this broken assumption.