Skip to content

Instantly share code, notes, and snippets.

View nodew's full-sized avatar
:octocat:
Focusing

Qiao Wang nodew

:octocat:
Focusing
View GitHub Profile
@nodew
nodew / observable.ts
Created November 28, 2018 06:15
observable pattern
function noop() { /* nothing */}
interface Unsubscribable {
unsubscribe(): void;
}
interface Subscription extends Unsubscribable {
unsubscribe(): void
}
@nodew
nodew / monad.ts
Last active October 22, 2023 10:35
monad with typescript
interface Functor<A> {
fmap<B>(fn: (a: A) => B): Functor<B>
}
abstract class Applicative<A> implements Functor<A> {
static of: <A>(a: A) => Applicative<A>;
abstract fmap<B>(fn: (a: A) => B): Applicative<B>;
abstract ap<B, C>(this: Applicative<(b: B) => C>, ma: Applicative<B>): Applicative<C>;
lift<B, C>(fn: (a: A, b: B) => C) : (mb: Applicative<B>) => Applicative<C> {
@nodew
nodew / lazy-list-with-generators.js
Last active August 7, 2018 09:16
lazy list implementation with generators
function wrap(iterator) {
const sequence = [];
const next = (i) => {
if (sequence.length > i) {
return {
value: sequence[i],
done: false
}
}
@nodew
nodew / monoid-monad.hs
Last active June 2, 2018 02:18
a monad is just a monoid in the category of endofunctors
{-# LANGUAGE RankNTypes #-}
module Main where
import Prelude hiding (Monad(..), Monoid(..))
-- class Monoid m where
-- mempty :: m
-- mappend :: m -> m -> m
-- mconcat :: [m] -> m
-- mconcat = foldr mappend mempty
@nodew
nodew / freeMonad.hs
Last active June 1, 2018 06:48
a minimal free monad implementation
{-# LANGUAGE RankNTypes, TypeOperators #-}
module Main where
import Prelude as P
data Free f a
= Pure a
| Free (f (Free f a))
instance Functor f => Functor (Free f) where
fmap f (Pure a) = Pure $ f a
@nodew
nodew / promise-then-catch.js
Created August 22, 2017 13:33
promise.catch is sugar of `then` to handle error
new Promise((resolve, reject) => {
reject(false);
}).then(() => {}, (error) => {
console.log('error handle')
}).catch(error => {
console.log('catch')
})
// => error handle
@nodew
nodew / yield.test.js
Created August 22, 2017 13:24
yield test
const slice = Array.prototype.slice;
const isGenerator = (obj) => {
return 'function' == typeof obj.next && 'function' == typeof obj.throw;
}
function isPromise(obj = {}) {
return 'function' == typeof obj.then;
}
@nodew
nodew / collection_of_scripts.sh
Created August 16, 2017 03:53
useful sh scripts collection
#!/bin/bash
const Y = function(F) {
return (function (h) {
return h(h)
})(function(h) {
return F(function() {
return h(h).apply(this, arguments)
})
})
}
#!/bin/bash
files=`find ./src -type f -regex "./.*.[^coffee]"`
for file in $files
do
path=`dirname $file`
newpath=`echo ${path//src/build}`
filename=`basename $file`
if [ ! -d $newpath ]; then