Skip to content

Instantly share code, notes, and snippets.

View suddenlyGiovanni's full-sized avatar
🐑
yak shaving

Giovanni Ravalico suddenlyGiovanni

🐑
yak shaving
View GitHub Profile

Keybase proof

I hereby claim:

To claim this, I am signing this object:

@suddenlyGiovanni
suddenlyGiovanni / use-async.tsx
Last active February 10, 2021 20:58
useAsync
import * as React from 'react'
enum EnumStatuses {
IDLE = 'idle',
PENDING = 'pending',
RESOLVED = 'resolved',
REJECTED = 'rejected',
}
type Statuses = 'idle' | 'pending' | 'resolved' | 'rejected'
@suddenlyGiovanni
suddenlyGiovanni / klarna.ts
Created October 1, 2020 17:23
oriented graph traversal
/* eslint-disable max-lines-per-function */
/* eslint-disable max-statements */
type Routes = [string, string][]
type FindRoutes = (routes: Routes) => string
class Stack<T> {
items: T[]
constructor() {
this.items = []
/**
* merges two list together into a new list maintaining the correct type
* @param {[...A]} a first list of any
* @param {[...B]} b second list of any
* @returns {[...A, ...B]}
*/
export function concat<A extends unknown[], B extends unknown[]>(
a: [...A],
b: [...B]
/**
* pipe :: ((a -> b), (b -> c), ..., (y -> z)) -> a -> z
*/
export const pipe = (...fns) => (...args) =>
fns.reduce((res, fn) => [fn.call(null, ...res)], args)[0]
/**
* @param {string} message
* @returns {<T>(x:T)=> T}
*/
@suddenlyGiovanni
suddenlyGiovanni / compose.js
Last active May 23, 2020 15:48
JavaScript's compose function capable of handling any number of function compositions
/**
* compose :: ((y -> z), ..., (b -> c), (a -> b)) -> a -> z
*/
export const compose = (...fns) => (...args) =>
fns.reduceRight((res, fn) => [fn.call(null, ...res)], args)[0]
/**
* @param {string} message
* @returns {<T>(x:T)=> T}
*/
@suddenlyGiovanni
suddenlyGiovanni / fixed_arity_curry.ts
Last active May 26, 2020 19:44
TypeScript Curry for functions of fixed arity
type AssertEqual<T, Expected> = T extends Expected ? (Expected extends T ? true : never) : never
type UnaryFn<A, B> = (a: A) => B
type BinaryFn<A, B, C> = (a: A, b: B) => C
type TernaryFn<A, B, C, D> = (a: A, b: B, c: C) => D
type QuaternaryFn<A, B, C, D, E> = (a: A, b: B, c: C, d: D) => E
type QuinaryFn<A, B, C, D, E, F> = (a: A, b: B, c: C, d: D, e: E) => F
type Curry2 = <A, B, C>(f: BinaryFn<A, B, C>) => Curried2<A, B, C>
type Curried2<A, B, C> = (a: A) => (b: B) => C
// let's pretend that Array.prototype.filter does not exist
describe('filter', () => {
const array = [0, 1, undefined, 2, null, 3, 'four', '']
describe('with baked in filtering behavior', () => {
function filter<T>(array: T[]): T[] {
const arr = []
for (let index = 0; index < array.length; index++) {
const element = array[index]
if (element === null || element === undefined) {
@suddenlyGiovanni
suddenlyGiovanni / README-Template.md
Created May 21, 2019 11:01 — forked from PurpleBooth/README-Template.md
A template to make good README.md

Project Title

One Paragraph of project description goes here

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.

Prerequisites

@suddenlyGiovanni
suddenlyGiovanni / clean_code.md
Created May 17, 2019 13:16 — forked from wojteklu/clean_code.md
Summary of 'Clean code' by Robert C. Martin

Code is clean if it can be understood easily – by everyone on the team. Clean code can be read and enhanced by a developer other than its original author. With understandability comes readability, changeability, extensibility and maintainability.


General rules

  1. Follow standard conventions.
  2. Keep it simple stupid. Simpler is always better. Reduce complexity as much as possible.
  3. Boy scout rule. Leave the campground cleaner than you found it.
  4. Always find root cause. Always look for the root cause of a problem.

Design rules