pure
所属分类:Dart语言编程
开发工具:Dart
文件大小:0KB
下载次数:0
上传日期:2022-03-20 19:40:37
上 传 者:
sh-1993
说明: Dart的函数编程要点:尾部调用优化、部分应用、记忆和更多
(Functional programming essentials for Dart: tail call optimization, partial application, memoization, _and_ more)
文件列表:
CHANGELOG.md (449, 2022-03-20)
LICENSE (1069, 2022-03-20)
analysis_options.yaml (47, 2022-03-20)
example/ (0, 2022-03-20)
example/CHANGELOG.md (29, 2022-03-20)
example/analysis_options.yaml (438, 2022-03-20)
example/bin/ (0, 2022-03-20)
example/bin/example.dart (854, 2022-03-20)
example/lib/ (0, 2022-03-20)
example/lib/example.dart (1851, 2022-03-20)
example/pubspec.lock (401, 2022-03-20)
example/pubspec.yaml (263, 2022-03-20)
lib/ (0, 2022-03-20)
lib/pure.dart (933, 2022-03-20)
lib/src/ (0, 2022-03-20)
lib/src/common/ (0, 2022-03-20)
lib/src/common/curried_function_types.dart (1852, 2022-03-20)
lib/src/common/function_types.dart (1481, 2022-03-20)
lib/src/composition/ (0, 2022-03-20)
lib/src/composition/dot.dart (269, 2022-03-20)
lib/src/composition/pipe.dart (367, 2022-03-20)
lib/src/constant/ (0, 2022-03-20)
lib/src/constant/extensions.dart (263, 2022-03-20)
lib/src/curry/ (0, 2022-03-20)
lib/src/curry/extensions.dart (2275, 2022-03-20)
lib/src/flip/ (0, 2022-03-20)
lib/src/flip/extensions.dart (1693, 2022-03-20)
lib/src/functions/ (0, 2022-03-20)
lib/src/functions/functions.dart (918, 2022-03-20)
lib/src/memoization/ (0, 2022-03-20)
lib/src/memoization/extensions.dart (3247, 2022-03-20)
lib/src/memoization/helpers.dart (410, 2022-03-20)
lib/src/nullable/ (0, 2022-03-20)
lib/src/nullable/extensions.dart (3055, 2022-03-20)
lib/src/partial_application/ (0, 2022-03-20)
lib/src/partial_application/extensions.dart (2205, 2022-03-20)
... ...
# Pure
Pure is a Dart package that brings functional programming essentials through extension methods. It offers function composition/pipes, function memoization, partial application, and recursion trampolines.
## Extensions
Down are listed extensions included in the `pure` package. Every extension is valid from 0/1 arguments up to 9 arguments.
### Composition
Provides function composition functionality.
Substituting function application with composition or pipes helps to make code more readable and reduces nesting.
```dart
double intToDouble(int x) => x.toDouble();
String doubleToString(double x) => x.toString();
/// Traditional application
String intToStringApplication(int x) => doubleToString(intToDouble(x));
/// Composition
final intToStringComposition = doubleToString.dot(intToDouble);
/// Pipes
String intToStringPipe1(int x) => x.pipe(intToDouble).pipe(doubleToString);
// or
String intToStringPipe2(int x) => intToDouble(x).pipe(doubleToString);
```
### Constant
Allows to create a function from a single value that discards its only argument and returns the value.
For example, can be used to map a list of numbers to a single number.
```dart
const numbers = [1, 2, 3];
final zeros = numbers.map(0.constant); /// Equivalent to `numbers.map((number) => 0);`
```
### Curry and Uncurry
Allows to curry an uncurried function and uncurry a curried function.
Currying is a process of transforming a function that takes multiple arguments into a chain of functions that take a single argument and returns another function, as many times as there were arguments in the original function.
```dart
String createPerson(String country, int age, String name) => "$country, $name, $age";
final createTwin = createPerson.curry("Canada")(12);
final tim = createTwin("Tim");
final jim = createTwin("Jim");
```
### Flipping
Allows to flip the arguments, reversing their order.
```dart
String createPerson(String firstName, String lastName) => "$firstName, $lastName";
final createFormalPerson = createPerson.flip;
print(createFormalPerson('John', 'Doe')); /// Prints "Doe, John".
```
### Nullable invocation
Allows to call a function that normally takes non-nullable arguments with nullable ones and runs it only when all arguments are not null indeed.
```dart
int sum(int first, int second) => first + second;
final sumNullable = sum.nullable;
print(sumNullable(2, 2)); /// Prints "4"
print(sumNullable(2, null)); /// Prints "null"
```
### Recursion Trampolines
Provides stack-safe recursion functionality.
Trampolines essentially do the same work as compiler's tail call optimization – flatten the stack of calls, making function never exceeding stack and never resulting in Stack Overflow.
```dart
/// Regular
int regularSum(int number, int sum) =>
number == 0 ? sum : regularSum(number - 1, sum + number);
/// Trampolined
Tram tramSum(int number, int sum) => number == 0
? Tram.done(sum)
: Tram.call(() => tramSum(number - 1, sum + number));
final n = 99999999;
final trampolined1 = tramSum.bounce(n, 0); // 4999999950000000
// or
final trampolined12 = tramSum(n, 0).bounce(); // 4999999950000000
final regular = regularSum(n, 0); // Unhandled exception: Stack Overflow
```
### Memoization
Provides hash-based memoization functionality.
Allows to perform optimization and perform actual computations only when inputs given to the function are given for the first time. It is highly discouraged to use memoization on impure functions, mainly because it does not make very much sense.
Below, memoization is used on an impure function only for demonstrational purposes.
```dart
int Function(int base) newCounter() {
var counter = 0;
return (base) => base + counter++;
}
final counter = newCounter().memoize();
counter(0); // Returns 0, counter becomes 1
counter(10); // Returns 11, counter becomes 2
counter(0); // Returns 0, counter stays 2
counter(11); // Returns 13, counter becomes 3
```
### Partial Application
Provides non-curried function partial application.
Existing ways do so through creating a function buffer or through currying, applying arguments, and uncurrying the function back. This approach eliminates the need for two intermediate steps and allows application arguments directly.
```dart
int sumThree(int a, int b, int c) => a + b + c;
int buffer(int b, int c) => sumThree(1, b, c);
final partial = sumThree.apply(1);
```
近期下载者:
相关文件:
收藏者: