- Pattern match.
- Keep consistent level of abstraction per function.
- Don't mix pure functions with IO functions. Segregate.
- Build upon ubiquituous domain language.
- Define Interfaces for service layer around infrastructure and application.
- Partial function composition == Dependency Injection. But it is not a preferred functional pattern.
- Don't YAGNI
- If you find yourself passing parameters and adding conditional..the abstraction is incorrect.
- Build upon these foundational low-level architectural design patterns:
- Value objects, Business Domains, Service Layers
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
def fib(n): | |
# print(f"for fib {n}") | |
a, b = 0, 1 | |
if n == a: return a | |
if n == b: return b | |
return do_fib(n-1, b, a+b) | |
def do_fib(n, /, a=0, b=1): | |
if n == 0: return a |
- Design in isolation (Partition)
- 2 business modules must communicate over message pattern. They are not considered business modules if one requires the other, in which case they don’t need to communicate over messages if a business process has to die, all interconnected business modules must die
- process are stateless
- fault tolerant implies scalability implies concurrency and concurrent business processes
- Availability
- its better to die on fault and restart, then drag and build up the pile of error
- when a process dies, fault detection and communicating fault is important
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
defmodule ToHex do | |
@hex_base 16 | |
def to_hex(0), do: 0 | |
def to_hex(num) when is_number(num) do | |
div(num, @hex_base) | |
|> IO.inspect | |
|> then(& {to_hex(&1), rem(num, @hex_base)}) |
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
x = [1,2,3] | |
if len(x) < 3: | |
return x | |
else: | |
return [4,5,6] |
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 numbers import Number | |
def selection_sort(arr: list[Number]) -> list[Number]: | |
n = len(arr) | |
swap_index = 0 | |
for start_idx, start_num in enumerate(arr): | |
min_num = float('inf') | |
for idx in range(start_idx, n): | |
num = arr[idx] |
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
# ==================================== # | |
VOWELS = ['a', 'e', 'i', 'o', 'u', "A", "E", "I", "O", "U"] | |
def calc_epigram(sentence: str) -> int: | |
return sum(ord(char) if char not in VOWELS else -ord(char) for char in filter(lambda x: x.isalpha(), sentence)) | |
assert (calc_epigram("why and how")) == 569 |
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
# ================ Palindrome ==================== # | |
def is_num_palindrome(num: int) -> bool: | |
num = abs(num) | |
while num > 9: | |
exponent = math.floor(math.log10(num)) | |
msb, remainder = divmod(num, 10 ** exponent) | |
num, lsb = divmod(remainder, 10) | |
if msb != lsb: | |
return False |
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
import math | |
# ================ Int to Roman ==================== # | |
RomanMapping = { | |
1: 'I', | |
4: 'IV', | |
5: 'V', | |
9: 'IX', | |
10: 'X', | |
40: 'XL', |
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
""" | |
Given two strings s and t of lengths m and n respectively, | |
return the minimum window substring of s such that every character in t (including duplicates) | |
is included in the window. | |
If there is no such substring, return the empty string "". | |
A substring is a contiguous sequence of characters within the string. | |
This is fucking hard. |