Skip to content

Instantly share code, notes, and snippets.

View dewey92's full-sized avatar
🌴

Jihad D. Waspada dewey92

🌴
View GitHub Profile
@dewey92
dewey92 / partialApp.hs
Last active February 28, 2019 00:32
Partial App - Haskell
fillNames :: String -> String -> String -> String
fillNames firstN middleN lastN = firstN ++ middleN ++ lastN
a :: String -> String -> String
a = fillNames "Jihad"
b :: String
b = a " Dzikri" " Waspada"
b == "Jihad Dzikri Waspada" -- True
@dewey92
dewey92 / functor-naive.ts
Last active November 27, 2018 10:35
Functor in a very naive way
type Tree<A> = Leaf<A> | Branch<A>;
type Leaf<A> = { _f: 'tree', kind: 'leaf', value: A };
type Branch<A> = { _f: 'tree', kind: 'branch', left: Tree<A>, right: Tree<A> };
function mapTree<A, B>(map: (val: A) => B, tree: Tree<A>): Tree<B> {
switch (tree.kind) {
case 'leaf':
return leaf(map(tree.value));
case 'branch':
return branch(
@dewey92
dewey92 / ts-doc.ts
Last active October 19, 2018 23:59
TS doc
export interface TimeSlot {
deliveryId: number;
from: string;
to: string;
}
export interface DeliveryTimeResponse {
date: string;
slots: {
DayTime: TimeSlot;
@dewey92
dewey92 / ts-gen-enum.ts
Created October 9, 2018 22:38
TS Generics dan enum
/* Enum */
enum Status {
Initial,
Fetching,
Done,
Error
}
interface ProductStore {
products: ProductModel[];
@dewey92
dewey92 / ts-int-uni-type.ts
Last active October 9, 2018 22:23
Intersection & Union Type
/* Intersection type */
interface Goku {
withKamehameha: boolean;
}
interface Bejita {
sayKakarot: boolean;
}
type Gogeta = Goku & Bejita;
const gogeta: Gogeta = {
@dewey92
dewey92 / ts-arr-lookup.ts
Last active October 19, 2018 21:03
TS Array Lookup
const getFinalPrice = (product: ProductModel) =>
product.price + product.tax;
const getProductById = (products: ProductModel[], id: number) =>
products.find(product => product.id === id);
const product = getProductById(productList, 1);
const finalPrice = getFinalPrice(product); // <- ERROR
// Argument of type 'ProductModel | undefined' is not assignable to parameter of type 'ProductModel'.
// Type 'undefined' is not assignable to type 'ProductModel'.
@dewey92
dewey92 / ts-arr-find.ts
Last active October 19, 2018 21:00
Array find
interface ProductModel {
id: number;
name: string;
price: number;
tax: number
}
const productList: ProductModel[] = [
{ id: 100, name: 'deluxe', price: 28, tax: 2.5 },
{ id: 101, name: 'giftcard', price: 30, tax: 3 },
@dewey92
dewey92 / ts-interface.ts
Last active October 9, 2018 20:45
TS interface basic
interface ProductModel {
id: number;
name: string;
price: number;
tax: number
}
const getFinalPrice = (product: ProductModel) =>
product.price + product.tax;
@dewey92
dewey92 / newtype.ts
Last active August 9, 2019 08:23
New Type
declare const _phantom: unique symbol;
export type NewType<Name, Type> = Type & { [_phantom]?: Name };
export type Celcius = NewType<'Celcius', number>;
export type Fahrenheit = NewType<'Fahrenheit', number>;
// ...etc
@dewey92
dewey92 / brand-impr.ts
Last active October 9, 2018 13:01
Branding improvement
type Brand<Name, Type> = Type & { _brand?: Name } // optional
type Celcius = Brand<'Celcius', number>;
type Fahrenheit = Brand<'Fahrenheit', number>;
const fiveC: Celcius = 5; // typechecked :)