Skip to content

Instantly share code, notes, and snippets.

@fResult
fResult / contravariant-functor.hs
Last active September 1, 2024 17:33
Create the model from various data format with the Contravariant Functor. Learned from Mathematics for Working Programmers - Day 5
import Data.Functor.Contravariant
import Data.List (init)
import Data.Text (Text, pack, splitOn, unpack)
data Person = Person
{ name :: String
, lastName :: String
, age :: Int
}
deriving (Show)
@fResult
fResult / contravariant-functor.ts
Last active August 31, 2024 07:56
Inspired by Haskell (learned from Mathematics for Working Programmers - Day 5), but this still concrete. It doesn't receive another Functor type, except for `Predicate`.
// NOTE: The idea is to lift the function from `T => boolean`
// up to the `Predicate<T>` functor and down to the `T => boolean` again.
// This indicates that a Functor is not actually the *Container with Map* or *Mappable Container*.
interface ContravariantFunctor<T> {
map<R>(f: (x: R) => T): Predicate<R>
}
// Predicate (functor) is the one instance of ContravariantFunctor type
class Predicate<T = unknown> implements ContravariantFunctor<any> {
@fResult
fResult / Functor.md
Last active August 19, 2024 18:43
Functor - from Mathematics for Working Programmer Day 5

Functor

Functor F... Let $\Bbb C$, $\Bbb D$ be categories. A Functor $F: \Bbb C\to \Bbb D$ is defined by 2 mappings

  1. Object map $F : Obj_{\Bbb{C}}\to Obj_{\Bbb{D}}$ such that

    $$A \to F_{Obj}(A)$$

  2. Morphism map $F_{Hom}:Hom_{\Bbb C}\to Hom_{\Bbb C}$ such that

$$(f:A\to B) \to F_{Obj}(f): F_{Obj}(A)\to F_{Obj}(B)$$

import Data.Maybe (fromJust, isNothing)
sqr :: Float -> Float
sqr n = n * n
half :: Float -> Float
half = (/ 2)
hypotenuse :: Float -> Float -> Float
hypotenuse a b = sqrt $ sqr a + sqr b
@fResult
fResult / is-not-odd.ts
Last active February 22, 2024 10:05
Make meme for the `is-odd` module
// Usage:
import isEven from "is-not-odd";
isEven(3); // false
isEven(4); // true
// In index.ts of `is-not-odd` module
import isOdd from "is-odd";
type PredicateFn<T> = (...args: [T, ...any[]]) => boolean;
@fResult
fResult / FutureResponseMapperForReactQuery.ts
Last active February 18, 2024 10:17
Higher Order Functions in mapping data fetching Strapi API integrate with React Query
import { compose, isEmpty, map } from 'lodash/fp';
import { useQuery } from '@tanstack/react-query';
// USAGE
// This makes us read "mapStrapiResponse toStaticContent AFTER fetchSomeStaticPageContent"
const fetchAndMapContent: () => Promise<SomeStaticContentAttrs> = compose(
mapStrapiResponse(toStaticContent),
fetchSomeStaticPageContent,
);
const pageContent: SomeStaticContentAttrs = await fetchAndMapContent();
@fResult
fResult / Maximum Sum Segment problem - Naive Solution.ts
Last active February 1, 2024 18:20
copy from the Haskell code in the "Mathematics for Working Programmer class - Day 3-4"
// MSS - Maximum Sum Segment problem
// MSS = maximum . map sum . concat . map inits . tails
// Playground - https://tsplay.dev/mpzE6w
function tail<T>(xs: T[]): T[] {
return xs.slice(1)
}
function init<T>(xs: T[]): T[] {
return xs.slice(0, -1)
}
export type UnaryFn<T, R> = (arg: T) => R;
export function compose<R>(fn: UnaryFn<any, R>): UnaryFn<any, R>;
export function compose<T1, R>(fn1: UnaryFn<T1, R>): UnaryFn<T1, R>;
export function compose<T1, T2, R>(
fn2: UnaryFn<T2, R>,
fn1: UnaryFn<T1, T2>
): UnaryFn<T1, R>;
export function compose<T1, T2, T3, R>(
fn3: UnaryFn<T3, R>,
import IconFacebook from '../components/icons/IconFacebook'
import IconTwitter from '../components/icons/IconTwitter'
function YourPage() {
const [hoveredIcons, setHoveredIcons] = useState({
fb: false,
tw: false,
ig: false,
pt: false,
})
function IconFacebook({ fill = "#FFF", width = 24, height = 24 }) {
return (
<svg xmlns="http://www.w3.org/2000/svg" width={width} height={height}>
<path
fill={fill}
d="M22.675 0H1.325C.593 0 0 .593 0 1.325v21.351C0 23.407.593 24 1.325 24H12.82v-9.294H9.692v-3.622h3.128V8.413c0-3.1 1.893-4.788 4.659-4.788 1.325 0 2.463.099 2.795.143v3.24l-1.918.001c-1.504 0-1.795.715-1.795 1.763v2.313h3.587l-.467 3.622h-3.12V24h6.116c.73 0 1.323-.593 1.323-1.325V1.325C24 .593 23.407 0 22.675 0z"
/>
</svg>
)
}