Dart by Example: Functions

  1. // A simple function definition
  2. yell(str) => str.toUpperCase();
  3. // Functions can have type annotations
  4. List lines(String str) {
  5. return str.split('\n');
  6. }
  7. main() {
  8. var poemLines = lines(poem);
  9. print(yell(poemLines.first));
  10. // functions are first-class
  11. var whisper = (String str) => str.toLowerCase();
  12. print(poemLines.map(whisper).last);
  13. }
  14. const poem = '''
  15. The wren
  16. Earns his living
  17. Noiselessly.''';
  18.  
  1. $ dart functions.dart
  2. THE WREN
  3. noiselessly.

by @jryanio | source | license