Skip to content

Instantly share code, notes, and snippets.

function isPromise(thing) {
return (
typeof thing === "object" && thing !== null
&& thing.then instanceof Function
&& thing.catch instanceof Function
);
}
function whileGenerates(gen, prevGenResult) {
@mgtitimoli
mgtitimoli / solution.js
Last active November 5, 2021 21:29
Find the substring triples count that contain the same amount of "a"
const getAllPartSets = s => {
const partSets = [];
// pivot in 0
for (let i = 2; i < s.length; i++) {
partSets.push([
s[0],
s.slice(1, i),
s.slice(i)
]);
const time24TupleToMinutes = ([hours, minutes]) => hours * 60 + minutes;
const timeStrToTuple = timeStr => timeStr.split(":").map(Number);
const time12ToTime24Tuple = time12 => {
const [hours, minutes] = timeStrToTuple(time12.slice(0, -2));
const amPm = time12.slice(-2);
return [
@mgtitimoli
mgtitimoli / Test.tsx
Created January 21, 2021 02:48
StateAccessor (getter, setter) Hook => useStateAccessor
import React from "react";
import type {ReactStateAccessor} from "./useStateAccessor";
type State = {
count: number;
};
type StateAccessor = ReactStateAccessor<State>
@mgtitimoli
mgtitimoli / useNonCachedQuery.ts
Created December 22, 2020 02:14
useNonCachedQuery
import {useEffect} from "react";
import {useQuery, useQueryClient} from "react-query";
import type {
QueryKey,
QueryFunction,
UseQueryOptions,
UseQueryResult
} from "react-query";
@mgtitimoli
mgtitimoli / httpStatusCode.ts
Created July 4, 2020 23:54
TypeScript HTTP Status Codes
/**
* Hypertext Transfer Protocol (HTTP) response status codes.
* @see {@link https://en.wikipedia.org/wiki/List_of_HTTP_status_codes}
*/
const httpStatusCodes = {
/**
* The server has received the request headers and the client should proceed to send the request body
* (in the case of a request for which a body needs to be sent; for example, a POST request).
* Sending a large request body to a server after a request has been rejected for inappropriate headers would be inefficient.
* To have a server check the request's headers, a client must send Expect: 100-continue as a header in its initial request
@mgtitimoli
mgtitimoli / README.md
Last active May 31, 2020 03:40
Find the minimum number of flips to get an alternating array of binary numbers

Exercise

Given an array of binary numbers, find the minimum number of flips (inversions) required to get an alternating version of it.

Solution

Given that we are dealing with binary numbers, there are 2 possible alternating versions of it, the one that starts with 0, and the one that starts with 1. This means that the input array should be compared with these 2 possibilities, get the number of required flips on each of them, and finally the result will be the minimum of these 2 counts.

@mgtitimoli
mgtitimoli / sortCsvFile.ts
Created April 23, 2020 15:27
Sort in Node.JS using OS commands
import * as childProcess from 'child_process';
import { promisify } from 'util';
type OptionalParams = {
fieldSeparator?: string;
hasHeader?: boolean;
};
type MandatoryParams = {
byColumn: number;
@mgtitimoli
mgtitimoli / git-diff-with-filter.sh
Created March 21, 2020 00:36
git diff with filter
git diff --name-only | grep -v $EXCLUDE_PATTERN | xargs git diff $OTHER_OPTIONS --
// @flow
import React, {createContext, useContext, useState} from "react";
import type {Context, Node, StatelessFunctionalComponent} from "react";
import useEffectOnUpdate from "./useEffectOnUpdate";
type UseStateStateUpdater<TState> = (state: TState) => TState;