Last active
February 28, 2021 20:23
-
-
Save idkjs/165bb731de21927e82b9367e2cb45832 to your computer and use it in GitHub Desktop.
UnboxedAnyVariantConstructor
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
/** Compatible with OCaml standard library */ | |
/** https://discord.com/channels/235176658175262720/235199119747055616/705145905971462275 */ | |
module type Iter = { | |
type t('a); | |
let iter: ('a => unit, t('a)) => unit; | |
}; | |
module JsIterable = { | |
type t('a); | |
type iterator('a); | |
type symbol; | |
[@bs.scope "Symbol"] [@bs.val] external iterator: symbol = ""; | |
[@bs.get_index] external getIterator: (t('a), symbol) => (. unit) => iterator('a) = ""; | |
[@bs.send] external next: iterator('a) => {. "_done": bool, "value": option('a) } = ""; | |
let rec iter(iterator, f) = { | |
let curr = next(iterator); | |
if (!curr##_done) { | |
switch (curr##value) { | |
| Some(value) => f(value) | |
| None => () | |
}; | |
iter(iterator, f); | |
}; | |
}; | |
let iter(f, t) = { | |
let iterator = getIterator(t, iterator)(.); | |
iter(iterator, f); | |
}; | |
}; | |
[|1,2,3,4|] |> Obj.magic |> JsIterable.iter(Js.log); |
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
@ocaml.doc(" Compatible with OCaml standard library ") | |
@ocaml.doc( | |
" https://discord.com/channels/235176658175262720/235199119747055616/705145905971462275 " | |
) | |
module type Iter = { | |
type t<'a> | |
let iter: ('a => unit, t<'a>) => unit | |
} | |
module JsIterable = { | |
type t<'a> | |
type iterator<'a> | |
type symbol | |
@scope("Symbol") @val external iterator: symbol = "" | |
@get_index external getIterator: (t<'a>, symbol, . unit) => iterator<'a> = "" | |
@bs.send | |
external next: iterator<'a> => {"_done": bool, "value": option<'a>} = "" | |
let rec iter = (iterator, f) => { | |
let curr = next(iterator) | |
if !curr["_done"] { | |
switch curr["value"] { | |
| Some(value) => f(value) | |
| None => () | |
} | |
iter(iterator, f) | |
} | |
} | |
let iter = (f, t) => { | |
let iterator = getIterator(t, iterator)(.) | |
iter(iterator, f) | |
} | |
} | |
[1, 2, 3, 4] |> Obj.magic |> JsIterable.iter(Js.log) |
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
@unboxed | |
type rec any = Any('a): any | |
let foo = input => | |
switch input { | |
| "a" => Any(["a"]) | |
| "b" => Any(true) | |
| _ => raise(Invalid_argument(input)) | |
} | |
<!-- https://discord.com/channels/235176658175262720/235199119747055616/802615090876645396 --> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment