Skip to content

Instantly share code, notes, and snippets.

View mlms13's full-sized avatar
⚗️

Michael Martin mlms13

⚗️
View GitHub Profile
@mlms13
mlms13 / ChangelogBuilder.re
Last active February 15, 2019 23:34
Categorize Git Commits
module Git = {
type commitish = Sha(string);
type commit = {
title: string,
descriptiong: string,
author: string,
date: Date.t
};
@mlms13
mlms13 / PromiseState.re
Created February 12, 2019 22:42
Dealing with async changes to state in ReasonReact
type state = {products: RemoteData.t(array(Product.t), string)};
type action =
| FetchProducts
| SetProducts(RemoteData.t(array(Product.t), string));
let component = ReasonReact.reducerComponent(__MODULE__);
let make = _children => {
...component,
initialState: () => {products: NotAsked},
@mlms13
mlms13 / App.re
Last active January 24, 2019 18:46
let render = ({ state }) => {
let { viewport, page } = state;
let { width, height } = viewport;
let content =
switch (page) {
| Overview => <Welcome />
| ShowUsers(users) => <UserTable users />
| NewUser(userForm) => <AddUser user=userForm />
};
@mlms13
mlms13 / gist:1e08ba6e9136b7d74a2cc57b5ce1bf10
Last active October 8, 2018 10:03
Use the Github API with RationalJS/Future and Belt.Result
type error('err) =
| NetworkError
| ExpectedJson
| ParseError('err);
/**
* Map one kind of Belt.Result.Error into another kind of Belt.Result.Error
* e.g. (('err => 'otherError), Belt.Result.t('a, 'err)) => Belt.Result.t('a, 'otherError)
*/
let mapErr = (fn, r) => switch r {
/* Importing is just renaming values that are already in scope */
let map = Belt.Array.map;
let reactDom = ReasonReact.createDomElement;
let reactString = ReasonReact.string;
/* You can also alias entire modules */
module RadioButton = Huey.Form.RadioButton;
/* Make the component */
let component = ReasonReact.statelessComponent("RadioButtonGroup");
@mlms13
mlms13 / SearchInput.re
Last active May 22, 2018 21:45
A stateful search input that notifies its parent onblur or on the enter key
type state = string;
type action =
| SetValue(string);
let component = ReasonReact.reducerComponent("SearchInput");
let valueFromEvent = (evt) =>
ReactDOMRe.domElementToObj(ReactEventRe.Form.target(evt))##value;
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];
@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)
@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 / 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)