Skip to content

Commit 28c155c

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

File tree

2 files changed

+46
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)