ich mache gerade mal wieder Schabernack mit Python und habe list gesubclassed um eine Pipelining Syntax zum Filtern zu ermöglichen.
Ich denke nicht, dass dies aktuell wirklich nützlich ist; es dienst vornehmlich zur Unterhaltung und als Proof-of-Concept:
Code: Alles auswählen
#! /usr/bin/env python3
from typing import Any, Callable
class PipelineFilterableList(list):
def __or__(self, predicate: Callable[[Any], bool]):
return type(self)(filter(predicate, self))
def main():
items = PipelineFilterableList([3, 9, 5, 13, 12, 30])
filtered_items = items | (lambda x: x > 5) | (lambda x: x % 2 == 0)
print(filtered_items)
if __name__ == '__main__':
main()
Code: Alles auswählen
#! /usr/bin/env python3
from functools import partial
from typing import Callable, Iterable
class PipelinableList(list):
def __or__(self, mutator: Callable[[Iterable], Iterable]):
return type(self)(mutator(self))
def main():
items = PipelinableList([3, 9, 5, 13, 12, 30])
filtered_items = items | partial(filter, lambda x: x > 5) | partial(filter, lambda x: x % 2 == 0)
squared_filtered_items = filtered_items | partial(map, lambda x: x ** 2)
print(filtered_items)
print(squared_filtered_items)
if __name__ == '__main__':
main()
Code: Alles auswählen
#! /usr/bin/env python3
from typing import Any, Callable
class PipelinableList(list):
def __or__(self, predicate: Callable[[Any], bool]):
return type(self)(filter(predicate, self))
def __matmul__(self, predicate: Callable[[Any], Any]):
return type(self)(map(predicate, self))
def main():
items = PipelinableList([3, 9, 5, 13, 12, 30])
filtered_items = items | (lambda x: x > 5) | (lambda x: x % 2 == 0)
squared_filtered_items = filtered_items @ (lambda x: x ** 2)
print(filtered_items)
print(squared_filtered_items)
if __name__ == '__main__':
main()