Documentation

Last updated: August 14, 2018

Installation

Pub

You can setup fpfantasy from pub

Add library into your dependecies:

dependencies: fpfantasy: ^0.0.1

Install it

With pub pub get or with flutter flutter packages get

Import it
import 'package:fpfantasy/fpfantasy.dart';

Or, you can import only what's you need
import 'pacakge:fpfantasy/src/compose.dart';

Curring

curry :: Function -> Int -> Function

Function curry(Function fn, {int argsLength: 2})

About.

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)

Examples ( More examples)

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
                                                    

Pattern matching

matching :: A -> B | Null -> Function

Function matching(T a, [b])

About.

Parameters

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)

Examples ( More examples)
With list

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'
                                                        
With function (It's the equivalent to list)

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

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.

Examples ( More examples)
With global function error

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
                                                        
Without global function error

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
                                    

Immutable

types :: [] -> Function | Null -> Function

<T>(List<T> args, [error(String a)]) => new _Types<T>(args, error).check;

Immutable data structures.

Examples ( More examples)
With global function error

    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
                                                            
Without global function error

    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
                                        

List

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];
                                        

Map

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};
                                            


Examples

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

ap :: [] -> [] -> []

List ap<T>(List functions, List<T> data)

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.

Examples ( More examples)

var addToEnd = (_) => '${_} END';
var result = ap([addToEnd, capitalize], ['some', 'String']);
// capitalize - from Utils (FpFantasy)

print('${result}'); // Some END String END
                                        

Chain

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

Examples ( More examples)

var result = chainList(['some', 'String'])
                .c((_) => '${_} END')
                .c((_) => capitalize(_))
                .result();

print(result); // [Some END String END]
                                            

Compose

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.

Examples ( More examples)

var a = (x) {
    x.add(3);

    return x;
    };
var b = (x) => x.last;
var c = compose([a, b]);

print('${c([1, 2])}'); // 3
                                            

Concat

concat :: A -> B -> C

dynamic concat(dynamic a, dynamic b)

Examples ( More examples)

print(concat(1, 2)); // 12
print(concat('a', 'b')); // ab
print(concat([1, 2], [3, 4])); // [1, 2, 3, 4]
                                                

Equals

Functions

Examples

Concat

concat :: A -> B -> C

dynamic concat(dynamic a, dynamic b)

Examples ( More examples)

print(concat(1, 2)); // 12
print(concat('a', 'b')); // ab
print(concat([1, 2], [3, 4])); // [1, 2, 3, 4]
                                                

Zip

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)

Examples