Skip to content

Instantly share code, notes, and snippets.

View lilactown's full-sized avatar
🌊
Do not follow in the footsteps of the sages. Seek what they sought.

Will Acton lilactown

🌊
Do not follow in the footsteps of the sages. Seek what they sought.
View GitHub Profile
@lilactown
lilactown / fizzbuzz.re
Created September 10, 2017 23:28
FizzBuzz in Reason
let fizzbuzz num => {
let shouldFizz = num mod 3 == 0;
let shouldBuzz = num mod 5 == 0;
switch (shouldFizz, shouldBuzz) {
| (true, true) => "FizzBuzz"
| (true, false) => "Fizz"
| (false, true) => "Buzz"
| (false, false) => string_of_int num
}
};
@lilactown
lilactown / unfold.re
Last active September 17, 2017 15:18
external _unfold : ('a => Js.t 'whatever) => 'a => stream 'b = "unfold" [@@bs.module "most"];
module Unfold = {
type t 'value 'seed =
| Value 'value 'seed
| Done;
};
external convertUnfoldValue : Js.t {. _done : bool} => Js.t {. seed : 'a, value : 'b} =
"%identity";
@lilactown
lilactown / promises.re
Last active August 20, 2022 07:56
Notes on using JavaScript Promises in ReasonML/BuckleScript
/**
* Making promises
*/
let okPromise = Js.Promise.make((~resolve, ~reject as _) => [@bs] resolve("ok"));
/* Simpler promise creation for static values */
Js.Promise.resolve("easy");
Js.Promise.reject(Invalid_argument("too easy"));
@lilactown
lilactown / example.js
Last active September 19, 2017 17:05
fetch('/endpoint')
.then(res => {
if (res.ok) {
return res.json();
}
throw new Error(res.statusText);
})
// handle response here
.then(({ status, payload }) => {
switch (status) {
@lilactown
lilactown / pledge.re
Created September 28, 2017 03:18
Making it easier to keep our Promises
module type Promise = {
type t 'a;
let then_: ('a => t 'b) => t 'a => t 'b;
let resolve: 'a => t 'a;
let all: array (t 'a) => t (array 'a);
let race: array (t 'a) => t 'a;
let make: (resolve::('a => unit) [@bs] => reject::(exn => unit) [@bs] => unit) => t 'a;
};
module Make (P: Promise) => {
@lilactown
lilactown / captcha.re
Last active December 1, 2017 21:42
Advent of Code, 2017 Day 1 (1/2)
let explode = (input) =>
Js.String.split("", input)
|> Array.map(int_of_string)
|> Array.to_list;
let rec solver = (current, next, rest, first, total) =>
switch (next == first, current == next, rest) {
/* when we're on the last item in the list */
| (true, true, []) => total + current + next
| (false, true, []) => total + current
@lilactown
lilactown / halfway.re
Last active December 1, 2017 22:01
Advent of Code, 2017 Day 1 (2/2)
let explode = (input) =>
Js.String.split("", input)
|> Array.map(int_of_string)
|> Array.to_list;
let solve = (input) => {
let len = String.length(input);
let step = len / 2;
let listInput = explode(input);
let nth = List.nth(listInput);
@lilactown
lilactown / dayOne.re
Last active December 2, 2017 04:13
Advent of Code, 2017 Day 1: A more general solution
let explode = (input) =>
Js.String.split("", input)
|> Array.map(int_of_string);
let rec solver = (digits, len, step, xPos, total) => {
let x = digits[xPos];
let yPos = (xPos + step) mod len;
let y = digits[yPos];
/* curry the next call to solver */
@lilactown
lilactown / dayTwo.re
Created December 2, 2017 21:10
Advent of Code, 2017, Day Two
let splitLines = (input) => Js.String.split("\n", input) |> Array.to_list;
let splitDigits = (line) =>
Js.String.split("\t", line) |> Array.map(int_of_string) |> Array.to_list;
let debug = (input) => {
Js.log(input);
input
};
@lilactown
lilactown / dayThree.re
Created December 4, 2017 09:00
Advent of Code, 2017: Day 3
let abs = Js.Math.abs_int;
/*
Desired output:
1 => (0, 0)
2 => (1, 0)
3 => (1, 1)
4 => (0, 1)
5 => (-1, 1)
6 => (-1, 0)