Skip to content

Instantly share code, notes, and snippets.

View mattdiamond's full-sized avatar

Matt Diamond mattdiamond

View GitHub Profile
@mattdiamond
mattdiamond / app.ts
Last active February 7, 2022 18:34
Error handling kata
import { traverse as _traverse } from 'fp-ts/Array';
import { Task, chainIOK } from 'fp-ts/Task';
import {
TaskEither,
fromOption,
fromPredicate,
ApplicativePar,
right,
left,
chain,
function * primes (count = Infinity) {
yield 2;
search:
for (let number = 3, found = 1; found < count; number += 2) {
const root = Math.sqrt(number);
for (let divisor = 3; divisor <= root; divisor += 2) {
if (number % divisor === 0) {
continue search;
}
}
function primeFactors (n) {
if (n <= 1) return [];
if (n % 2 === 0) {
return [2, ...primeFactors(n / 2)];
}
const root = Math.sqrt(n);
let divisor = 3;
while (n % divisor) {
divisor += 2;
@mattdiamond
mattdiamond / prototypes.js
Last active December 7, 2018 22:53
Fun with Javascript Prototypes
Function.prototype.if = function (conditional) {
return input => conditional(input) ? this(input) : input;
}
let double = x => x * 2;
let positive = x => x > 0;
[2, -4, 6, -8].map(double.if(positive));
// [4, -4, 12, -8]
Function.prototype.patch = function (func) {
function convertToBase(number, base){
let value = number,
remainder,
result = '';
while (value !== 0) {
remainder = value % base;
value = ~~(value / base);
if (remainder < 0){
value += 1;
@mattdiamond
mattdiamond / gist:0d3a8ea2430bc6cfa6237a2030bb0414
Last active December 4, 2017 18:13
Flexible curry function

Based on version in Underscore-Contrib, but without enforcing unary.

var curry = (function(){
	function collectArgs(func, context, argCount, args, newArgs){
		args = args.concat(Array.from(newArgs));
		if (args.length >= argCount){
			return func.apply(context || this, args);
		}
 return function(){
@mattdiamond
mattdiamond / format-heroku-logs
Last active May 29, 2016 19:50
a short nodejs script that will convert dates
#!/usr/bin/env node
String.prototype.mapLines = function(func){
return this.split("\n").map(func).join("\n");
}
function convertDate(line){
if (!line) return line;
var split = line.split(' '),
@mattdiamond
mattdiamond / format-heroku-logs
Created May 29, 2016 19:49
a short nodejs script that will convert dates
#!/usr/bin/env node
String.prototype.mapLines = function(func){
return this.split("\n").map(func).join("\n");
}
function convertDate(line){
if (!line) return line;
var split = line.split(' ');
var date = new Date(split[0]);
@mattdiamond
mattdiamond / index.html
Created May 15, 2014 02:28
Curlicue Fractal
<!DOCTYPE html>
<head>
<meta charset='utf-8'>
<script type='text/javascript' src='//cdnjs.cloudflare.com/ajax/libs/paper.js/0.9.18/paper-core.min.js'></script>
<script type='text/javascript'>
paper.install(window);
window.onload = function(){
myCanvas.width = window.innerWidth;
myCanvas.height = window.innerHeight;
paper.setup('myCanvas');