Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between List take() vs. getRange() in Dart

I want the first n elements of some List. From what I can tell, I have two options: take(n) and getRange(0, n).

  1. What's the difference between them?
  2. When would I use one over the other (assuming I always want the first n elements)?
like image 283
TJ Mazeika Avatar asked Aug 03 '15 16:08

TJ Mazeika


People also ask

What is the difference between list and array in Dart?

A very commonly used collection in programming is an array. Dart represents arrays in the form of List objects. A List is simply an ordered group of objects.

How many types of Dart lists are there?

There are broadly two types of lists on the basis of their length: Fixed Length List. Growable List.

What is the difference between list and map in Dart?

List, Set, Queue are iterable while Maps are not. Iterable collections can be changed i.e. their items can be modified, add, remove, can be accessed sequentially. The map doesn't extend iterable.


1 Answers

The most obvious difference is that take() can only use elements at the beginning, you can combine it with skip() like list.skip(3).take(5) to get similar behavior though.
list.take() is lazy evaluated which works nice with functional style programming and might be more efficient if the elements aren't actually iterated over later.
list.take() also is tolerant when there aren't as many elements in the list as requested. take() returns as many as available, getRange() throws. take() is available on all iterables (also streams), getRange() is only available by default on list.

like image 60
Günter Zöchbauer Avatar answered Nov 07 '22 03:11

Günter Zöchbauer