Skip to content

Instantly share code, notes, and snippets.

View gvergnaud's full-sized avatar
🚀

Gabriel Vergnaud gvergnaud

🚀
View GitHub Profile
/* ------------------------------------------------------------------------- *
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)),
@gvergnaud
gvergnaud / Promises-under-the-hood.md
Last active December 24, 2023 18:35
Promises, under the hood.

Promises, under the hood

You all know that to create a new Promise you need to define it this way:

  new Promise((resolve, reject) => {
    ...
    resolve(someValue)
  })

You are passing a callback that defines the specific behavior of your promise.

@gvergnaud
gvergnaud / Task.js
Last active June 27, 2021 19:34
Task.js — Full Implementation (SemiGroup, Monoid, Functor, Monad, Applicative)
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')
@gvergnaud
gvergnaud / lazy-list.js
Last active July 31, 2023 23:57
Lazy List, implemented with es6 generators
/* ----------------------------------------- *
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 => {