How to write map2 for Base.Sequence?

Though it looks like, even under Flambda, all of the options allocate quite a lot, though the one I wrote is a bit better:

let%expect_test "test laziness and allocation" =
  let s = map2 (Sequence.range 0 100) (Sequence.range 0 100) ~f:(+) in
  let s' =
    Sequence.zip (Sequence.range 0 100) (Sequence.range 0 100)
    |> Sequence.map ~f:(fun (x,y) -> x + y)
  in
  let _ =
    Expect_test_helpers_kernel.require_no_allocation [%here]
      (fun () -> Sequence.take s' 10 |> Sequence.to_list)
  in
  [%expect {|
    (* CR require-failed: yminsky/src/z.ml:21:53.
       Do not 'X' this CR; instead make the required property true,
       which will make the CR disappear.  For more information, see
       [Expect_test_helpers_kernel.require]. *)
    ("allocation exceeded limit"
      (allocation_limit (Minor_words 0))
      (minor_words_allocated 565)) |}];
  let s' =
    Expect_test_helpers_kernel.require_no_allocation [%here]
      (fun () -> Sequence.take s 10 |> Sequence.to_list)
  in
  [%expect {|
    (* CR require-failed: yminsky/src/z.ml:33:53.
       Do not 'X' this CR; instead make the required property true,
       which will make the CR disappear.  For more information, see
       [Expect_test_helpers_kernel.require]. *)
    ("allocation exceeded limit"
      (allocation_limit (Minor_words 0))
      (minor_words_allocated 435)) |}];
  print_s [%sexp (s' : int list)];
  [%expect {|
    (0 2 4 6 8 10 12 14 16 18) |}];
1 Like