Skip to content

Instantly share code, notes, and snippets.

@humanhighway
humanhighway / applicative-functor-example.js
Last active May 1, 2021 14:13
applicative functor example
// 將 Box 參數改為 funcion
const Box = (f) => ({
map: (g) => Box((x) => g(f(x))),
fold: (x) => f(x),
// applicative
ap: (other) => Box((x) => other.map(f(x)).fold())
});
Box.of = (value) => Box(() => value);
const f1 = Box.of(10);
@humanhighway
humanhighway / natural-transformation-example.js
Last active May 1, 2021 10:28
natural transformation example
import { Task, Either } from "./types";
const { Left, Right } = Either;
const fetchDB = (index) =>
Task((rej, res) => {
res(index < 3 ? Left("not found") : Right("get data succeed"));
});
// natural transformation: either to task
const eitherToTask = (e) => e.fold(Task.rejected, Task.of);
@humanhighway
humanhighway / machine.js
Last active April 15, 2021 07:08
Generated by XState Viz: https://xstate.js.org/viz
// import { Machine } from 'xstate';
const states = {
UNINITIALIZED: 'UNINITIALIZED',
IDLE: 'IDLE',
// PHONE
PHONE_UNINITIALIZED: 'PHONE_UNINITIALIZED',
PHONE_IDLE: 'PHONE_IDLE',
PHONE_SET_FETCHING: 'PHONE_SET_FETCHING',
PHONE_SET_SUCCEED: 'PHONE_SET_SUCCEED',
// you can copy type.js from https://github.com/FrontendMasters/hardcore-functional-js-v2/blob/master/types.js
import { Task } from "./types";
// warp fetch
const fetchRepo = (url) =>
Task((rej, res) => {
fetch(url)
.then((res) => res.json())
.then((content) => res(content))
.catch((e) => rej(e));
const Box = (x) => ({
map: f => Box(f(x)), // always return Box type.
fold: (f) => f(x), // unwarp value
})
const result= Box(10)
.map((x) => x * 2)
.map((x) => x + 10)
.fold(x => x);
// Either Monad
const Left = x => ({
map: f => Left(x),
fold: (f, g) => f(x)
});
const Right = x => ({
map: f => Right(f(x)),
fold: (f, g) => g(x)
});
@humanhighway
humanhighway / WASMSocket.js
Last active September 10, 2020 12:02
socket wasm worker
import Emitter from 'eventemitter3';
import worker from './socket.worker';
let socket;
export function getSocket() {
if (!socket) socket = new WasmSocket();
return socket;
}
export const eventTypes = {
@humanhighway
humanhighway / socket.worker.js
Last active September 10, 2020 12:14
webworker wasm
// 原本綁定在 window object 的 wasm(rust) -> js function 參考, 因為放到 worker 改至 worker 執行環境的 self reference.
// 將監聽函數轉發至 worker post message
self.onSocketOpen = message => self.postMessage({ type: 'OPEN', message });
self.onSocketMessage = message => self.postMessage({ type: 'MESSAGE', message });
let wasm;
console.log('worker execute');
import('./../../wasm/pkg/wasm').then(w => wasm = w);
export const send = async message => {
@humanhighway
humanhighway / worker.config.js
Created September 10, 2020 11:30
workerize-loader
{
test: /\.worker.js$/,
exclude: [/node_modules|vendor/],
use: [{
loader: 'workerize-loader',
}],
},
@humanhighway
humanhighway / nodejs-ase256-cbc-encrypet-decrtpt.js
Last active September 9, 2020 07:09
nodejs-ase256-cbc-encrypet-decrtpt.rs
import crypto from 'crypto';
const key = Buffer.from('01234567012345670123456701234567', 'utf-8');
const iv = Buffer.from('0123456701234567', 'utf-8');
export const encrypt = message => {
const cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
const encrypted = cipher.update(message, 'utf8', 'base64');
return encrypted + cipher.final('base64');
};