Last active
May 12, 2018 15:55
-
-
Save gtkatakura/022498972985e90179e0a93bdfb6c9fd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const defaultMiddleware = next => (...args) => next(...args) | |
const compose = (() => { | |
const base = (f, g) => (...args) => f(g(...args)); | |
return (...funcs) => funcs.reduce(base) | |
})(); | |
const applyMiddlewares = (middlewares, func) => compose(...middlewares)(func); | |
const recursive = (func, middlewares = [defaultMiddleware]) => { | |
const wrapper = applyMiddlewares( | |
middlewares, | |
func((...args) => wrapper(...args)) | |
); | |
const withMiddleware = middleware => recursive(func, [...middlewares, middleware]); | |
return Object.assign(wrapper, { withMiddleware }); | |
}; | |
const sum = recursive(self => ([first = 0, ...rest]) => { | |
console.log(1); | |
if (rest.length === 0) return first; | |
return first + self(rest); | |
}); | |
const sumWithLogging = sum.withMiddleware(next => (...args) => { | |
console.log("before logging = ", args); | |
return next(...args); | |
}) | |
const memoize = func => { | |
const cache = {}; | |
return (...args) => { | |
const key = JSON.stringify(args); | |
console.log(key); | |
if (!cache.hasOwnProperty(key)) { | |
cache[key] = func(...args); | |
} | |
return cache[key]; | |
} | |
}; | |
const cacheMiddleware = next => memoize((...args) => next(...args)) | |
const sumWithLoggingAndCache = sumWithLogging.withMiddleware(cacheMiddleware) | |
const sumWithBeforeAndAfterLoggingsAndCache = sumWithLoggingAndCache.withMiddleware(next => (...args) => { | |
console.log("after logging = ", args); | |
return next(...args); | |
}) | |
//sumWithBeforeAndAfterLoggingsAndCache([1, 2, 3]) === sumWithBeforeAndAfterLoggingsAndCache([1, 2, 3]); | |
//sumWithLoggingAndCache([1]); | |
//sumWithLoggingAndCache([1, 2]); | |
sumWithBeforeAndAfterLoggingsAndCache([2,1]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Tentando transformar o `recursive` em uma função `recursive` | |
const defaultMiddleware = next => (...args) => next(...args) | |
const compose = (() => { | |
const base = (f, g) => (...args) => f(g(...args)); | |
return (...funcs) => funcs.reduce(base) | |
})(); | |
const applyMiddlewares = (middlewares, func) => compose(...middlewares)(func); | |
const recursive = (() => { | |
const base = (func, middlewares = [defaultMiddleware]) => { | |
const wrapper = applyMiddlewares( | |
middlewares, | |
func((...args) => wrapper(...args)) | |
); | |
return wrapper; | |
} | |
const recursive = base(self => (func, middlewares = [defaultMiddleware]) => { | |
const wrapper = base(func, middlewares); | |
const withMiddleware = middleware => self(func, [...middlewares, middleware]); | |
return Object.assign(wrapper, { withMiddleware }); | |
}); | |
return recursive(self => (...args) => recursive(...args)) | |
})(); | |
const cacheMiddleware = next => memoize((...args) => next(...args)) | |
const recursiveWithCacheMiddleware = recursive.withMiddleware(next => (...args) => { | |
const wrapper = next(...args); | |
const withCache = () => wrapper.withMiddleware(cacheMiddleware); | |
return Object.assign(wrapper, { withCache }); | |
}); | |
const sum = recursiveWithCacheMiddleware(self => ([first = 0, ...rest]) => { | |
console.log(1); | |
if (rest.length === 0) return first; | |
return first + self(rest); | |
}); | |
const sumWithLogging = sum.withMiddleware(next => (...args) => { | |
console.log("before logging = ", args); | |
return next(...args); | |
}) | |
const memoize = func => { | |
const cache = {}; | |
return (...args) => { | |
const key = JSON.stringify(args); | |
console.log(key); | |
if (!cache.hasOwnProperty(key)) { | |
cache[key] = func(...args); | |
} | |
return cache[key]; | |
} | |
}; | |
const sumWithLoggingAndCache = sumWithLogging.withMiddleware(cacheMiddleware) | |
const sumWithBeforeAndAfterLoggingsAndCache = sumWithLoggingAndCache.withMiddleware(next => (...args) => { | |
console.log("after logging = ", args); | |
return next(...args); | |
}) | |
//sumWithBeforeAndAfterLoggingsAndCache([1, 2, 3]) === sumWithBeforeAndAfterLoggingsAndCache([1, 2, 3]); | |
//sumWithLoggingAndCache([1]); | |
//sumWithLoggingAndCache([1, 2]); | |
sumWithBeforeAndAfterLoggingsAndCache([2,1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment