Is there a Dart equivalent of Python's all() method? - dart

I'm looking for a method that returns true if a function test, given an element, returns true for every element of an Iterable, or in my case a List, similar to Python's all() method.

I think what you are looking is the every method.
var numbers = [1, 2, 3, 4, 5];
var evenNumbers = [2, 4, 6, 8, 10];
print(numbers.every((n) => n.isEven)); //false
print(evenNumbers.every((n) => n.isEven)); //true

Iterable#every
Checks whether every element of this iterable satisfies test.
void main() {
print([1, 2, 3].every((i) => i > 0));
}

Related

Is it possible to remove duplicates in Dart with another variable

I have searched a lot for removing duplicates from a list in Dart using ANOTHER variable.
Here is what I mean:
List<int> numbers = [1, 2, 3, 4];
// This list has 4 new elements than the first one
List<int> moreNumbers = [1, 2, 3, 4, 5, 6, 7, 8];
// Now I want to push the moreNumbers unique elements to the numbers one
I want to push it so the end result for the numbers variable should be:
[1, 2, 3, 4, 5, 6, 7, 8];
Is it possible?
void main() {
var lst = [1,2,3,4];
var lst2 = [1,2,3,4,5,6,7,8];
var s = {...(lst+lst2)};
print(s.toList());
}
The trivial approach would be:
for (var number in moreNumbers) {
if (!numbers.contains(number)) {
numbers.add(number);
}
}
Not particularly efficient if numbers is long, because contains on a list can take time proportional to the length of the list.
The time/space trade-off would be creating a set from numbers, because sets have cheap contains:
var alsoNumbers = numbers.toSet(); // Also linear, but only happens once.
for (var number in moreNumbers) {
if (alsoNumbers.add(number)) { // true if elements was added
numbers.add(number);
}
}
(Using add instead of contains ensures that you update the set with new values, so you won't add the same new value twice.)
If you could just make numbers a Set to begin with, it would be much easier to avoid duplicates, just do numbers.addAll(moreNumbers).

How cascading modifies original object?

var intList = [3, 2, 1];
var sorted = intList..toList()..sort(); // [1, 2, 3]
var sorted2 = intList..toList().sort(); // [3, 2, 1]
Why my original list is also being modified in first sort and which list is being sorted in second sort?
NOTE: I'm not looking for the correct way to do it which is this:
var sorted = intList.toList()..sort(); // [1, 2, 3]
x..y evalutes to x. Cascade chains are evaluated left-to-right, so x..y..z is the same as (x..y)..z. Your first example therefore makes calls to toList() and to sort() on the original object.
Member access (.) has higher precedence than the cascade operator (..). Your second example calls sort() on the copy returned by toList(), not on the original object.

What is difference between filtering: Where and takeWhile in Dart

I see both of them (where and takeWhile) has the same function .. or I might miss something here!
The documentation for Iterable.where says:
Returns a new lazy Iterable with all elements that satisfy the predicate test.
The documentation Iterable.takeWhile says:
Returns a lazy iterable of the leading elements satisfying test.
(emphasis added).
In other words, Iterable.takeWhile will stop iterating once it reaches the first item that does not satisfy the test callback.
A concrete example:
var list = [1, 1, 2, 3, 5, 8];
print(list.where((x) => x.isOdd).toList()); // Prints: [1, 1, 3, 5]
print(list.takeWhile((x) => x.isOdd).toList()); // Prints: [1, 1]

Convert nested list (2d list) to one list of elements using built-in methods like map() in Dart

How can I convert 2d list to 1d list with all the elements in dart?
I have this 2d List:
List<List<int>> list2d = [[1, 2], [3, 4]];
What I want is:
List<int> list1d = [1, 2, 3, 4];
By converting the first one (2d) to the second (1d) but without writing any (for/while) loops code, if there any built-in methods like map()/where()/cast()/...etc.
Any ideas?
As other have pointed out, expand does what you want
var list2d = [[1, 2], [3, 4]];
var list1d = list2d.expand((x) => x).toList();
You can also, and perhaps preferably, use a list literal:
var list1d = [for (var list in list2d) ...list];
In general, iterable.expand((x) => e).toList() is equivalent to [for (var x in iterable) ...e].
Simply by using the reduce function like this:
List<int> list1d = list2d.reduce((value, element) {
value.addAll(element);
return value;
});
Definition:
List<T> reduce(List<T> Function(List<T>, List<T>) combine);
You can just use the .expand method:
List<int> list1d = list2d.expand((e) => e).toList();

How can I order objects by price in Dart?

I have tried to sort the list of objects based on the price of each individual object has. However, I have got this error the expression here has a type of void, and therefore it cannot be used
class Item{
String productName;
double price;
}
List<Item> items = ...;
items.sort((a, b) => a.price.compareTo(b.price));
List.sort modifies the object on which it is call. It doesn't return any value and you have to use the original list.
var list = [3, 1, 2];
list.sort();
print(list); // displays [1, 2, 3]
If you want to inline the .sort() to use the list directly, you can use the cascade notation:
var list = [3, 1, 2]..sort();
print(list); // displays [1, 2, 3]
// or
var list = [3, 1, 2];
print(list..sort()); // displays [1, 2, 3]

Resources