Skip to content

A dart package which helps to use varius functional paradigms like function currying, piping.

License

Notifications You must be signed in to change notification settings

dhaval15/functional

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

}

About

A dart package which helps to use varius functional paradigms like function currying, piping.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages