This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fetch('/endpoint') | |
.then(res => { | |
if (res.ok) { | |
return res.json(); | |
} | |
throw new Error(res.statusText); | |
}) | |
// handle response here | |
.then(({ status, payload }) => { | |
switch (status) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |