2

I found iterable functions, but I am not sure how I can use. For example, skip, take, map, forEach, fold and join

Could you give me examples how to use?

1

2 Answers 2

2

Yes, let's check the following sample code.

List<int> values = [1, 2, 3, 4, 5, 6, 7, 8, 9];
    
print(values.skip(5).toList());
//[6, 7, 8, 9]
    
print(values.skip(5).take(3).toList());
//[6, 7, 8]
    
values.skip(5).take(3).map((e) => e.toString()).forEach((element) {print(element);});
//6 7 8
    
String str = values.fold("initialValue",
        (previousValue, element) => previousValue + ", " + element.toString());    
print(str);
//initialValue, 1, 2, 3, 4, 5, 6, 7, 8, 9
    
str = values.join(", ");
print(str);
//1, 2, 3, 4, 5, 6, 7, 8, 9
  • skip(1) skips the first value, 1, in the values list literal.
  • take(3) gets the next 3 values 2, 3, and 4 in the values list literal.
  • map() Returns a new lazy [Iterable] with elements that are created by calling f on each element of this Iterable in iteration order.
  • fork() Reduces a collection to a single value by iteratively combining each element of the collection with an existing value
  • join() Converts each element to a [String] and concatenates the strings.
Sign up to request clarification or add additional context in comments.

Comments

0

Hi Avdienko and welcome to Stack Overflow. I will give you an example for a .forEach iterable function performed on a List.

List<int> listOfIntegers = [1, 2, 3, 4, 5];

listOfIntegers.forEach((element) {
   print(element.toString() + " ");
});

This code will result in printing "1 2 3 4 5 " to the console.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.