Skip to content

Instantly share code, notes, and snippets.

View tangentlin's full-sized avatar

Tianzhen Lin (Tangent) tangentlin

View GitHub Profile
@tangentlin
tangentlin / credit-card-processing-imperative.ts
Created March 31, 2024 01:20
Imperative programming example
function processPayment(amount: number, method: string) {
switch (method) {
case 'credit':
// Process credit card payment
break;
case 'paypal':
// Process PayPal payment
break;
case 'bank_transfer':
// Process bank transfer
@tangentlin
tangentlin / credit-card-processing-declarative.ts
Created March 31, 2024 01:24
A contrived declarative approach of declarative programming
interface PaymentMethod {
process(amount: number): void;
}
const paymentMethods: Record<string, PaymentMethod> = {
credit: {
process(amount: number) {
console.log(`Processing $${amount} via Credit Card`);
// Credit card processing logic...
}
interface CreditCardType {
name: string;
logoUri: string;
validate: (number: string) => boolean;
format: (number: string) => string;
}
const creditCardTypes: Record<string, CreditCardType> = {
visa: {
name: "Visa",
function validateAndFormatCard(number: string, type: string) {
const cardType = creditCardTypes[type];
if (!cardType) {
throw new Error('Unsupported card type');
}
const isValid = cardType.validate(number);
const formattedNumber = cardType.format(number);
return {
isValid,
formattedNumber,