前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Dart 匿名函数和箭头函数的思考

Dart 匿名函数和箭头函数的思考

作者头像
星宇大前端
发布2020-04-23 14:34:12
8040
发布2020-04-23 14:34:12
举报
文章被收录于专栏:大宇笔记

不管什么语言,不管是叫闭包,Block,lambda表达式还是箭头函数。其实都是函数的简单写法,为了更方便的在各种场景使用。

学了太多的语言,感觉有点乱,整理一下Dart的函数当参数的写法。

无返回函数做参数


以List forEach函数为例,接收一个参数为int 返回 void的函数。

代码语言:javascript
复制
 /**
   * Applies the function [f] to each element of this collection in iteration
   * order.
   */
  void forEach(void f(E element)) {
    for (E element in this) f(element);
  }
- 最常规的写法
代码语言:javascript
复制
//最常规写法  定义为第一种方法
void printFunc(int element){
  print(element);
}

main(List<String> args) {
  //以forEach 函数调用为例
  List<int> array = [1,2,3];
  array.forEach(printFunc);
}
- 匿名函数写法
代码语言:javascript
复制
main(List<String> args) {
  //和第一种方法相似,只是么有了名字,顾名曰匿名函数
  var f1 = (e){
    print(e);
  };
  //以forEach 函数调用为例
  List<int> array = [1,2,3];
  array.forEach(f1);
}
- 箭头函数写法
代码语言:javascript
复制
main(List<String> args) {
  //以forEach 函数调用为例
  List<int> array = [1,2,3];
  //直接传入箭头函数
  array.forEach((item) => print(item));
}
- 当然箭头函数也可以赋值
代码语言:javascript
复制
main(List<String> args) {
  //箭头函数赋值
  var f = (e) => print(e);
  //以forEach 函数调用为例
  List<int> array = [1,2,3];
  //赋值传入箭头函数
  array.forEach(f);
}

关于匿名函数和箭头函数的赋值,第一反应前面加一个返回值就行,But报错了,没有这种写法。

返回函数做参数


以List reduce函数为例,reduce函数定义如下:

代码语言:javascript
复制
/**
   * Reduces a collection to a single value by iteratively combining elements
   * of the collection using the provided function.
   *
   * The iterable must have at least one element.
   * If it has only one element, that element is returned.
   *
   * Otherwise this method starts with the first element from the iterator,
   * and then combines it with the remaining elements in iteration order,
   * as if by:
   *
   *     E value = iterable.first;
   *     iterable.skip(1).forEach((element) {
   *       value = combine(value, element);
   *     });
   *     return value;
   *
   * Example of calculating the sum of an iterable:
   *
   *     iterable.reduce((value, element) => value + element);
   *
   */
  E reduce(E combine(E value, E element)) {
    Iterator<E> iterator = this.iterator;
    if (!iterator.moveNext()) {
      throw IterableElementError.noElement();
    }
    E value = iterator.current;
    while (iterator.moveNext()) {
      value = combine(value, iterator.current);
    }
    return value;
  }
- 最常规的写法
代码语言:javascript
复制
main(List<String> args) {
  List<int> array = [1,2,3];
  var result =  array.reduce(sum);
  print(result);
}

int sum(int a, int b){
  return a + b;
}
- 匿名函数写法
代码语言:javascript
复制
main(List<String> args) {
  List<int> array = [1,2,3];
  final f = (int a, int b){
    return a + b;
  };
  final result1 = array.reduce(f);
  print(result1);
}
- 箭头函数
代码语言:javascript
复制
main(List<String> args) {
  List<int> array = [1,2,3];
  var result2 =  array.reduce((a,b)=> a+b);
  print(result2);
}
- 箭头函数赋值
代码语言:javascript
复制
main(List<String> args) {
  List<int> array = [1,2,3];
  final f1 = (int a,int b) => a + b;
  var result2 =  array.reduce(f1);
  print(result2);
}

总结

匿名函数的简写就是箭头函数。 其实仔细想想函数的语法糖就是匿名函数,匿名函数的简写就是箭头函数。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/04/22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 无返回函数做参数
    • - 最常规的写法
      • - 匿名函数写法
        • - 箭头函数写法
          • - 当然箭头函数也可以赋值
          • 返回函数做参数
            • - 最常规的写法
              • - 匿名函数写法
                • - 箭头函数
                  • - 箭头函数赋值
                  • 总结
                  领券
                  问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档