Skip to content

Commit 3bf1467

Browse files
committed
Make minor additions to Predef
1 parent f326fe0 commit 3bf1467

File tree

3 files changed

+89
-31
lines changed

3 files changed

+89
-31
lines changed

hkmc2/shared/src/test/mlscript-compile/Predef.mjs

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -87,75 +87,88 @@ Predef1 = class Predef {
8787
static pipeFrom(f1, x3) {
8888
return runtime.safeCall(f1(x3))
8989
}
90-
static andThen(f2, g) {
91-
return (x4) => {
90+
static tap(x4, f2) {
91+
let tmp;
92+
tmp = runtime.safeCall(f2(x4));
93+
return (tmp , x4)
94+
}
95+
static pat(f3, x5) {
96+
let tmp;
97+
tmp = runtime.safeCall(f3(x5));
98+
return (tmp , x5)
99+
}
100+
static andThen(f4, g) {
101+
return (x6) => {
92102
let tmp;
93-
tmp = runtime.safeCall(f2(x4));
103+
tmp = runtime.safeCall(f4(x6));
94104
return runtime.safeCall(g(tmp))
95105
}
96106
}
97-
static compose(f3, g1) {
98-
return (x4) => {
107+
static compose(f5, g1) {
108+
return (x6) => {
99109
let tmp;
100-
tmp = runtime.safeCall(g1(x4));
101-
return runtime.safeCall(f3(tmp))
110+
tmp = runtime.safeCall(g1(x6));
111+
return runtime.safeCall(f5(tmp))
102112
}
103113
}
104-
static passTo(receiver, f4) {
114+
static passTo(receiver, f6) {
105115
return (...args) => {
106-
return runtime.safeCall(f4(receiver, ...args))
116+
return runtime.safeCall(f6(receiver, ...args))
107117
}
108118
}
109-
static call(receiver1, f5) {
119+
static call(receiver1, f7) {
110120
return (...args) => {
111-
return f5.call(receiver1, ...args)
121+
return f7.call(receiver1, ...args)
112122
}
113123
}
114-
static pass1(f6) {
124+
static pass1(f8) {
115125
return (...xs) => {
116-
return runtime.safeCall(f6(xs[0]))
126+
return runtime.safeCall(f8(xs[0]))
117127
}
118128
}
119-
static pass2(f7) {
129+
static pass2(f9) {
120130
return (...xs) => {
121-
return runtime.safeCall(f7(xs[0], xs[1]))
131+
return runtime.safeCall(f9(xs[0], xs[1]))
122132
}
123133
}
124-
static pass3(f8) {
134+
static pass3(f10) {
125135
return (...xs) => {
126-
return runtime.safeCall(f8(xs[0], xs[1], xs[2]))
136+
return runtime.safeCall(f10(xs[0], xs[1], xs[2]))
127137
}
128138
}
139+
static passing(f11, ...args) {
140+
return f11.bind(null, ...args)
141+
}
129142
static print(...xs) {
130143
let tmp, tmp1;
131144
tmp = Predef.map(Predef.renderAsStr);
132145
tmp1 = runtime.safeCall(tmp(...xs));
133146
return runtime.safeCall(globalThis.console.log(...tmp1))
134147
}
135-
static printRaw(x4) {
148+
static printRaw(x6) {
136149
let tmp;
137-
tmp = Predef.render(x4);
150+
tmp = Predef.render(x6);
138151
return runtime.safeCall(globalThis.console.log(tmp))
139152
}
140153
static interleave(sep) {
141-
return (...args) => {
154+
return (...args1) => {
142155
let res, len, i, scrut, idx, scrut1, scrut2, tmp, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
143-
scrut2 = args.length === 0;
156+
scrut2 = args1.length === 0;
144157
if (scrut2 === true) {
145158
return []
146159
} else {
147-
tmp = args.length * 2;
160+
tmp = args1.length * 2;
148161
tmp1 = tmp - 1;
149162
tmp2 = globalThis.Array(tmp1);
150163
res = tmp2;
151-
len = args.length;
164+
len = args1.length;
152165
i = 0;
153166
tmp8: while (true) {
154167
scrut = i < len;
155168
if (scrut === true) {
156169
tmp3 = i * 2;
157170
idx = tmp3;
158-
res[idx] = args[i];
171+
res[idx] = args1[i];
159172
tmp4 = i + 1;
160173
i = tmp4;
161174
scrut1 = i < len;
@@ -282,14 +295,14 @@ Predef1 = class Predef {
282295
static tupleGet(xs3, i1) {
283296
return globalThis.Array.prototype.at.call(xs3, i1)
284297
}
285-
static map(f9) {
298+
static map(f12) {
286299
return (...xs4) => {
287300
let tmp;
288-
tmp = Predef.pass1(f9);
301+
tmp = Predef.pass1(f12);
289302
return runtime.safeCall(xs4.map(tmp))
290303
}
291304
}
292-
static fold(f10) {
305+
static fold(f13) {
293306
return (init, ...rest) => {
294307
let i2, len, scrut, tmp, tmp1, tmp2, tmp3;
295308
i2 = 0;
@@ -298,7 +311,7 @@ Predef1 = class Predef {
298311
scrut = i2 < len;
299312
if (scrut === true) {
300313
tmp = runtime.safeCall(rest.at(i2));
301-
tmp1 = runtime.safeCall(f10(init, tmp));
314+
tmp1 = runtime.safeCall(f13(init, tmp));
302315
init = tmp1;
303316
tmp2 = i2 + 1;
304317
i2 = tmp2;
@@ -312,7 +325,7 @@ Predef1 = class Predef {
312325
return init
313326
}
314327
}
315-
static foldr(f11) {
328+
static foldr(f14) {
316329
return (first, ...rest) => {
317330
let len, i2, init, scrut, scrut1, tmp, tmp1, tmp2, tmp3, tmp4, tmp5;
318331
len = rest.length;
@@ -330,7 +343,7 @@ Predef1 = class Predef {
330343
tmp2 = i2 - 1;
331344
i2 = tmp2;
332345
tmp3 = runtime.safeCall(rest.at(i2));
333-
tmp4 = runtime.safeCall(f11(tmp3, init));
346+
tmp4 = runtime.safeCall(f14(tmp3, init));
334347
init = tmp4;
335348
tmp5 = runtime.Unit;
336349
continue tmp6;
@@ -339,7 +352,7 @@ Predef1 = class Predef {
339352
}
340353
break;
341354
}
342-
return runtime.safeCall(f11(first, init))
355+
return runtime.safeCall(f14(first, init))
343356
}
344357
}
345358
}

hkmc2/shared/src/test/mlscript-compile/Predef.mls

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ fun not(x) = x is false
1111
fun (|>) pipeInto(x, f) = f(x)
1212
fun (<|) pipeFrom(f, x) = f(x)
1313

14+
fun (!>) tap(x, f) = f(x); x
15+
fun (<!) pat(f, x) = f(x); x
16+
1417
fun (>>) andThen(f, g)(x) = g(f(x))
1518
fun (<<) compose(f, g)(x) = f(g(x))
1619

@@ -22,6 +25,8 @@ fun pass1(f)(...xs) = f(xs.0)
2225
fun pass2(f)(...xs) = f(xs.0, xs.1)
2326
fun pass3(f)(...xs) = f(xs.0, xs.1, xs.2)
2427

28+
fun passing(f, ...args) = f.bind(null, ...args)
29+
2530

2631
fun print(...xs) =
2732
console.log(...map(renderAsStr)(...xs))

hkmc2/shared/src/test/mlscript/std/PredefTest.mls

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,46 @@ print(1, 2, 3)
5858
//│ = [function]
5959

6060

61+
2 + 2 |> print
62+
//│ > 4
63+
64+
print <| 2 + 2
65+
//│ > 4
66+
67+
(_ * 2) <| 2 + 2 |> (_ + 1)
68+
//│ = 9
69+
70+
2 + 2 !> print
71+
//│ > 4
72+
//│ = 4
73+
74+
print <! 2 + 2
75+
//│ > 4
76+
//│ = 4
77+
78+
print <! 2 + 2 |> (_ + 1)
79+
//│ > 4
80+
//│ = 5
81+
82+
(_ + 1) <| 2 + 2 !> print
83+
//│ > 4
84+
//│ = 5
85+
86+
87+
tuple passing(1, 2, 3) <| 4
88+
//│ = [1, 2, 3, 4]
89+
90+
passing(tuple, 1, 2, 3) of 4, 5, 6
91+
//│ = [1, 2, 3, 4, 5, 6]
92+
93+
(tuple passing(1, 2, 3)) of 4, 5, 6
94+
//│ = [1, 2, 3, 4, 5, 6]
95+
96+
:re
97+
tuple passing(1, 2, 3) of 4, 5, 6
98+
//│ ═══[RUNTIME ERROR] TypeError: f11.bind is not a function
99+
100+
61101
:re
62102
??("oops")
63103
//│ ═══[RUNTIME ERROR] Error: Not implemented: oops

0 commit comments

Comments
 (0)