- Delegate work to helper objects.
- Use objects from different concrete classes to get different implementations for helper methods.
- strategy, state, etc.
- (from refactoring.guru) object A contains objects B; B can live without A.
class Solution: | |
def intToRoman(self, num: int) -> str: | |
numerals = { | |
1: 'I', | |
4: 'IV', | |
5: 'V', | |
9: 'IX', | |
10: 'X', | |
40: 'XL', | |
50: 'L', |
const x = { | |
a:{ | |
b:{ | |
c:'neat' | |
} | |
} | |
} | |
const loop = (object, path) => { | |
let index = 0 |
def addAllNumbersFromZeroToN(n): | |
if n > 0: | |
return n + addAllNumbersFromZeroToN(n-1) | |
return n | |
print(addAllNumbersFromZeroToN(3)) #6 | |
print(addAllNumbersFromZeroToN(2)) #3 | |
print(addAllNumbersFromZeroToN(5)) #15 |
const useRecords = () => { | |
const [record, setRecord] = useState<Record<string, string>>({ | |
'record 1': 'foo', | |
'record 2': 'foo', | |
'record 3': 'bar', | |
}) | |
return { | |
get uniqueRecords() { | |
const uniqueRecords = new Set(Object.values(record)) |
import { memo, useEffect, useRef } from "react" | |
const Dialog: React.FC<{ open: boolean, className?:string, children:React.ReactElement }> = ({ open, className, children }) => { | |
const dialogRef = useRef<HTMLDialogElement>(null) | |
useEffect(() => { | |
if (open) { | |
if(dialogRef.current?.open){ | |
dialogRef.current?.close() | |
} |
const createExposedPromise = <T>() => { | |
let resolve: (value: T | PromiseLike<T>) => void | |
let reject: (reason: any) => void | |
const promise = new Promise<T>((res, rej) => { | |
resolve = res | |
reject = rej | |
}) | |
return { |
const pubSub = <UpdateType>() => { | |
type Subscriber = ({ update, unsubscribe }: { update: UpdateType; unsubscribe: () => void }) => void | |
const subscribers = new Set<Subscriber>() | |
const subscribe = (sub: Subscriber) => subscribers.add(sub) | |
const publish = (update: UpdateType) => { | |
subscribers.forEach((sub) => { | |
const unsubscribe = () => subscribers.delete(sub) | |
sub({ update, unsubscribe }) |
Why is it cool?