Last active
December 27, 2021 16:55
-
-
Save thuwarakeshm/f1fefa214f4cdec164507912af56481b to your computer and use it in GitHub Desktop.
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
from pipe import chain | |
nested_list = [[1, 2, 3], [4, 5, 6], [7, [8, 9]]] | |
unfolded_list = list(nested_list | |
| chain | |
) | |
print(unfolded_list) |
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
from pipe import Pipe | |
@Pipe | |
def sqr(n: int = 1): | |
return n ** 2 | |
result = 10 | sqr | |
print(result) |
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
from typing import List | |
from pipe import Pipe, as_list, select | |
def fib(n: int = 1): | |
"""Recursively create fibonacci number""" | |
return n if n < 2 else fib(n-1)+fib(n-2) | |
@Pipe | |
def apply_fun(nums: List[int], fun): | |
"""Apply any function to list elements and create a new list""" | |
return nums | select(fun) | as_list | |
result = [5, 10, 15] | apply_fun(fib) | |
print(result) |
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
from pipe import dedup, groupby, where, select, sort | |
num_list = [1, 4, 2, 27, | |
6, 8, 10, 7, 13, 19, 21, 20, 7, 18, 27] | |
results = list(num_list | |
| groupby(lambda x: "Odd" if x % 2 == 1 else "Even") | |
) | |
print(results) |
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
from pipe import dedup, groupby, where, select, sort | |
num_list = [1, 4, 2, 27, | |
6, 8, 10, 7, 13, 19, 21, 20, 7, 18, 27] | |
results = list(num_list | |
| groupby(lambda x: "Odd" if x % 2 == 1 else "Even") | |
| select(lambda x: {x[0]: [y**2 for y in x[1]]}) | |
) | |
print(results) |
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
num_list_with_duplicates = [1, 4, 2, 27, | |
6, 8, 10, 7, 13, 19, 21, 20, 7, 18, 27] | |
# Remove duplicates from the list | |
num_list = list(dict.fromkeys(num_list_with_duplicates)) | |
# Filter for odd numbers | |
odd_list = [num for num in num_list if num % 2 == 1] | |
# Square the numbers | |
odd_square = list(map(lambda x: x**2, odd_list)) | |
# Sort values in ascending order | |
odd_square.sort() | |
print(odd_square) |
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
from pipe import dedup, where, select, sort | |
num_list_with_duplicates = [1, 4, 2, 27, | |
6, 8, 10, 7, 13, 19, 21, 20, 7, 18, 27] | |
# perform pipe oerations | |
results = list(num_list_with_duplicates | |
| dedup | |
| where(lambda x: x % 2 == 1) | |
| select(lambda x: x**2) | |
| sort | |
) | |
print(results) |
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
nested_list = [[1, 2, 3], [4, 5, 6], [7, [8, 9]]] | |
unfolded_list = [num for item in nested_list for num in item] | |
print(unfolded_list) |
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
from pipe import as_list, skip_while | |
result = [3, 4, 5, 3] | skip_while(lambda x: x < 5) | as_list | |
print(result) |
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
from pipe import as_list, take_while, where | |
result = [3, 4, 5, 3] | take_while(lambda x: x < 5) | as_list | |
print(f"take_while: {result}") | |
result2 = [3, 4, 5, 3] | where(lambda x: x < 5) | as_list | |
print(f"where: {result2}") |
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
from pipe import traverse | |
nested_list = [[1, 2, 3], [4, 5, 6], [7, [8, 9]]] | |
unfolded_list = list(nested_list | |
| traverse | |
) | |
print(unfolded_list) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment