Skip to content

Instantly share code, notes, and snippets.

View mlms13's full-sized avatar
⚗️

Michael Martin mlms13

⚗️
View GitHub Profile
@mlms13
mlms13 / BooleanRuleEngine.re
Created March 28, 2019 23:35
Evaluate rules using other rules?
// I'm not entirely sure what this thing is. It feels powerful and expressive
// and like you can compose things. Not sure what it would be useful for, though.
// Maybe you could make a UI to build arbitrarily complex predicate functions
// for filtering collections of data?
module RuleEngine = {
type t('a) =
| Rule('a)
| Not(t('a))
| And(t('a), t('a))
@mlms13
mlms13 / DoFuture.re
Created April 9, 2019 00:08
I don't know anything about PPXs, and I'm not even quite a do-notation pro, but I'd really like something like this in Reason
[%do Future]
do {
user <- getUser("some_user_id")
orders = user.orders
firstOrderId <- orders |> List.head |> Future.fromOption
order <- getOrder(firstOrderId)
pure(user.firstName ++ " ordered a " ++ order.productName)
};
Future.(
@mlms13
mlms13 / Overlapping.re
Created April 9, 2019 16:09
Different approaches to representing elements pulled out from the natural flow (menus, tooltips, etc)
let onClick = _ => send(ToggleMenu);
// The following is how elm-ui does it (translated to JSX)
// Pros:
// - Relatively easy to understand
// - `Button` is any normal component
// - It's obvious that the container element can have all of the normal
// layout props and decoration attributes
//
// Cons:
open Typeclasses;
// a binary search tree is a binary tree that holds order-able members and
// keeps lesser values on the left and greater or equal values on the right
module Make = (Order: Ord) => {
type t =
| Empty
| Node(t, Order.t, t);
// O(log n) operation to add a new value in a valid position in the tree
@mlms13
mlms13 / CommonInfix.re
Last active August 20, 2025 21:39
Commonly-used infix functions in functional programming (Reason edition)
let (<|) = ('a => 'b, 'a) => 'b; // purescript and haskell use ($)
let (|>) = ('a, 'a => 'b) => 'b; // purescript uses (#)
let compose: ('b => 'c, 'a => 'b, 'a) => 'c;
let (<<) = compose; // purescript uses (<<<), haskell uses (.)
let composeFlipped: ('a => 'b, 'b => 'c, 'a) => 'c;
let (>>) = composeFlipped; // purescript uses (>>>)
@mlms13
mlms13 / monad.ml
Last active December 17, 2019 17:43
Understanding that module example from the PR
module type Monad =
sig
type 'a t
val map : ('a -> 'b) -> 'a t -> 'b t
val apply : ('a -> 'b) t -> 'a t -> 'b t
val pure : 'a -> 'a t
val bind : 'a t -> ('a -> 'b t) -> 'b t
end
module Option : Monad with type 'a t = 'a option = struct
@mlms13
mlms13 / Api.re
Created December 21, 2019 05:08
Request Weather Data using Relude
open Relude.Globals;
let baseUrl = "http://localhost:5050";
// `getForecast` is an IO that will either succeed with decoded
// ForecastData or fail with a Fetch error.
//
// Since IO is inherently lazy, creating this value doesn't actually
// _do_ anything. Instead it needs to be run, which we do inside an
// `UpdateWithIO` return from a `useReducer` branch in our view.
@mlms13
mlms13 / ReludeRandom.bs.js
Created January 2, 2020 01:34
JS representation of a module when the rei is all at the top level
// Generated by BUCKLESCRIPT, PLEASE EDIT WITH CARE
'use strict';
var ReludeRandom_Seed = require("./ReludeRandom_Seed.bs.js");
var ReludeRandom_Generator = require("./ReludeRandom_Generator.bs.js");
var ReludeRandom_RandomInt = require("./ReludeRandom_RandomInt.bs.js");
var ReludeRandom_RandomList = require("./ReludeRandom_RandomList.bs.js");
var Seed = {
fromInt: ReludeRandom_Seed.fromInt,
@mlms13
mlms13 / Demo.bs.js
Created January 18, 2020 19:59
Structural typing with first-class modules
// Generated by BUCKLESCRIPT, PLEASE EDIT WITH CARE
'use strict';
var List$Modules = require("./List.bs.js");
var String$Modules = require("./String.bs.js");
var x = List$Modules.contains(String$Modules.Eq, "a", /* :: */[
"b",
/* :: */[
"c",
@mlms13
mlms13 / Reader.re
Last active January 29, 2020 22:22
Reader, I guess
let const = (a, _) => a;
let (<<) = (f, g, a) => f(g(a));
module type Type = {type t;};
module Function1 = {
type t('input, 'output) = 'input => 'output;
};
module type Functor = {