Skip to content

Instantly share code, notes, and snippets.

View VitorLuizC's full-sized avatar

Vitor L Cavalcanti VitorLuizC

View GitHub Profile
@VitorLuizC
VitorLuizC / README.md
Last active January 30, 2020 19:31
O que é um chain/then/fmap/flatMap?

O que é um chain/then/fmap/flatMap?

Promise.prototype.then and Array.prototype.flatMap são implementações aproximadas do chain/fmap, uma função característica dos Monads.

Resumindo o máximo possível, um chain é um map que faz flatten do retorno do callback.

O chain recebe uma função que modifica o valor contido num Monad e retorna um novo valor contido num Monad com a mesma assinatura.

interface Monad<T> {
@VitorLuizC
VitorLuizC / Animation.js
Created January 28, 2020 04:33
Closure Factories vs Classes in JavaScript
class Animation {
/**
* Creates an object to provide `start` and `stop` methods to animations.
* @param {function(number):void} animate
*/
constructor(animate) {
/**
* @type {null|number}
*/
this._id = null;
type Task<T> = () => Promise<T>;
const runInSequence = async <T>([task, ...tasks]: Task<T>[]): Promise<T[]> => {
if (!task)
return [];
return [await task(), ...(await runInSequence(tasks))];
};
export default runInSequence;
@VitorLuizC
VitorLuizC / index.d.ts
Last active January 19, 2020 16:10
`Object.keys` that allows generics and returns an array of its keys.
interface ObjectConstructor {
keys<T>(o: T): Extract<keyof T, string>[];
}
const createDownloadLink = (href: string, download: string) => {
const link = document.createElement('a');
link.href = href;
link.target = "_blank";
link.download = download;
link.style.display = "none";
return link;
};
import useStore from './useStore.js';
const useAuthentication = () => {
const { state, updateState } = useStore();
const setUser = (user) => updateState({ user });
const setToken = (token) => updateState({ token });
const logout = () => {
import { useEffect } from 'react';
/*
* Returns a new array without items whose property received is duplicated.
*
* @example
* removeDuplicatesByProp([
* {id: 1, name: 'apple'},
* {id: 2, name: 'apple'},
* {id: 3, name: 'orange'}
* ], 'name');
* //=> [{id: 1, name: 'apple'}, {id: 3, name: 'orange'}]
/**
* Símbolo que representa a pilha vazia.
*/
const EMPTY_STACK: unique symbol = Symbol('EMPTY_STACK');
type Stack<T> = Readonly<{
value: T;
next: Stack<T>;
}>;
@VitorLuizC
VitorLuizC / index.ts
Created November 21, 2019 14:23
Como documentar um objeto usando TSDoc?
// Como documentar o tipo de um objeto usando TSDoc?
/**
* Usuário do sistema.
*/
type User = {
/**
* Nome do usuário.
*/