forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurry.ml
149 lines (119 loc) · 4.25 KB
/
curry.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
(* OCamlScript compiler
* Copyright (C) 2015-2016 Bloomberg Finance L.P.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, with linking exception;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*)
(* Author: Hongbo Zhang *)
external function_length : 'a -> int = "js_function_length"
external apply1 :
('a -> 'b) -> 'a -> 'b
= "js_apply1"
external apply2 :
('a -> 'b -> 'c) -> 'a -> 'b -> 'c
= "js_apply2"
external apply3 :
('a -> 'b -> 'c -> 'd)
-> 'a -> 'b -> 'c -> 'd
= "js_apply3"
external apply4 :
('a -> 'b -> 'c -> 'd -> 'e)
-> 'a -> 'b -> 'c -> 'd -> 'e
= "js_apply4"
external apply5 :
('a -> 'b -> 'c -> 'd -> 'e -> 'f )
-> 'a -> 'b -> 'c -> 'd -> 'e -> 'f =
"js_apply5"
external apply6 :
('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g )
-> 'a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g =
"js_apply6"
external apply7 :
('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h )
-> 'a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h =
"js_apply7"
external apply8 :
('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h -> 'i)
-> 'a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g -> 'h -> 'i =
"js_apply8"
external apply_args :
('a -> 'b) -> _ array -> 'b = "js_apply"
external append : 'a array -> 'a array -> 'a array = "caml_array_append"
external sub : 'a array -> int -> int -> 'a array = "caml_array_sub"
let rec curry f args =
let arity = function_length f in
let len = Array.length args in
let d = arity - len in
if d = 0 then
apply_args f args (**f.apply (null,args) *)
else if d < 0 then
(** TODO: could avoid copy by tracking the index *)
curry (Obj.magic (apply_args f (sub args 0 arity)))
(sub args arity (-d))
else
Obj.magic (fun x -> curry f (append args [|x|] ))
(* Generated code
[if/else]
Here it would be nice to just generate
[switch .. default]
*)
let curry1 o x arity =
(match arity with
| 0 -> apply1 (Obj.magic o) x
| 1 -> apply1 (Obj.magic o) x
| 2 -> apply2 (Obj.magic o) x
| 3 -> apply3 (Obj.magic o) x
| 4 -> apply4 (Obj.magic o) x
| 5 -> apply5 (Obj.magic o) x
| 6 -> apply6 (Obj.magic o) x
| 7 -> apply7 (Obj.magic o) x
| _ -> (fun a -> curry o [|x; a |]))
let app1 o x =
let len = function_length o in
if len = 1 || len = 0 then apply1 o x
else Obj.magic (curry1 o x len )
let app2 o x y =
let len = function_length o in
if len = 2 then apply2 o x y
else Obj.magic (curry o [|x; y|])
let app3 o a0 a1 a2 =
let len = function_length o in
if len = 3 then apply3 o a0 a1 a2
else
Obj.magic (curry o [|a0;a1;a2|])
let app4 o a0 a1 a2 a3 =
let len = function_length o in
if len = 4 then apply4 o a0 a1 a2 a3
else
Obj.magic (curry o [|a0;a1;a2; a3 |])
let app5 o a0 a1 a2 a3 a4 =
let len = function_length o in
if len = 5 then apply5 o a0 a1 a2 a3 a4
else
Obj.magic (curry o [|a0;a1;a2; a3; a4 |])
let app6 o a0 a1 a2 a3 a4 a5 =
let len = function_length o in
if len = 6 then apply6 o a0 a1 a2 a3 a4 a5
else
Obj.magic (curry o [|a0;a1;a2; a3; a4; a5 |])
let app7 o a0 a1 a2 a3 a4 a5 a6 =
let len = function_length o in
if len = 7 then apply7 o a0 a1 a2 a3 a4 a5 a6
else
Obj.magic (curry o [|a0;a1;a2; a3; a4; a5; a6 |])
let app8 o a0 a1 a2 a3 a4 a5 a6 a7 =
let len = function_length o in
if len = 8 then apply8 o a0 a1 a2 a3 a4 a5 a6 a7
else
Obj.magic (curry o [|a0;a1;a2; a3; a4; a5; a6; a7|])