Everyone knows that to create a new Promise, you need to define it this way:
new Promise((resolve, reject) => {
...
resolve(someValue)
})You pass a callback that defines the specific behavior of your promise.
| /* ------------------------------------------------------------------------- * | |
| Async / Await simple implementation with generators and promises | |
| * -------------------------------------------------------------------------- */ | |
| const wrap = generator => (...args) => { | |
| const iterator = generator(...args) | |
| const loop = ({ value: promise, done }) => { | |
| if (!done) { | |
| promise.then( | |
| x => loop(iterator.next(x)), |
| import { compose, add, map, range } from 'lodash/fp' | |
| import { Just, Nothing } from './Maybe' | |
| class Task { | |
| constructor(fork) { | |
| this.fork = fork | |
| } | |
| static of(x) { | |
| return Task.resolve(x) |
| import React, { Component } from 'react' | |
| import isEqual from 'lodash/fp/isEqual' | |
| // let you inject local state to you component | |
| const stateful = (mapPropsToInitialState = () => ({})) => Child => { | |
| return class StateFul extends Component { | |
| static displayName = `StateFul(${Child.displayName || Child.name})` |
| /* ----------------------------------------- * | |
| Sum | |
| * ----------------------------------------- */ | |
| const Sum = x => ({ | |
| x, | |
| concat: ({ x: y }) => Sum(x + y), | |
| inspect: () => `Sum(${x})`, | |
| }) |
| const Task = require('data.task') | |
| const Either = require('data.either') | |
| const { Left, Right } = Either | |
| const request = require('request') | |
| const { List } = require('immutable-ext') | |
| const { drop, prop, map } = require('lodash/fp') | |
| const effect = f => x => { | |
| f(x) | |
| return x |
| /* --------------------------------------------------------------- * | |
| Remove node modules recursively in a pure functionnal way | |
| * ---------------------------------------------------------------- */ | |
| // applying functionnal techniques from @drboolean in his egghead course | |
| // https://egghead.io/courses/professor-frisby-introduces-composable-functional-javascript | |
| const fs = require('fs.extra') | |
| const path = require('path') |
| /* ----------------------------------------- * | |
| Lazy List Implementation | |
| * ----------------------------------------- */ | |
| // Haskell-like infinite List, implemented with es6 generators | |
| // Lazyness lets you do crazy stuff like | |
| List.range(0, Infinity) | |
| .drop(1000) | |
| .map(n => -n) |
| // crack the ceasar code by minimazing the distance between english language letter | |
| // occurrence and all the letter occurrence of all the possible shifts of a message. | |
| const englishLetterOccurrence = [ 8.2, 1.5, 2.8, 4.3, 12.7, 2.2, 2.0, 6.1, 7.0, 0.2, 0.8, 4.0, 2.4, 6.7, 7.5, 1.9, 0.1, 6.0, 6.3, 9.1, 2.8, 1.0, 2.4, 0.2, 2.0, 0.1 ] | |
| const letters = 'abcdefghijklmnopqrstuvwxyz'.split('') | |
| const letterOccurrence = str => | |
| letters | |
| .map(c => (str.match(new RegExp(c, 'gi')) || []).length) |
| const State = state => new Proxy({ | |
| state, | |
| listeners: [], | |
| subscribe(listener) { | |
| this.listeners.push(listener) | |
| return () => this.listeners.filter(x => x !== listener) | |
| }, | |
| set(stateUpdates) { | |
| this.state = Object.assign({}, this.state, stateUpdates); | |
| this.listeners.forEach(f => { |