Skip to content

Commit

Permalink
Switch to tail-recursive List.map in the piqirun.ml runtime library
Browse files Browse the repository at this point in the history
  • Loading branch information
alavrik committed Jun 28, 2015
1 parent eb446e4 commit de71d13
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions piqirun/piqirun.ml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,58 @@ type string_slice =
}


(* the below alternative tail-recursive implementation of stdlib's List.map is
* copied from Core (https://github.com/janestreet/core_kernel)
*
* note that the order of arguments was changed back to match the one of
* stdlib's
*)

let list_map_slow f l = List.rev (List.rev_map f l)

let rec list_count_map f l ctr =
match l with
| [] -> []
| [x1] ->
let f1 = f x1 in
[f1]
| [x1; x2] ->
let f1 = f x1 in
let f2 = f x2 in
[f1; f2]
| [x1; x2; x3] ->
let f1 = f x1 in
let f2 = f x2 in
let f3 = f x3 in
[f1; f2; f3]
| [x1; x2; x3; x4] ->
let f1 = f x1 in
let f2 = f x2 in
let f3 = f x3 in
let f4 = f x4 in
[f1; f2; f3; f4]
| x1 :: x2 :: x3 :: x4 :: x5 :: tl ->
let f1 = f x1 in
let f2 = f x2 in
let f3 = f x3 in
let f4 = f x4 in
let f5 = f x5 in
f1 :: f2 :: f3 :: f4 :: f5 ::
(if ctr > 1000
then list_map_slow f tl
else list_count_map f tl (ctr + 1))

let list_map f l = list_count_map f l 0


module List =
struct
include List

let map = list_map
end


module IBuf =
struct
type t =
Expand Down

0 comments on commit de71d13

Please sign in to comment.