Warning
Dart doesn't have the spread operator ( Like js ) also, doesn't have the overloading methods. So, now you can set only 5 arguments to function (It's the 5 carried functions, 1 -> 2 -> 3 -> 4 -> 5)
You can setup fpfantasy from pub
dependencies:
fpfantasy: ^0.0.1
With pub
pub get
or with flutter
flutter packages get
import 'package:fpfantasy/fpfantasy.dart';
import 'pacakge:fpfantasy/src/compose.dart';
curry :: Function -> Int -> Function
Function curry(Function fn, {int argsLength: 2})
Dart doesn't have the spread operator ( Like js ) also, doesn't have the overloading methods. So, now you can set only 5 arguments to function (It's the 5 carried functions, 1 -> 2 -> 3 -> 4 -> 5)
var a = curry((a, b) => a + b);
var b = a(1);
print('1 + 1 = ${b(1)}, 1 + 2 = ${b(2)}'); // 1 + 1 = 2, 1 + 2 = 3
print('1 + 3 = ${a(1, 3)}, 1 + 2 = ${(a(1)(2))}'); // 1 + 3 = 4, 1 + 2 = 3
matching :: A -> B | Null -> Function
Function matching
You have the two ways to use it.
First - you set to first argument the list with list or map and then you
need to set data.
Second - you can set first parameter - pattern and second - handler, then
you will take again that function (matching) and you can add more patterns.
When you need to call it, you must set data (only one parameter)
var a = matching([
[0, () => 0],
[1, 1],
[2, () => () => 2],
[default_, 'default'] // default_ - from matching (fpfantasy) = 'default'
]);
print('0 = ${a(0)}, 1 = ${a(1)}, 2 = ${a(2)()}, 3 = ${a(3)}');
// 0 = 0, 1 = 1, 2 = 2, 3 = 'default'
var a = matching(0, () => 0)
(1, 1)
(2, () => () => 2)
(default_, 'default');
print('0 = ${a(0)}, 1 = ${a(1)}, 2 = ${a(2)()}, 3 = ${a(3)}');
// 0 = 0, 1 = 1, 2 = 2, 3 = 'default'
types :: [] -> Function | Null -> Function
<T>(List<T> args, [error(String a)]) => new _Types<T>(args, error).check;
Checks types of parameters give to your function.
var intOrString = types([String, int], (String argType) => throw new Exception('Incorrect type'));
bool foo<T>(T a) {
intOrString(a);
return true;
}
foo('q'); // true
foo(2); // true
fo([]); // exception - Incorrect type
var intOrString = types([String, int]);
bool foo<T>(T a) {
var b = intOrString(a, (_) => false);
return isNull(b) ? true : b;
}
foo('q'); // true
foo(2); // true
fo([]); // false
types :: [] -> Function | Null -> Function
<T>(List<T> args, [error(String a)]) => new _Types<T>(args, error).check;
Immutable data structures.
var intOrString = types([String, int], (String argType) => throw new Exception('Incorrect type'));
bool foo<T>(T a) {
intOrString(a);
return true;
}
foo('q'); // true
foo(2); // true
fo([]); // exception - Incorrect type
var intOrString = types([String, int]);
bool foo<T>(T a) {
var b = intOrString(a, (_) => false);
return isNull(b) ? true : b;
}
foo('q'); // true
foo(2); // true
fo([]); // false
flattenList :: [] -> Function | Null -> list
List flattenList<T>(List<T> data, [dynamic f(T)])
Makes one list from list with any nesting of lists.
var a = [1, 2, 3, [4, 5, [6]]];
print(flattenList(a));
// [1, 2, 3, 4, 5, 6, 7];
print(flattenList(a, (b) => b + 1));
// [2, 3, 4, 5, 6, 7, 8];
flattenMap :: {} -> Function | Null -> Map
Map flattenMap<K, V>(Map<K, V> data, [Map f(K, V)])
Makes one map from map with any nesting of maps.
var a = {'a': 1, 'b': 2, {'c': 3}};
print(flattenMap(a));
// {'a': 1, 'b': 2, 'c': 3};
print(flattenMap(a, (b, c) => {b: c + 1}));
// {'a': 2, 'b': 3, 'c': 4};
generate :: int -> Map
Map<int, int> generate(int index)
Creating map with length what is given by parameter, key and value = index.
fromValues :: [] -> Map
Map<K, V> fromValues<K, V>(List values)
Creating map with length of list length what is given by parameter, key and value = value in the list.
withValues :: [] -> Map
Map<K, V> withValues<K, V>(List values)
Creating map with length of list length what is given by parameter, key = index of each iteration, value = value in the list.
withCallback :: [] -> Function -> Map
Map<K, V> withCallback<K, V>(List values, Map callback(Int, V /* dynamic */))
Creating map with length of list length what is given by parameter, you must set the callback function for each value, key and value = value what is returns from your callback.
ap :: [] -> [] -> []
List ap<T>(List
Takes list of functions, list with data. Calls each function with setting each value from list with data. Your functions must return the value. Returns value when will calls all functions.
var addToEnd = (_) => '${_} END';
var result = ap([addToEnd, capitalize], ['some', 'String']);
// capitalize - from Utils (FpFantasy)
print('${result}'); // Some END String END
chainList :: [] -> ChainList a(c :: Function -> a, result :: List)
var chainList = (List d) => new ChainList(d)
chainMap :: {} -> ChainMap a(c :: Function -> a, result :: {})
var chainMap = (Map d) => new ChainMap(d)
Works like ap
var result = chainList(['some', 'String'])
.c((_) => '${_} END')
.c((_) => capitalize(_))
.result();
print(result); // [Some END String END]
compose :: [] -> A -> B
dynamic _call<T>(List<Function> functions, T data)
Like ap, but takes the any type of data and set it to the each function.
var a = (x) {
x.add(3);
return x;
};
var b = (x) => x.last;
var c = compose([a, b]);
print('${c([1, 2])}'); // 3
concat :: A -> B -> C
dynamic concat(dynamic a, dynamic b)
print(concat(1, 2)); // 12
print(concat('a', 'b')); // ab
print(concat([1, 2], [3, 4])); // [1, 2, 3, 4]
concat :: A -> B -> C
dynamic concat(dynamic a, dynamic b)
print(concat(1, 2)); // 12
print(concat('a', 'b')); // ab
print(concat([1, 2], [3, 4])); // [1, 2, 3, 4]
zipWith :: Function -> List -> List -> List
List zipWith<T>(List f(T y, T x), List<T> a, List<T> b)
zipMap :: List -> List -> Map
Map zipMap<T>(List<T> a, List<T> b)
zip :: List -> List -> List
List zip<T>(List<T> a, List<T> b)