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: →:
Underneath the standard 20 through 15 and B, mark D (for doubles), T (for triples), and 3B (for three-in-a-bed). (Three-in-a-bed means all three darts in the same scoring area.
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.
In Dart, we use the late keyword to declare variables that will be initialized later. These are called non-nullable variables as they are initialized after the declaration. Hence, we use the late keyword. Note: Once we declare a non-nullable late variable, the variable can't be null at runtime.
If you are attempting to put a String variable inside another static String you can do the following for simple variables: String firstString = 'sentence'; String secondString = 'This is a $firstString'; Which will output This is a sentence .
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 StringE
is the element we are dealing with inside of the functionf
is our closureLet'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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With