草庐IT

dart - 'T'、 'f'、 'E'、 'e'、 '→' 在 dart/flutter 文档中代表什么?

coder 2023-07-22 原文

我正在学习 flutter,但我不明白那些字母的含义。

map<T>(T f(E e)) → Iterable<T>
Returns a new lazy Iterable with elements that are created by 
calling f on each element of this Iterable in iteration order. [...]

那么,它们代表什么? 电话: F: 乙: 电子: →:

最佳答案

Iterable.map<T> :

map<T>(T f(E e)) → Iterable<T>

Returns a new lazy Iterable with elements that are created by calling f on each element of this Iterable in iteration order. [...]

  • T是一种语言 Type在这种情况下,可迭代项的类型也是函数 f 的类型必须返回。
  • 告诉你 return type整个函数(map)在这种情况下是一个IterableT
  • f是应用于 Element e 的函数它作为参数传递给函数,这样函数就可以用这个当前值做一些操作,然后返回一个类型为 T 的新值。基于元素的值 e .

如果您导航 Iterable map function definition你会看到:

Iterable<T> map <T>(
    T f(
      E e
    )
)

所以我想从确切的 map<T> 开始完善我的答案OP 的功能,然后切换到更复杂的示例。

为了阐明所有这些,让我们以 Iterable 类的具体类 Set 为例。类选择 Set类型 String在这种情况下:

Set<String> mySet = Set();
for (int i=0; i++<5;) {
  mySet.add(i.toString());
}
var myNewSet = mySet.map((currentValue) => (return "new" + currentValue));
for (var newValue in myNewSet) {
  debugPrint(newValue);
}

这里我有一个字符串集 Set<String>我想要另一个一组字符串 Set<String>以便 值与原始 map 的值相同,但用前缀 "new:" 包围。为此,我们可以轻松地使用 map<T>以及它想要作为参数的闭包。

作为闭包传递的函数是

(currentValue) => ("new:" + currentValue)

如果我们愿意,我们也可以这样写:

(currentValue) {
  return "new:" + currentValue;
}

甚至传递这样的函数:

String modifySetElement(String currentValue) {
  return "new:" + currentValue;
}
  • var myNewSet = mySet.map((value) => ("new:" + value));
  • var myNewSet = mySet.map((value) {return "new:" + value;});
  • var myNewSet = mySet.map((value) => modifySetElement("new:" + value));

这意味着函数(闭包)的参数是 String 元素 ESet我们正在修改。 我们甚至不必指定类型,因为它是通过方法定义推断的,这是泛型的强大功能之一。

该函数(闭包)将一次应用于所有 Set 的元素,但您将其写为一次闭包。

总结一下:

  • T是字符串
  • E是我们在函数内部处理的元素
  • f是我们的结束

让我们用一个更复杂的例子来更深入。我们现在将处理 Dart Map类。

它的 map 函数定义如下:

map<K2, V2>(MapEntry<K2, V2> f(K key, V value)) → Map<K2, V2>

所以在这种情况下,前一个和第三个 T(K2, V2)和函数的返回类型 f (闭包),作为元素 E参数对 KV (即迭代的当前 MapEntry 元素的键和值),是 MapEntry<K2, V2> 的类型并且是前一秒 T .

然后整个函数返回一个新的 Map<K2, V2>

以下是 Map 的实际示例:

Map<int, String> myMap = Map();
for (int i=0; i++<5;) {
  myMap[i] = i.toString();
}
var myNewMap = myMap.map((key, value) => (MapEntry(key, "new:" + value)));
for (var mapNewEntry in myNewMap.entries) {
  debugPrint(mapNewEntry.value);
}

在这个例子中我有一个 Map<int, String>我想要另一个 Map<int, String>这样(像以前一样)值与原始 map 的值相同,但用前缀 "new:" 包围.

同样,您也可以用这种方式编写闭包(您的 f 函数)(也许它更好地突出了一个事实,即它是一个基于当前 map 条目值创建全新 MapEntry 的幻想)。

var myNewMap = myMap.map((key, value) {
    String newString = "new:" + value;
    return MapEntry(key, newString);
});

所有这些符号都称为 Generics因为它们是通用占位符,根据您使用它们的上下文对应于一种或另一种类型。

这是上面链接的摘录:

Using generic methods

Initially, Dart’s generic support was limited to classes. A newer syntax, called generic methods, allows type arguments on methods and functions:

T first<T>(List<T> ts) {
  // Do some initial work or error checking, then...
  T tmp = ts[0];
  // Do some additional checking or processing...
  return tmp;
}

Here the generic type parameter on first () allows you to use the type argument T in several places:

In the function’s return type (T). In the type of an argument (List<T>). In the type of a local variable (T tmp).

关注这个link用于泛型名称约定。

关于dart - 'T'、 'f'、 'E'、 'e'、 '→' 在 dart/flutter 文档中代表什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53894436/

有关dart - 'T'、 'f'、 'E'、 'e'、 '→' 在 dart/flutter 文档中代表什么?的更多相关文章

  1. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  2. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  3. ruby-on-rails - Rails - 子类化模型的设计模式是什么? - 2

    我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co

  4. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  5. ruby - 什么是填充的 Base64 编码字符串以及如何在 ruby​​ 中生成它们? - 2

    我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%

  6. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  7. ruby - 为什么 4.1%2 使用 Ruby 返回 0.0999999999999996?但是 4.2%2==0.2 - 2

    为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返

  8. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  9. ruby - 在 jRuby 中使用 'fork' 生成进程的替代方案? - 2

    在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',

  10. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

随机推荐