Skip to content

Instantly share code, notes, and snippets.

View mlms13's full-sized avatar
⚗️

Michael Martin mlms13

⚗️
View GitHub Profile
@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;
/* 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 / 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 {
@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 / 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 / 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 / elm-game-loop.md
Created February 21, 2019 22:21
Like TEA, but for a game loop?

The Elm Architecture, specialized for games.

Key points:

  • Instead of reacting to events, application state is updated on regular intervals
    • The engine collects all input events (keys up/down, mouse movement, button clicks, possibly joysticks, game pads)
    • User of this library specifies how to translate diffs in input events to game actions
  • Instead of a single "update" function, there are several specialized functions
    • physics update
  • given a current state (which should include things like position and velocity) and a time delta, provide a new state
@mlms13
mlms13 / TFirstTLast.re
Created February 24, 2019 18:01
t-first and t-last approach when working with nested data structures in Reason
/**
* t-first and t-last examples when working with `Future.t(Result.t('a, 'e))`
**/
// `Future` and `Result` are both t-last
getJSON("some/url")
|> Future.map(Result.flatMap(decodeUser))
|> Future.map(Result.mapWithDefault(DataFailed, v => ShowData(v)))
|> Future.tap(send);

Current behavior

onfocus: send UpdateLocationSuggesion

UpdateLocationSuggesion

  • if we already have geolocation: send UpdateLocationSuggesions
  • if we don't: send GetLocation

UpdateLocationSuggesions

  • if mobile and empty input and LocationSuccess:
[@bs.module "react-native-calendario"]
external calendar: ReasonReact.reactClass = "default";
type dateRange = {
startDate: Js.Date.t,
endDate: option(Js.Date.t),
};
let dateRangeFromJS = range => {
startDate: range##startDate,