Skip to content

Commit bc46bf0

Browse files
committed
feat: refactor Partial functions into an object-oriented approach && add PartialR functions
1 parent 0fa0632 commit bc46bf0

File tree

2 files changed

+278
-87
lines changed

2 files changed

+278
-87
lines changed

func.go

Lines changed: 231 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,254 @@
11
package lo
22

3-
// Partial returns new function that, when called, has its first argument set to the provided value.
4-
func Partial[T1, T2, R any](f func(a T1, b T2) R, arg1 T1) func(T2) R {
3+
// Func is a function with 0 argument and 1 return value (null-ary function).
4+
type Func[R any] func() R
5+
6+
// Func1 is a function with 1 argument and 1 return value (unary function),
7+
// which supports partial application.
8+
type Func1[T1, R any] func(T1) R
9+
10+
// MFunc1 casts function f with 1 argument and 1 return value to Func1.
11+
func MFunc1[T1, R any](f Func1[T1, R]) Func1[T1, R] {
12+
return f
13+
}
14+
15+
// Partial binds the first argument (from left to right) of Func1 f to value t1,
16+
// producing Func of smaller arity.
17+
func (f Func1[T1, R]) Partial(t1 T1) Func[R] {
18+
return func() R {
19+
return f(t1)
20+
}
21+
}
22+
23+
// PartialR binds the first argument (from right to left) of Func1 f to value t1,
24+
// producing Func of smaller arity.
25+
func (f Func1[T1, R]) PartialR(t1 T1) Func[R] {
26+
return func() R {
27+
return f(t1)
28+
}
29+
}
30+
31+
// Func2 is a function with 2 arguments and 1 return value (2-ary function),
32+
// which supports partial application.
33+
type Func2[T1, T2, R any] func(T1, T2) R
34+
35+
// MFunc2 casts function f with 2 arguments and 1 return value to Func2
36+
func MFunc2[T1, T2, R any](f Func2[T1, T2, R]) Func2[T1, T2, R] {
37+
return f
38+
}
39+
40+
// Partial binds the first argument (from left to right) of Func2 f to value t1,
41+
// producing Func1 of smaller arity.
42+
func (f Func2[T1, T2, R]) Partial(t1 T1) Func1[T2, R] {
543
return func(t2 T2) R {
6-
return f(arg1, t2)
44+
return f(t1, t2)
745
}
846
}
947

10-
// Partial1 returns new function that, when called, has its first argument set to the provided value.
11-
func Partial1[T1, T2, R any](f func(T1, T2) R, arg1 T1) func(T2) R {
12-
return Partial(f, arg1)
48+
// PartialR binds the first argument (from right to left) of Func2 f to value t2,
49+
// producing Func1 of smaller arity.
50+
func (f Func2[T1, T2, R]) PartialR(t2 T2) Func1[T1, R] {
51+
return func(t1 T1) R {
52+
return f(t1, t2)
53+
}
1354
}
1455

15-
// Partial2 returns new function that, when called, has its first argument set to the provided value.
16-
func Partial2[T1, T2, T3, R any](f func(T1, T2, T3) R, arg1 T1) func(T2, T3) R {
56+
// Func3 is a function with 3 arguments and 1 return value (3-ary function),
57+
// which supports partial application.
58+
type Func3[T1, T2, T3, R any] func(T1, T2, T3) R
59+
60+
// MFunc3 casts function f with 3 arguments and 1 return value to Func3
61+
func MFunc3[T1, T2, T3, R any](f Func3[T1, T2, T3, R]) Func3[T1, T2, T3, R] {
62+
return f
63+
}
64+
65+
// Partial binds the first argument (from left to right) of Func3 f to value t1,
66+
// producing Func2 of smaller arity.
67+
func (f Func3[T1, T2, T3, R]) Partial(t1 T1) Func2[T2, T3, R] {
1768
return func(t2 T2, t3 T3) R {
18-
return f(arg1, t2, t3)
69+
return f(t1, t2, t3)
1970
}
2071
}
2172

22-
// Partial3 returns new function that, when called, has its first argument set to the provided value.
23-
func Partial3[T1, T2, T3, T4, R any](f func(T1, T2, T3, T4) R, arg1 T1) func(T2, T3, T4) R {
73+
// PartialR binds the first argument (from right to left) of Func3 f to value t3,
74+
// producing Func2 of smaller arity.
75+
func (f Func3[T1, T2, T3, R]) PartialR(t3 T3) Func2[T1, T2, R] {
76+
return func(t1 T1, t2 T2) R {
77+
return f(t1, t2, t3)
78+
}
79+
}
80+
81+
// Func4 is a function with 4 arguments and 1 return value (4-ary function),
82+
// which supports partial application.
83+
type Func4[T1, T2, T3, T4, R any] func(T1, T2, T3, T4) R
84+
85+
// MFunc4 casts function f with 4 arguments and 1 return value to Func4
86+
func MFunc4[T1, T2, T3, T4, R any](f Func4[T1, T2, T3, T4, R]) Func4[T1, T2, T3, T4, R] {
87+
return f
88+
}
89+
90+
// Partial binds the first argument (from left to right) of Func4 f to value t1,
91+
// producing Func3 of smaller arity.
92+
func (f Func4[T1, T2, T3, T4, R]) Partial(t1 T1) Func3[T2, T3, T4, R] {
2493
return func(t2 T2, t3 T3, t4 T4) R {
25-
return f(arg1, t2, t3, t4)
94+
return f(t1, t2, t3, t4)
2695
}
2796
}
2897

29-
// Partial4 returns new function that, when called, has its first argument set to the provided value.
30-
func Partial4[T1, T2, T3, T4, T5, R any](f func(T1, T2, T3, T4, T5) R, arg1 T1) func(T2, T3, T4, T5) R {
98+
// PartialR binds the first argument (from right to left) of Func4 f to value t4,
99+
// producing Func3 of smaller arity.
100+
func (f Func4[T1, T2, T3, T4, R]) PartialR(t4 T4) Func3[T1, T2, T3, R] {
101+
return func(t1 T1, t2 T2, t3 T3) R {
102+
return f(t1, t2, t3, t4)
103+
}
104+
}
105+
106+
// Func5 is a function with 5 arguments and 1 return value (5-ary function),
107+
// which supports partial application.
108+
type Func5[T1, T2, T3, T4, T5, R any] func(T1, T2, T3, T4, T5) R
109+
110+
// MFunc5 casts function f with 5 arguments and 1 return value to Func5
111+
func MFunc5[T1, T2, T3, T4, T5, R any](f Func5[T1, T2, T3, T4, T5, R]) Func5[T1, T2, T3, T4, T5, R] {
112+
return f
113+
}
114+
115+
// Partial binds the first argument (from left to right) of Func5 f to value t1,
116+
// producing Func4 of smaller arity.
117+
func (f Func5[T1, T2, T3, T4, T5, R]) Partial(t1 T1) Func4[T2, T3, T4, T5, R] {
31118
return func(t2 T2, t3 T3, t4 T4, t5 T5) R {
32-
return f(arg1, t2, t3, t4, t5)
119+
return f(t1, t2, t3, t4, t5)
33120
}
34121
}
35122

36-
// Partial5 returns new function that, when called, has its first argument set to the provided value
37-
func Partial5[T1, T2, T3, T4, T5, T6, R any](f func(T1, T2, T3, T4, T5, T6) R, arg1 T1) func(T2, T3, T4, T5, T6) R {
123+
// PartialR binds the first argument (from right to left) of Func5 f to value t5,
124+
// producing Func4 of smaller arity.
125+
func (f Func5[T1, T2, T3, T4, T5, R]) PartialR(t5 T5) Func4[T1, T2, T3, T4, R] {
126+
return func(t1 T1, t2 T2, t3 T3, t4 T4) R {
127+
return f(t1, t2, t3, t4, t5)
128+
}
129+
}
130+
131+
// Func6 is a function with 6 arguments and 1 return value (6-ary function),
132+
// which supports partial application.
133+
type Func6[T1, T2, T3, T4, T5, T6, R any] func(T1, T2, T3, T4, T5, T6) R
134+
135+
// MFunc6 casts function f with 6 arguments and 1 return value to Func6
136+
func MFunc6[T1, T2, T3, T4, T5, T6, R any](f Func6[T1, T2, T3, T4, T5, T6, R]) Func6[T1, T2, T3, T4, T5, T6, R] {
137+
return f
138+
}
139+
140+
// Partial binds the first argument (from left to right) of Func6 f to value t1,
141+
// producing Func5 of smaller arity.
142+
func (f Func6[T1, T2, T3, T4, T5, T6, R]) Partial(t1 T1) Func5[T2, T3, T4, T5, T6, R] {
38143
return func(t2 T2, t3 T3, t4 T4, t5 T5, t6 T6) R {
39-
return f(arg1, t2, t3, t4, t5, t6)
144+
return f(t1, t2, t3, t4, t5, t6)
145+
}
146+
}
147+
148+
// PartialR binds the first argument (from right to left) of Func6 f to value t6,
149+
// producing Func5 of smaller arity.
150+
func (f Func6[T1, T2, T3, T4, T5, T6, R]) PartialR(t6 T6) Func5[T1, T2, T3, T4, T5, R] {
151+
return func(t1 T1, t2 T2, t3 T3, t4 T4, t5 T5) R {
152+
return f(t1, t2, t3, t4, t5, t6)
153+
}
154+
}
155+
156+
// Func7 is a function with 7 arguments and 1 return value (7-ary function),
157+
// which supports partial application.
158+
type Func7[T1, T2, T3, T4, T5, T6, T7, R any] func(T1, T2, T3, T4, T5, T6, T7) R
159+
160+
// MFunc7 casts function f with 7 arguments and 1 return value to Func7
161+
func MFunc7[T1, T2, T3, T4, T5, T6, T7, R any](f Func7[T1, T2, T3, T4, T5, T6, T7, R]) Func7[T1, T2, T3, T4, T5, T6, T7, R] {
162+
return f
163+
}
164+
165+
// Partial binds the first argument (from left to right) of Func7 f to value t1,
166+
// producing Func6 of smaller arity.
167+
func (f Func7[T1, T2, T3, T4, T5, T6, T7, R]) Partial(t1 T1) Func6[T2, T3, T4, T5, T6, T7, R] {
168+
return func(t2 T2, t3 T3, t4 T4, t5 T5, t6 T6, t7 T7) R {
169+
return f(t1, t2, t3, t4, t5, t6, t7)
170+
}
171+
}
172+
173+
// PartialR binds the first argument (from right to left) of Func7 f to value t7,
174+
// producing Func6 of smaller arity.
175+
func (f Func7[T1, T2, T3, T4, T5, T6, T7, R]) PartialR(t7 T7) Func6[T1, T2, T3, T4, T5, T6, R] {
176+
return func(t1 T1, t2 T2, t3 T3, t4 T4, t5 T5, t6 T6) R {
177+
return f(t1, t2, t3, t4, t5, t6, t7)
178+
}
179+
}
180+
181+
// Func8 is a function with 8 arguments and 1 return value (8-ary function),
182+
// which supports partial application.
183+
type Func8[T1, T2, T3, T4, T5, T6, T7, T8, R any] func(T1, T2, T3, T4, T5, T6, T7, T8) R
184+
185+
// MFunc8 casts function f with 8 arguments and 1 return value to Func8
186+
func MFunc8[T1, T2, T3, T4, T5, T6, T7, T8, R any](f Func8[T1, T2, T3, T4, T5, T6, T7, T8, R]) Func8[T1, T2, T3, T4, T5, T6, T7, T8, R] {
187+
return f
188+
}
189+
190+
// Partial binds the first argument (from left to right) of Func8 f to value t1,
191+
// producing Func7 of smaller arity.
192+
func (f Func8[T1, T2, T3, T4, T5, T6, T7, T8, R]) Partial(t1 T1) Func7[T2, T3, T4, T5, T6, T7, T8, R] {
193+
return func(t2 T2, t3 T3, t4 T4, t5 T5, t6 T6, t7 T7, t8 T8) R {
194+
return f(t1, t2, t3, t4, t5, t6, t7, t8)
195+
}
196+
}
197+
198+
// PartialR binds the first argument (from right to left) of Func8 f to value t8,
199+
// producing Func7 of smaller arity.
200+
func (f Func8[T1, T2, T3, T4, T5, T6, T7, T8, R]) PartialR(t8 T8) Func7[T1, T2, T3, T4, T5, T6, T7, R] {
201+
return func(t1 T1, t2 T2, t3 T3, t4 T4, t5 T5, t6 T6, t7 T7) R {
202+
return f(t1, t2, t3, t4, t5, t6, t7, t8)
203+
}
204+
}
205+
206+
// Func9 is a function with 9 arguments and 1 return value (9-ary function),
207+
// which supports partial application.
208+
type Func9[T1, T2, T3, T4, T5, T6, T7, T8, T9, R any] func(T1, T2, T3, T4, T5, T6, T7, T8, T9) R
209+
210+
// MFunc9 casts function f with 9 arguments and 1 return value to Func9
211+
func MFunc9[T1, T2, T3, T4, T5, T6, T7, T8, T9, R any](f Func9[T1, T2, T3, T4, T5, T6, T7, T8, T9, R]) Func9[T1, T2, T3, T4, T5, T6, T7, T8, T9, R] {
212+
return f
213+
}
214+
215+
// Partial binds the first argument (from left to right) of Func9 f to value t1,
216+
// producing Func8 of smaller arity.
217+
func (f Func9[T1, T2, T3, T4, T5, T6, T7, T8, T9, R]) Partial(t1 T1) Func8[T2, T3, T4, T5, T6, T7, T8, T9, R] {
218+
return func(t2 T2, t3 T3, t4 T4, t5 T5, t6 T6, t7 T7, t8 T8, t9 T9) R {
219+
return f(t1, t2, t3, t4, t5, t6, t7, t8, t9)
220+
}
221+
}
222+
223+
// PartialR binds the first argument (from right to left) of Func9 f to value t9,
224+
// producing Func8 of smaller arity.
225+
func (f Func9[T1, T2, T3, T4, T5, T6, T7, T8, T9, R]) PartialR(t9 T9) Func8[T1, T2, T3, T4, T5, T6, T7, T8, R] {
226+
return func(t1 T1, t2 T2, t3 T3, t4 T4, t5 T5, t6 T6, t7 T7, t8 T8) R {
227+
return f(t1, t2, t3, t4, t5, t6, t7, t8, t9)
228+
}
229+
}
230+
231+
// Func10 is a function with 10 arguments and 1 return value (10-ary function),
232+
// which supports partial application.
233+
type Func10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, R any] func(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10) R
234+
235+
// MFunc10 casts function f with 10 arguments and 1 return value to Func10
236+
func MFunc10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, R any](f Func10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, R]) Func10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, R] {
237+
return f
238+
}
239+
240+
// Partial binds the first argument (from left to right) of Func10 f to value t1,
241+
// producing Func9 of smaller arity.
242+
func (f Func10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, R]) Partial(t1 T1) Func9[T2, T3, T4, T5, T6, T7, T8, T9, T10, R] {
243+
return func(t2 T2, t3 T3, t4 T4, t5 T5, t6 T6, t7 T7, t8 T8, t9 T9, t10 T10) R {
244+
return f(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10)
245+
}
246+
}
247+
248+
// PartialR binds the first argument (from right to left) of Func10 f to value t10,
249+
// producing Func9 of smaller arity.
250+
func (f Func10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, R]) PartialR(t10 T10) Func9[T1, T2, T3, T4, T5, T6, T7, T8, T9, R] {
251+
return func(t1 T1, t2 T2, t3 T3, t4 T4, t5 T5, t6 T6, t7 T7, t8 T8, t9 T9) R {
252+
return f(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10)
40253
}
41254
}

0 commit comments

Comments
 (0)