Skip to content

Commit 4f11812

Browse files
committed
feat: Add utilities for working with functions
- pipe - compose - pack - apply
1 parent 1730a54 commit 4f11812

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

lib/relic.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ export 'src/middleware/middleware_logger.dart' show logRequests;
2323
export 'src/relic_server.dart';
2424
export 'src/router/lookup_result.dart';
2525
export 'src/router/router.dart';
26+
export 'src/util/functional.dart';

lib/src/util/functional.dart

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
extension Pipe<T> on T {
2+
R pipe<R>(R Function(T) next) => next(this);
3+
}
4+
5+
extension Compose<R, T> on R Function(T) {
6+
R Function(U) compose<U>(T Function(U) inner) => (u) => this(inner(u));
7+
}
8+
9+
// === Apply section ===
10+
extension Apply1<T, R> on R Function(T) {
11+
R apply(T x) => this(x);
12+
}
13+
14+
extension Apply2<R, T, U> on R Function(T, U) {
15+
R Function(U) apply(T t) => (U u) => this(t, u);
16+
}
17+
18+
extension Apply3<R, T, U, V> on R Function(T, U, V) {
19+
R Function(U, V) apply(T t) => (U u, V v) => this(t, u, v);
20+
}
21+
22+
extension Apply4<R, T, U, V, X> on R Function(T, U, V, X) {
23+
R Function(U, V, X) apply(T t) => (U u, V v, X x) => this(t, u, v, x);
24+
}
25+
26+
// === Pack section ===
27+
extension Pack1<R, T> on R Function(T) {
28+
R Function((T,)) get pack => ((T,) x) => this(x.$1);
29+
}
30+
31+
extension Pack2<R, T, U> on R Function(T, U) {
32+
R Function((T, U)) get pack => ((T, U) x) => this(x.$1, x.$2);
33+
}
34+
35+
extension Pack3<R, T, U, V> on R Function(T, U, V) {
36+
R Function((T, U, V)) get pack => ((T, U, V) x) => this(x.$1, x.$2, x.$3);
37+
}
38+
39+
extension Pack4<R, T, U, V, X> on R Function(T, U, V, X) {
40+
R Function((T, U, V, X)) get pack =>
41+
((T, U, V, X) x) => this(x.$1, x.$2, x.$3, x.$4);
42+
}

0 commit comments

Comments
 (0)