Last active
September 17, 2017 15:18
-
-
Save lilactown/b251c70ce370919ffe6c1bfdd47ba12d to your computer and use it in GitHub Desktop.
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"; | |
/* Creates a stream from a generating function and a seed */ | |
let unfold f seed => | |
_unfold | |
( | |
fun x => | |
switch (f x) { | |
| Unfold.Done => convertUnfoldValue {"_done": true} | |
| Unfold.Value value seed => {"value": value, "seed": seed} | |
} | |
) | |
seed; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A few suggestions:
'a => Js.t 'whatever
external cast : Js.t 'a => Js.t 'b = "%identity";
instead ofconvertUnfoldValue
cast
to force the value to the right typeunfold
, i.e. you can get rid of theseed
parameter. I.e.let unfold f seed => ... seed;
is equivalent tolet unfold f => ...;