Idiomatic Functional Programming and Closures in Dart

By pjain      Published April 25, 2020, 1:14 a.m. in blog Programming   

Functional Programming in Dart

Passing Function as argument

  1. Function can be any int double(int i) => 2i; int square(int i) => ii;

  2. Declare Consumer method with Function argument void exec(int i, Function f) { print(f(i)); }

  3. Pass in function or a Lambda expression in place

    exec(10,double); // 20 exec(10,square); // 100 exec(10, (x) => (x*x));

Closures - functional encapsulation vs OOP

void main() {
  outer() { // closure around incr function
    var c = 0;
    incr(){
      c++;
      print(c);
    }
    return incr;
  }
  var myfn = outer();
  myfn();
  myfn();
  print(myfn);
}
  • Prints 1 2 Closure 'main_outer_incr'

0 comments

There are no comments yet

Add new comment

Similar posts

List++ Advanced Application Features

Interview Questions in Flutter

NAV: URL Launcher, Deep Links

NAV: Navigation Drawers