functional 0.9.5

  • Readme
  • Changelog
  • Example
  • Installing
  • new68

Functional #

Utilities for functional programimng in dart.

Install #

Add this in dependencies of pubspec.yaml

functional: ^0.9.4+1

Import #

import 'package:functional/functional.dart';

Usage #

Currying
int add(int a, int b) => a + b;

void main() {
  final inc = add % 1;
  final dec = add % -1;

  print(inc(5)); // Same as add(1, 5);
  print(dec(5)); // Same as add(-1, 5);
}

Currying For Optional Parameters

void printAll({String a, int b, double c}) {
  print(a);
  print(b);
  print(c);
}

void main() {
  final func = printAll % #a['Hello'] % #b[2] % #c[4.0];
  func(); // Same as printAll(a: 'Hello', b: 2, c: 4.0);
}
Function Piping
String toUpperCase(String s) => s.toUpperCase();

void main() {
  final convertToUppercaseAndPrint = toUpperCase | print;
  convertToUppercaseAndPrint('Hello'); 
    // Same as print(toUpperCase('Hello'));
}
Collection Piping
int inc(int a) => a++;

void main() {
    
  [1, 2, 3, 4, 5, 6] | print;
  // Same as [1, 2, 3, 4, 5, 6].forEach((int value) => print(value));

  final incrementedList = [1, 2, 3, 4, 5, 6] ^ inc;
  // Same as [1, 2, 3, 4, 5, 6].map((int value)=> inc(value));

  print(incrementedList);
  // Will print : (2, 3, 4, 5, 6, 7)

  incrementedList | print;
  /* Will print : 
     2
     3
     4
     5
     6
     7
  */
}
Stream Piping

Stream<int> generate(int start, int end, Duration duration) async* {
  for (int i = start; i <= end; i++) {
    yield i;
    await Future.delayed(duration);
  }
}

void main() {
    
  final stream = generate(1, 9, Duration(seconds: 1));
  final subscription = stream | print;
  // Same as stream.listen((event) => print(event));

}

[0.9.5] #

  • Added Currying for pure optional parametered functions.

[0.9.4+1] #

  • Minor Bug Fixes

[0.9.4] #

  • Added Stream Piping

[0.9.3] #

  • README fixed

[0.9.2] #

  • Added example

[0.9.1] #

  • Added pipe and collections

[0.9.0] #

  • Initial Build

example/lib/main.dart

import 'package:functional/functional.dart';

int add(int a, int b) => a + b;

String toUpperCase(String s) => s.toUpperCase();

Stream<int> generate(int start, int end, Duration duration) async* {
  for (int i = start; i <= end; i++) {
    yield i;
    await Future.delayed(duration);
  }
}

void printAll({String a, int b, double c}) {
  print(a);
  print(b);
  print(c);
}

void main() {
  // Currying
  final inc = add % 1;
  final dec = add % -1;

  print(inc(5)); // Same as add(1, 5);
  print(dec(5)); // Same as add(-1, 5);

  // Currying in optional parameters

  final func = printAll % #a['Hello'] % #b[2] % #c[4.0];
  func(); // Same as printAll(a: 'Hello', b: 2, c: 4.0);

  // Function Piping

  final convertToUppercaseAndPrint = toUpperCase | print;
  convertToUppercaseAndPrint('Hello');
  // Same as print(toUpperCase('Hello'));

  // Collection Piping

  [1, 2, 3, 4, 5, 6] | print;
  // Same as [1, 2, 3, 4, 5, 6].forEach((int value) => print(value));

  final incrementedList = [1, 2, 3, 4, 5, 6] ^ inc;
  // Same as [1, 2, 3, 4, 5, 6].map((int value)=> inc(value));

  print(incrementedList);
  // Will print : (2, 3, 4, 5, 6, 7)

  incrementedList | print;
  /* Will print : 
     2
     3
     4
     5
     6
     7
  */

  // Stream piping

  final stream = generate(1, 9, Duration(seconds: 1));
  final subscription = stream | print;
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  functional: ^0.9.5

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:functional/functional.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
37
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
68
Learn more about scoring.

We analyzed this package on Jul 9, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.14

Health issues and suggestions

Document public APIs. (-1 points)

38 out of 38 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Fix lib/src/pipe.dart. (-0.50 points)

Analysis of lib/src/pipe.dart reported 1 hint:

line 8 col 28: Avoid shadowing type parameters.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
Dev dependencies
pedantic ^1.8.0
test ^1.6.0