Skip to content

Instantly share code, notes, and snippets.

View Lucifier129's full-sized avatar
🎯
Focusing

工业聚 Lucifier129

🎯
Focusing
View GitHub Profile
import React from 'react'
import ReactDOM from 'react-dom'
import { reactive } from 'rxjs-react'
import { from, merge } from 'rxjs'
import { map, delay, startWith } from 'rxjs/operators'
@reactive
class App extends React.Component {
data$ = from(fetch(this.props.url).then(res => res.json())).pipe(delay(1000))
render() {
import React from 'react'
import ReactDOM from 'react-dom'
import { reactive } from 'rxjs-react'
import { from, merge, of } from 'rxjs'
import { map } from 'rxjs/operators'
const ComponentA = () => <div>component A</div>
const Loading = () => <div>loading...</div>
const fakeImportComponentA = () =>
new Promise(resovle => {
@Lucifier129
Lucifier129 / recursive.js
Last active June 25, 2018 08:09
Tail call optimization by algebraic effects
const recursive = f => {
let isCalling = false
let proxy = (...args) => {
if (isCalling) throw args
while (true) {
try {
isCalling = true
return f(...args)
} catch(params) {
if (params instanceof Error) throw params
@Lucifier129
Lucifier129 / algebraic-effects.js
Created June 28, 2018 14:33
algebraic effects and handlers for promise and observable
import { interval, isObservable } from 'rxjs'
import { tap } from 'rxjs/operators'
const type = {
isThenable: obj => !!(obj && typeof obj.then === 'function'),
isObservable: obj => !!(obj && isObservable(obj)),
isError: obj => obj instanceof Error,
isObject: obj => obj != null && typeof obj === 'object'
}
var noop = () => {}
var compose = (f1, f2) => (...args) => f1(f2(...args))
var range = (start, end) => {
let n = start
let f = (next, complete) => {
if (n <= end) {
next(n++)
f(next, complete)
} else {
complete()
@Lucifier129
Lucifier129 / parser-combintor.js
Created August 23, 2018 08:02
a simple parser combinator impletement by javascript
// parser combintor
const join = separator => value => value.join(separator)
const predicate = f => input => {
if (input && f(input[0])) {
return {
value: input[0],
remain: input.slice(1)
}
}
@Lucifier129
Lucifier129 / stream-combinator.js
Created August 23, 2018 08:28
a simple stream combinator implement by javascript
const interval = period => sink => {
let timer
let i = 0
let next = () => sink.next(i++)
let start = () => {
timer = setInterval(next, period)
}
let finish = () => {
clearInterval(timer)
sink.finish()
@Lucifier129
Lucifier129 / naive-json-parser.js
Last active December 23, 2020 23:15
a naive json parser implemented by parser combinator
// parser combinator
const fail = () => []
const failed = list => list.length === 0
const of = value => input => [value, input]
const bind = (parser, f) => input => {
let result = parser(input)
@Lucifier129
Lucifier129 / monadic-functional-observable.js
Created March 30, 2019 13:16
monadic functional observable supports pullable and pushable
const identity = x => x
const noop = () => {}
const pipe = (...args) => args.reduce((a, f) => f(a))
const create = producer => sink => {
let isFinish = false
let wrap = f => a => {
if (isFinish) return
if (f) f(a)
@Lucifier129
Lucifier129 / monadic-async-parser-combinator.js
Last active April 16, 2019 05:22
monadic parser combinator supports async and streaming input
// parser combinator
const pipe = (...args) => args.reduce((a, f) => f(a))
const Deferred = () => {
let resolve, reject
let promise = new Promise((a, b) => {
resolve = a
reject = b
})