How do I count the number of occurrence of an item in a list? - Dart - dart

I would like to know how many occurrences of a single item consists in a list in dart. As far as I know, the contains method checks only up to the first occurrence and returns a bool.

Following counts how many "1"s are in list:
[1, 2, 1].where((item) => item == 1).length

Related

Check if one list contains every element from second list

Honestly I have no idea how to check if list 1 contains every element from 2nd list, but I need it.
EDIT: i want to check if those 2 lists with same length have the same values, just with different indices
Thank you in advance
Something like this?
void main() {
final list1 = [5, 2, 3, 10, 2, 1, 11, 4];
final list2 = [1, 2, 3, 4, 5];
print(list1.toSet().containsAll(list2)); // true
}
First of all, you need to specify what you mean by "same elements". Do you mean equal elements (according to ==) or identical elements (according to the identical function)? I'm going to assume ==, since that's the most common, and if your elements don't override ==, it's the same thing as identity anyway.
The simplest approach, if possible, is to sort both lists, then compare each element of one list to the element of the other list at the same position. That does require that the elements have an ordering that agrees with == (so a.compareTo(b) == 0 if and only if a == b), and that you are allowed to change the ordering of the lists.
If you do almost anything else, you need to consider what happens if the same element occurs more than once in one of the lists.
Just comparing lengths as well is not enough, since [1, 1, 2] and [1, 2, 2] both have three elements, and all elements of either list is also an element of the other list. If duplicate elements can occur, you need to be counting the elements as well.
You can use UnorderedIterableEquality from package:collection. It implements a counting-based equality check on arbitrary iterables, which includes working on lists.
Example:
const unorderedEquals = UnorderedIterableEquality();
// ...
if (unorderedEquals.equals(list1, list2)) {
// ... lists have same elements ...
}

How can I iterate over list items in Pandoc's lua-filter function?

Pandoc's lua filter makes it really easy to iterate over a document and munge the document as you go. My problem is I can't figure out how to isolate list item elements. I can find lists and the block level things inside each list item, but I can't figure out a way to iterate over list items.
For example let's say I had the following Markdown document:
1. One string
Two string
2. Three string
Four string
Lets say I want to make the first line of each list item bold. I can easily change how the paragraphs are handled inside OrderedLists, say using this filter and pandoc --lua-filter=myfilter.lua --to=markdown input.md
local i
OrderedList = function (element)
i = 0
return pandoc.walk_block(element, {
Para = function (element)
i = i + 1
if i == 1 then return pandoc.Para { pandoc.Strong(element.c) }
else return element end
end
})
end
This will indeed change the first paragraph element to bold, but it only changes the first paragraph of the first list item because it's iterating across all paragraphs in all list items in the list, not on each list item, then on each paragraph.
1. **One string**
Two string
2. Three string
Four string
If I separate the two list items into two separate lists again the first paragraph of the first item is caught, but I want to catch the first paragraph of every list item! I can't find anything in the documentation about iterating over list items. How is one supposed to do that?
The pandoc Lua filter docs have recently been updated with more info on the properties of each type. E.g., for OrderedList elements, the docs should say (it currently says items instead of content, which is a bug):
OrderedList
An ordered list.
content: list items (List of Blocks)
listAttributes: list parameters (ListAttributes)
start: alias for listAttributes.start (integer)
style: alias for listAttributes.style (string)
delimiter: alias for listAttributes.delimiter (string)
tag, t: the literal OrderedList (string)
So the easiest way is to iterate over the content field and change items therein:
OrderedList = function (element)
for i, item in ipairs(element.content) do
local first = item[1]
if first and first.t == 'Para' then
element.content[i][1] = pandoc.Para{pandoc.Strong(first.content)}
end
end
return element
end

Reorder elements in Erlang

I want to redefine the order of a tuple looking for specific words
Example, I have a list of tuples like this:
[{"a",["r001"]},
{"bi",["bidder"]},
{"bo",["an"]}]
But sometimes the order of the tuples can change for example:
[{"bi",["bidder"]},
{"a",["r001"]},
{"bo",["an"]}]
or
[{"bo",["an"]},
{"a",["r001"]},
{"bi",["bidder"]}]
The first string/list of the tuple is my unique key ("bo","a","bi")
But I want to be able to reorder the list of tuples, always like:
[{"a",["r001"]},
{"bi",["bidder"]},
{"bo",["an"]}]
How can I achieve this?
This will do it:
lists:sort(fun({A,_},{B,_}) -> A =< B end, List).
Or this, which will sort by the tuples second element after the first:
lists:sort(List).
I offer the second version, because without the custom sort function, it is faster for data like this.
If you need to sort by specified element, you just sort by specified element
lists:keysort(1, List).

.indexOf() equivalent in Neo4j Cypher

No matter how I swing it, I need some kind of function to find the index of a item in an array supplied as a parameter.
I am trying to simply update items in a collection based on the index of one of their properties in an array, and have been poring through Cypher docs for nearly 2 hours...
It would also be acceptable to order the items by that array, and then run a foreach on the ordered list...
Following #stefan-armbruster answer and great blog post, a slow but simple index_of can be done with:
reduce(x=[-1,0], i IN [1,2,7,5,21,5,1,435] |
CASE WHEN i = 21 THEN [x[1], x[1]+1] ELSE [x[0], x[1]+1] END
)[0]
Here reduce function works with a two elements array: the position and the current index. If an element in your array matches the given condition, the first element of the reduced array will be replaced with the current index.
I put an example on neo4j console http://console.neo4j.org/?id=34byv
I've blogged about that recently. You can use the reduce function with an three element array as state variable containing
the index of the highest occupation so far
the current index (aka iteration number)
the value of highest occupation so far
As an example to find the index of max element in an array:
RETURN reduce(x=[0,0,0], i IN [1,2,2,5,2,1] |
CASE WHEN i>x[2] THEN [x[1],x[1]+1,o] ELSE [x[0], x[1]+1,x[2]] END
)[0]

mongoid: select elements that have at least n elements in array

In mongoid you can query items that have at least one element in array:
Item.any_in(tag_ids: [id1,id2,id3])
You can also select elements that have all elements in array:
Item.all_in(tag_ids: [id1,id2,id3])
My Question: Is there any way to query elements that have at least n elements in array ?
I'd like to query something like Item.at_least(tag_ids: [id1,id2,id3], n: 2) to return any Item that share at least two ids with [id1,id2,id3]
Thanks !
I don't know a pure Mongoid-solution. I also haven't found such query in the MongoDB manual: http://docs.mongodb.org/manual/reference/operator/query-array/
I would use some mix of Mongoid and array operations.
The disadvantage of it is, that all items which have at least 1 of these tags will be loaded.
searched_tag_ids = ['54253ad452656b1d25000000','54253adc52656b1d25010000','54253ae352656b1d25020000']
items_with_min_1_searched_tag = Item.any_in(tag_ids: searched_tag_ids).to_a
items_with_min_2_searched_tag = items_with_min_1_searched_tag.select{|item| (item.tag_ids.collect{|tag_id| tag_id.to_s} & searched_tag_ids).size >=2}

Resources