Functor F...
Let
-
Object map
$F : Obj_{\Bbb{C}}\to Obj_{\Bbb{D}}$ such that$$A \to F_{Obj}(A)$$ -
Morphism map
$F_{Hom}:Hom_{\Bbb C}\to Hom_{\Bbb C}$ such that
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) |
// 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> { |
Functor F...
Let
Object map
Morphism map
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 |
// 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; |
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(); |
// 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> | |
) | |
} |