Skip to content

Instantly share code, notes, and snippets.

View mlms13's full-sized avatar
⚗️

Michael Martin mlms13

⚗️
View GitHub Profile
@mlms13
mlms13 / flat-to-tree.js
Last active April 29, 2016 17:17
Collect a flat array of objects as nested map
var flat = [
{ id: "A", name: "A", parent: "" },
{ id: "B", name: "B", parent: "" },
{ id: "C", name: "C", parent: "D" },
{ id: "D", name: "D", parent: "B" },
{ id: "E", name: "E", parent: "A" }
];
function unflatten(list) {
return; //...
#! /usr/bin/env bash
color_black='\033[0;30m'
color_red='\033[0;31m'
color_green='\033[0;32m'
color_yellow='\033[0;33m'
color_blue='\033[0;34m'
color_purple='\033[0;35m'
color_cyan='\033[0;36m'
color_gray='\033[0;37m'
@mlms13
mlms13 / brackets.hx
Last active May 11, 2017 20:45
Balanced Brackets
function match(arr: Array<ParenToken>, leftCount = 0): Bool {
return switch arr.getOption(0) {
case None: leftCount == 0;
case Some(LeftParen): match(arr.tail(), leftCount + 1);
case Some(RightParen): leftCount <= 0 ? false : match(arr.tail(), leftCount - 1);
};
}
function tokenize(str: String): Array<ParenToken> {
@mlms13
mlms13 / postgresql_color_adt.sql
Last active September 19, 2017 02:32
Create a sum type for colors in sql
-- data NamedColor = Red | Green | Blue
-- data CustomColor = CustomColor
-- { r :: Int
-- , g :: Int
-- , b :: Int
-- }
-- data Color = NamedColor | CustomColor
create type custom_color as (r int, g int, b int);
static var coins = [4, 3, 1];
var computed = new Map(); // cache already-solved target amounts
// return the minimum number of coins needed to make change for the
// given target amount, using the list of coins above. Returns `None`
// for requests that don't make sense (target < smallest coin)
// break it into all possible sub-problems, and cache the result from computing those
// did i do it? is this dynamic programming?
function makeChange(target: Int, count: Int): Option<Int> {
return switch computed.getOption(target) {
@mlms13
mlms13 / keybase.md
Last active December 1, 2017 17:37

Keybase proof

I hereby claim:

  • I am mlms13 on github.
  • I am mulmus (https://keybase.io/mulmus) on keybase.
  • I have a public key ASC1pfTBJTZEjJ8xXbxWGMnKwv5AyUjTCKvxW90-0NN3sgo

To claim this, I am signing this object:

@mlms13
mlms13 / Tree.purs
Created January 29, 2018 18:11
Binary Tree Depth
module BinaryTree where
import Prelude
import Data.Maybe (Maybe(..))
import Data.Tuple (Tuple(..))
data Tree a
= Empty
| Node (Tree a) a (Tree a)
@mlms13
mlms13 / Mana.purs
Created February 8, 2018 21:24
Function to optimize tapping lands in MTG based on other cards you might want to cast
data ManaSymbol
= Generic
| Specific SpecificMana
| Split ManaColor ManaColor
data SpecificMana = Colorless | Colored ManaColor
data ManaColor = W | U | R | B | G
newtype Land = Land (List SpecificMana)
@mlms13
mlms13 / perfect.purs
Created February 21, 2018 23:37
Find Perfect Numbers
-- http://www.mrpowell.net/progra/javascript/project4/project4.htm
-- A perfect number is a number where the sum of all the divisors is equal to two times the number. For example.
-- The divisors of the number 6 are 1, 2 , 3 and 6. 2 times 6 is 12.
-- 2 * 6 = 1 + 2 + 3 + 6
-- Write a Java script program which will output the first nine perfect numbers.
import Data.Foldable (fold, sum)
import Data.List (List(..), (:), range, filter, reverse)
import Data.Int (toNumber)
type t('a) =
| NonEmpty('a, list('a));
let singleton = (x: 'a) => NonEmpty(x, []);
let fromList = (l: list('a)): option(t('a)) => switch l {
| [] => None
| [x, ...xs] => Some(NonEmpty(x, xs))
};
let toList = (NonEmpty(x, xs)) => [x, ...xs];