Skip to content

Instantly share code, notes, and snippets.

View brycetshaw's full-sized avatar

Bryce Shaw brycetshaw

View GitHub Profile
@brycetshaw
brycetshaw / memoize.ts
Created October 16, 2021 22:38
A memoize function written in typescript.
function memoize<
Response,
ProvidedFunctionType extends (...args: any[]) => Response
>(
computeExpensiveValue: ProvidedFunctionType,
keyFunction: (...args: any[]) => string,
cachingFunction?: () => Map<string, Response>
): ProvidedFunctionType {
// if no caching object is provided, instanciate a Map instead.
const hashMap = cachingFunction ? cachingFunction() : new Map();
@brycetshaw
brycetshaw / superArray.ts
Last active September 10, 2021 16:13
extending the array class
export class SuperArray<Data> extends Array {
static from<Data>(input: Data[]): SuperArray<Data> {
return new SuperArray(input);
}
constructor(input: any[]) {
super(input.length);
input.forEach((element, index) => {
this[index] = element;
})
@brycetshaw
brycetshaw / fizzbuzz.js
Last active December 27, 2021 07:09
Solves the classic FizzBuzz problem in an aggressively functional programming style.
function arrayOfNumbers(n) {
return Array.from(Array(n).keys())
.map((x) => ++x)
}
function fizzBuzz(n) {
const mutators = [
{key: 3, val:"Fizz"},
{key:5, val: "Buzz"}
@brycetshaw
brycetshaw / AnagramFinder.java
Last active August 16, 2020 17:09
Parses a list of words, sorts lexicographically, and groups words that are anagrams. Today I learned how to use the streams API and the word "lexicographic".
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class AnagramFinder {
const queryString = (year, goals, page) => {
const rootUrl = 'https://jsonmock.hackerrank.com';
const queryString = `/api/football_matches?year=${year}&team1goals=${goals}&team2goals=${goals}&page=${page}`;
return rootUrl + queryString;
}
@brycetshaw
brycetshaw / reduce-reduced-requests.js
Last active August 7, 2020 20:12
maps integer array to promise array and resolves it to a single result
async function getNumDraws(year) {
const arrayOfScores = Array.from(Array(11).keys());
return Promise.all(arrayOfScores.map(item => getTiesCountByNumberOfGoals(year, item)))
.then(drawsCountForEachScore => {
return drawsCountForEachScore.reduce((x, y) => x + y, 0);
})
}
async function getTiesCountByNumberOfGoals(year, goals) {
const [gamesOnFirstPage, numberOfPages] = await getNumOfGamesFromPage(year, goals, 0);
const pageArray = Array.from(Array(numberOfPages).keys()).filter((e) => e !== 0);
return Promise.all(
[
gamesOnFirstPage,
...pageArray.map(page => getNumOfGamesFromPage(year, goals, page))
])
@brycetshaw
brycetshaw / make-request.js
Last active August 7, 2020 20:09
Makes a network request!
const fetch = require('node-fetch');
async function getNumOfGamesFromPage(year, goals, page) {
const response = await fetch(queryString(year, goals, page))
.catch(err => console.log("get error " + err.message));
const json = await response.json();
if (page === 0) {
return [json.data.length, json.total_pages];
@brycetshaw
brycetshaw / concurrent-http-requests-full.js
Last active November 21, 2021 17:12
A script to make concurrent network requests!
const fetch = require('node-fetch');
const queryString = (year, goals, page) => {
const rootUrl = 'https://jsonmock.hackerrank.com';
const queryString = `/api/football_matches?year=${year}&team1goals=${goals}&team2goals=${goals}&page=${page}`;
return rootUrl + queryString;
}
async function getNumOfGamesFromPage(year, goals, page) {
const response = await fetch(queryString(year, goals, page))