Skip to content

Instantly share code, notes, and snippets.

@susisu
Last active July 25, 2018 18:10
Show Gist options
  • Save susisu/82613db613ebb53c2249e4a0d8abac17 to your computer and use it in GitHub Desktop.
Save susisu/82613db613ebb53c2249e4a0d8abac17 to your computer and use it in GitHub Desktop.

https://bucklescript.github.io/bucklescript-playground//?code=bW9kdWxlJTIwSXRlcmF0b3IlM0ElMjBzaWclMEElMjAlMjB0eXBlJTIwJ2ElMjB0JTBBJTIwJTIwdmFsJTIwbmV4dCUzQSUyMCdhJTIwdCUyMC0lM0UlMjAnYSUyMG9wdGlvbiUwQWVuZCUyMCUzRCUyMHN0cnVjdCUwQSUyMCUyMHR5cGUlMjAnYSUyMHQlMEElMEElMjAlMjB0eXBlJTIwJ2ElMjByZXMlMjAlM0QlMjAlN0IlMEElMjAlMjAlMjAlMjBfZG9uZSUzQSUyMGJvb2wlMjAlNUIlNDBicy5hcyUyMCUyMmRvbmUlMjIlNUQlM0IlMEElMjAlMjAlMjAlMjB2YWx1ZSUzQSUyMCdhJTNCJTBBJTIwJTIwJTdEJTIwJTVCJTQwJTQwYnMuZGVyaXZpbmclMjBhYnN0cmFjdCU1RCUwQSUwQSUyMCUyMGV4dGVybmFsJTIwX25leHQlM0ElMjAnYSUyMHQlMjAtJTNFJTIwJ2ElMjByZXMlMjAlM0QlMjAlMjJuZXh0JTIyJTIwJTVCJTQwJTQwYnMuc2VuZCU1RCUwQSUwQSUyMCUyMGxldCUyMG5leHQlMjBpdGVyJTIwJTNEJTBBJTIwJTIwJTIwJTIwbGV0JTIweCUyMCUzRCUyMF9uZXh0JTIwaXRlciUyMGluJTBBJTIwJTIwJTIwJTIwaWYlMjBfZG9uZSUyMHglMjB0aGVuJTIwTm9uZSUyMGVsc2UlMjBTb21lJTIwKHZhbHVlJTIweCklMEFlbmQlMEElMEElMEFtb2R1bGUlMjB0eXBlJTIwSXRlcmFibGUlMjAlM0QlMjBzaWclMEElMjAlMjB0eXBlJTIwKCdhJTJDJTIwJ2IpJTIwd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.deriving abstract]
external _next: 'a t -> 'a res = "next" [@@bs.send]
let next iter =
let x = _next iter in
if _doneGet x then None else Some (valueGet x)
end
module type Iterable = sig
type ('a, 'b) t
type ('a, 'b) value
val iterator: ('a, 'b) t -> ('a, 'b) value Iterator.t
end
module Iteration (I: Iterable): sig
val iterate: ('a, 'b) I.t -> f:(('a, 'b) I.value -> unit) -> unit
end = struct
let iterate iterable ~f =
let iterator = I.iterator iterable in
let rec loop () =
match Iterator.next iterator with
| Some x -> f x; loop ()
| None -> ()
in loop ()
end
module Array = struct
module Iterable: Iterable with type ('a, 'b) t = 'b array and type ('a, 'b) value = 'b = struct
type ('a, 'b) t = 'b array
type ('a, 'b) value = 'b
let iterator = [%raw {| x => x[Symbol.iterator]() |}]
end
include Iteration (Iterable)
end
module String = struct
module Iterable: Iterable with type ('a, 'b) t = string and type ('a, 'b) value = string = struct
type ('a, 'b) t = string
type ('a, 'b) value = string
let iterator = [%raw {| x => x[Symbol.iterator]() |}]
end
include Iteration (Iterable)
end
module Map = struct
type ('k, 'v) map
module Iterable: Iterable with type ('a, 'b) t = ('a, 'b) map and type ('a, 'b) value = 'a * 'b = struct
type ('a, 'b) t = ('a, 'b) map
type ('a, 'b) value = 'a * 'b
let iterator = [%raw {| x => x[Symbol.iterator]() |}]
end
include Iteration (Iterable)
end
let () =
Array.iterate [|1; 2; 3|] ~f:(fun n ->
Js.log (n + 1)
)
let () =
Array.iterate [|"foo"; "bar"; "baz"|] ~f:(fun msg ->
Js.log (msg ^ "!")
)
let () =
String.iterate "nya" ~f:(fun c ->
Js.log (c ^ "?")
)
let () =
let m: (string, int) Map.map = [%raw {|
new Map([
["foo", 1],
["bar", 2],
])
|}] in
Map.iterate m ~f:(fun (k, v) ->
Js.log (k ^ " -> " ^ (string_of_int v))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment