Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Dart support functional programming?

Does Google Dart language allow for functional programming to occur? In particular, are the following features supported?

  • functions stored as variables (references),
  • functional currying,
  • lazy parameters

Other features of functional programming?

It looks like Dart does not support immutable data.

like image 665
Phil Avatar asked May 25 '13 05:05

Phil


2 Answers

Dart has first-class functions and supports many functional programming constructs. Here are some examples of assigning functions to variables and of a curried function:

main() {   f1(x) => x * 2;         // Define the function f1   var f2 = f1;            // Assign f1 to the variable f2   print(f2(7));           // Feel free to call f2 like any other function    add(a) => (b) => a + b; // Curried addition   print(add(3)(4));       // Calling curried addition    var add3 = add(3);      // Combining the   print(add3(2));         //  concepts } 

As expected, this produces:

 14 7 5 

I don't believe lazy parameters are possible, and you already noted that there is clearly mutable data.

like image 80
Darshan Rivka Whittle Avatar answered Oct 05 '22 16:10

Darshan Rivka Whittle


Depends on what you mean by "functional programming". Functions are first-class objects, which covers point 1, there is Function.apply which lets you implement currying yourself, so that covers point 2, but other than that, Dart isn't very functional (immutability -- no, referential transparency -- no, lazy evaluation -- no, what else have you -- probably also no).

like image 44
Ladicek Avatar answered Oct 05 '22 16:10

Ladicek