Skip to content

Instantly share code, notes, and snippets.

View fResult's full-sized avatar

Sila Setthakan-anan fResult

View GitHub Profile
@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>,
@fResult
fResult / True definition of Higher-Order Function from the Math book.md
Last active January 14, 2024 16:19
True definition of Higher-Order Function from Mathematics for Working Programmers class, by lect. Dave Rawitat Pulam

Higher-Order Functions 😡😡😡

See something from an actual Maths book.

Definition

A function is called a higher-order function if its arguments and values are allowed to be functions.
This is an important property that most good programming languages possess.
The composition and tupling operations are examples of functions that take other functions as arguments and return functions as results.

@fResult
fResult / zip.ts
Last active March 26, 2023 08:51
function zip(xs, ys) {
if (xs.length < ys.length) {
return xs.map((x, idx) => [x, ys[idx]])
} else {
return ys.map((y, idx) => [xs[idx], y])
}
}
// FP Style
@fResult
fResult / Day 05 - EP4 (Continue) Functors and natural transformations.md
Last active March 26, 2023 08:33
Day 05 - EP4 (Continue) Functors and natural transformations.md

Functors and natural transformations (Continue)

แบบฝึกหัดจากรอบที่แล้ว

แบบฝึกหัดใหม่

เราจะมารู้เรื่อง Category $\Bbb{Rel}$ กัน แต่ก่อนจะเข้าใจเรื่อง Relation ได้ ต้องเท้าความเรื่อง Category of Set ก่อน กำหนดให้มี Category $\Bbb{Set}$ ที่มี Object เป็น $Set$(s) ต่างๆ มี Morphism เป็น function(s) มี Identity Morphism เป็น $1_x=x$ (หรือ $f\ x=x$)

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>
)
}
@fResult
fResult / typescriptreact.json
Last active February 28, 2023 08:01
[TSX Snippet] Create Typescript Arrow Function Component for `.tsx` file
{
"Create TypeScript React Arrow Functional Component and for `.tsx` file": {
"prefix": "tsrafce",
"body": [
"import React, { FC } from 'react'",
"",
"type ${1:${TM_FILENAME_BASE}}Props = {",
" className?: string",
" ${2:prop1}: ${3:type1}",
"}",