Skip to content

Instantly share code, notes, and snippets.

View dewey92's full-sized avatar
🌴

Jihad D. Waspada dewey92

🌴
View GitHub Profile
@dewey92
dewey92 / Main.purs
Last active January 2, 2021 22:55
FsService - purs
module Main where
import Prelude
import Effect (Effect)
import Effect.Class (liftEffect)
import Effect.Aff (Aff, launchAff_)
import Node.FS.Aff as FS
import Node.Buffer as Buffer
import Node.Encoding (Encoding(..))
// brand.ts
// use symbol, but don't export it!
declare const _brand: unique symbol;
export type Brand<Name, Type> = Type & { [_brand]?: Name }
// degree-module.ts
export type Celcius = Brand<'Celcius', number>;
export type Fahrenheit = Brand<'Fahrenheit', number>;
@dewey92
dewey92 / counter-2.jsx
Last active May 17, 2019 18:59
Counter Effect Cleaner
// Counter.jsx
import * as React from 'react'
import { useCounterReducer } from './useCounterReducer'
const Counter = () => {
const [[counter, effect], actions] = useCounterReducer();
// Hooks ini hanya berfokus pada "side-effect" dari reducer di atas
React.useEffect(() => {
if (effect === 'INVALID_STATE') {
@dewey92
dewey92 / useCounterReducer-2.js
Last active May 17, 2019 18:57
Counter Reducer Solution
// useCounterReducer.js
import * as React from 'react';
// Array destructuring disini sangat membantu code readability
const counterReducer = ([state, effect], action) => {
if (action.type === 'INCREMENT') {
return [state + 1];
}
if (action.type === 'DECREMENT') {
@dewey92
dewey92 / hack-counter-1.jsx
Last active May 17, 2019 17:57
Hack Counter
// ...
const Counter = () => {
// ...
const decr = () => {
if (state === 0) {
alert('Ya akhi, nilai counter ndak boleh di bawah 0 😚');
} else {
dispatch('DECREMENT');
@dewey92
dewey92 / counter-1.jsx
Last active May 17, 2019 17:12
Counter effects
// Counter.jsx
import * as React from 'react'
import { useCounterReducer } from './useCounterReducer'
const Counter = () => {
const [counter, dispatch] = useCounterReducer();
React.useEffect(() => {
if (counter < 0) {
alert('Ya akhi, nilai counter ndak boleh di bawah 0 😚');
@dewey92
dewey92 / useCounterReducer.js
Created May 17, 2019 16:59
Counter Reducer
// useCounterReducer.js
import * as React from 'react';
const counterReducer = (state, action) => {
if (action.type === 'INCREMENT') {
return state + 1;
}
if (action.type === 'DECREMENT') {
return state - 1;
@dewey92
dewey92 / hkt-concept.ts
Last active May 8, 2019 21:16
HKT concept ts
const map = <F, A, B>(fn: (val: A) => B, thing: F<A>): F<B> = /* IMPL */;
@dewey92
dewey92 / generic-tree.ts
Last active May 8, 2019 06:38
Generic Tree
type Tree<A> = Leaf<A> | Branch<A>
type Leaf<A> = {
kind: 'leaf';
value: A;
}
type Branch<A> = {
kind: 'branch';
left: Tree<A>;
right: Tree<A>;
}
interface Array<T> {
map: ...
filter: ...
reduce: ...
length: number;
...
}
const names: Array<string> = ['jihad', 'waspada']
const odds: Array<number> = [1, 3, 5, 7]
// etc