Dart / flutter文档中的“ T”,“ f”,“ E”,“ e”,“→”代表什么? [英] what does 'T', 'f', 'E', 'e', '→' stand for in dart/flutter docs?

查看:547
本文介绍了Dart / flutter文档中的“ T”,“ f”,“ E”,“ e”,“→”代表什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习颤振,但我不理解这些字母的含义。

  map< T>(T f (E e))→可迭代的T。 
返回一个新的惰性Iterable,其元素由
以迭代顺序在此Iterable的每个元素上调用f所创建。 [...]

那么,它们代表什么?
T:
f:
E:
e:
→:

解决方案



所以总结:




  • T 是字符串

  • E 是我们正在函数内部处理的元素

  • f 是我们的闭包



让我们更深入地讲一个更复杂的例子。现在,我们将处理



同样,您也可以在此编写闭包( f 函数)方式(也许可以更好地强调以下事实:这是根据当前地图条目值创建全新的MapEntry的功能)。

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

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



这是上面链接的摘录:


使用通用方法



最初,Dart的通用支持仅限于类。一种称为通用方法的较新语法,允许在方法和函数上使用
类型的参数:

  T first< T>( List< T> ts){
//做一些初步的工作或错误检查,然后...
T tmp = ts [0];
//做一些额外的检查或处理...
return tmp;
}

在first()上的泛型类型参数允许您使用
类型参数T在几个地方:



在函数的返回类型中( T )。在参数类型
中( List< T> )。以局部变量的类型( T tmp )。


遵循此链接以获取通用名称约定。


i`m learning the flutter, but i do not understand those letters meaning.

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. [...]

so,what do they stand for? T: f: E: e: →:

解决方案

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 is a language Type in this case the Type of the items of the iterable and is also the type that function f must return.
  • tells you the return type of the whole function (map) in this case an Iterable of T
  • f is the function applied to the Element e that is passed as the parameter to the function so that the function could do some operation with this current value and then return a new value of type T based on the value of the element e.

If you navigate the Iterable map function definition you will see that:

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

So I wanna sharpen my answer starting with the exact map<T> function of the OP and then swich to a more complex example.

Just to clarify all these let's take a concrete class of the Iterable class, the Set class choosing a Set of type String in such a scenario:

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);
}

Here I've got a Set of String Set<String> and I want another Set of String Set<String> so that the value is the same value of the original map, but sorrounded with a prefix of "new:". And for that we could easily use the map<T> along with the closure it wants as paraemters.

The function passed as closure is

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

And if we want we could write it also like that:

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

or even pass a function like that:

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));

And this means that the parameter of the function (closure) is the String value of the element E of the Set we're modifying. We don't even have to specify the type because its inferred by method definition, that's one of the power of generics.

The function (closure) will be applied to all the Set's elements once at a time, but you write it once as a closure.

So summarising:

  • T is String
  • E is the element we are dealing with inside of the function
  • f is our closure

Let's go deeper with a more complex example. We'll now deal with the Dart Map class.

Its map function is define like that:

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

So in this case the previous first and third T is (K2, V2) and the return type of the function f (closure), that takes as element E parameter the pair K and V (that are the key and value of the current MapEntry element of the iteration), is a type of MapEntry<K2, V2> and is the previous second T.

The whole function then return a new Map<K2, V2>

The following is an actual example with 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);
}

In this example I've got a Map<int, String> and I want another Map<int, String> so that (like before) the value is the same value of the original map, but sorrounded with a prefix of "new:".

Again you could write the closure (your f function) also in this way (maybe it highlights better the fact that it's a fanction that create a brand new MapEntry based on the current map entry value).

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

All these symbols are called Generics because they are generic placeholder that correspond to a type or another based on the context you are using them.

That's an extract from the above link:

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).

Follow this link for Generics name conventions.

这篇关于Dart / flutter文档中的“ T”,“ f”,“ E”,“ e”,“→”代表什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆