Created
February 24, 2023 20:45
-
-
Save umairsd/bd2f18aa4f3d68148be10a546d579e2a to your computer and use it in GitHub Desktop.
Experiment with Python Type Annotations.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Learned about Python type annotations. | |
from typing import Callable | |
from typing import Any | |
from typing import TypeVar | |
T = TypeVar('T') | |
S = TypeVar('S') | |
U = TypeVar('U') | |
def append(list1: [T], list2: [T]) -> [T]: | |
for item in list2: | |
list1.append(item) | |
return list1 | |
def concat(list_of_lists: [[T]]) -> [T]: | |
result: [T] = [] | |
for l in list_of_lists: | |
result = append(result, l) | |
return result | |
def _filter(function: Callable[[T], bool], items: [T]) -> [T]: | |
result: [T] = [] | |
for item in items: | |
if function(item): | |
result.append(item) | |
return result | |
def _length(items: [T]) -> int: | |
count = 0 | |
for item in items: | |
count += 1 | |
return count | |
def _map(function: Callable[[T], S], items: [T]) -> [S]: | |
result: [S] = [] | |
for item in items: | |
result.append(function(item)) | |
return result | |
def _foldl(function: Callable[[U, T], U], items: [T], initial: U) -> U: | |
result = initial | |
for item in items: | |
result = function(result, item) | |
return result | |
def _foldr(function: Callable[[T, U], U], items: [T], initial: U) -> U: | |
result = initial | |
for item in _reverse(items): | |
result = function(item, result) | |
return result | |
def _reverse(items: [T]) -> [T]: | |
result = [] | |
for i in range(len(items) - 1, -1, -1): | |
result.append(items[i]) | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment