iteration

Typescript,'NodeListOf<Element>' is not an array type or a string type

你。 提交于 2020-07-18 03:36:30
问题 Converting my JS to TS strict mode. The following syntax looks fine to me but TS is complaining in the for loop on allSubMenus with: [ts] Type 'NodeListOf<Element>' is not an array type or a string type. What am I missing? function subAct(target:Node){ const allSubMenus : NodeListOf<Element> = document.querySelectorAll('.subMenuItems') for (const sub of allSubMenus){ sub.classList.remove('active') } } 回答1: You need to set the target compiler option to es6 or higher for NodeListOf<T> to be

What is the cleanest way to get the sum of numbers in a collection/list in Dart?

耗尽温柔 提交于 2020-07-18 03:27:31
问题 I don't like using an indexed array for no reason other than I think it looks ugly. Is there a clean way to sum with an anonymous function? Is it possible to do it without using any outside variables? 回答1: Dart iterables now have a reduce function (https://code.google.com/p/dart/issues/detail?id=1649), so you can do a sum pithily without defining your own fold function: var sum = [1, 2, 3].reduce((a, b) => a + b); 回答2: int sum = [1, 2, 3].fold(0, (previous, current) => previous + current); or

Iterative Solution to End-Overlapping Indices

◇◆丶佛笑我妖孽 提交于 2020-07-09 11:50:33
问题 The bounty expires in 5 days . Answers to this question are eligible for a +50 reputation bounty. Luke Poeppel is looking for an answer from a reputable source . I have a list that holds tuples that represent ranges of numbers. My goal is to return all (see the note below; really looking for the longest) possible subsets of this collection that overlap only by the second value in each tuple or not at all. The function I have been using is a recursive solution to this problem. def get_all_end

Filtering files or directories discovered with fs::read_dir()

两盒软妹~` 提交于 2020-07-09 08:56:06
问题 I've got this function: fn folders(dir: &Path) -> Result<Vec<PathBuf>, io::Error> { fs::read_dir(dir)? .into_iter() .map(|x| x.map(|entry| entry.path())) .collect() } It's actually borrowed from here. The function is OK; unfortunately, I don't really understand how it works. Ok(["/home/ey/dir-src/9", "/home/ey/dir-src/11", "/home/ey/dir-src/03 A Letter of Explanation.mp3", "/home/ey/dir-src/02 Egyptian Avenue.mp3", "/home/ey/dir-src/alfa", "/home/ey/dir-src/10"]) The test output shows both

How should I loop over the elements of a C++ container in reverse order? [duplicate]

北慕城南 提交于 2020-06-14 08:32:39
问题 This question already has answers here : Iterating C++ vector from the end to the begin (11 answers) Closed 5 hours ago . Suppose I'm a newbie C++ programmer. I have a C++ container; say, a vector: std::vector<int> vec { 12, 34, 56, 78 }; I know I can iterate over all of the elements with a simple loop: for(std::vector<int>::size_type i = 0; i < vec.size(); i++) { std::cout << vec[i] << '\n'; } and maybe I've even learned a little about Modern C++, so I know I can use a ranged-for loop: for

How should I loop over the elements of a C++ container in reverse order? [duplicate]

你。 提交于 2020-06-14 08:32:09
问题 This question already has answers here : Iterating C++ vector from the end to the begin (11 answers) Closed 5 hours ago . Suppose I'm a newbie C++ programmer. I have a C++ container; say, a vector: std::vector<int> vec { 12, 34, 56, 78 }; I know I can iterate over all of the elements with a simple loop: for(std::vector<int>::size_type i = 0; i < vec.size(); i++) { std::cout << vec[i] << '\n'; } and maybe I've even learned a little about Modern C++, so I know I can use a ranged-for loop: for

Iterating over a list in python using for-loop

一个人想着一个人 提交于 2020-05-24 04:55:05
问题 I have a question about iterating over a list in python. Let's say I have a list: row = ['1', '2', '3'] and want to convert its element to integers, so that: row = [1, 2, 3] . I know I can do it with list comprehension: row = [int(i) for i in row] or for-loop: for i in range(len(row)): row[i] = int(row[i]) My question concerns range(len(row)) part. Can someone answer in layman friendly way, why I can do something like this: for i in row: print(i) But I can't do this: for i in row: row[i] =

Iterate over list of list of maps in terraform

岁酱吖の 提交于 2020-05-13 06:32:06
问题 Consider I have a variable that is a list of list of maps. Example: processes = [ [ {start_cmd: "a-server-start", attribute2:"type_a"}, {start_cmd: "a-worker-start", attribute2:"type_b"} {start_cmd: "a--different-worker-start", attribute2:"type_c"} ], [ {start_cmd: "b-server-start", attribute2:"type_a"}, {start_cmd: "b-worker-start", attribute2:"type_b"} ] ] In each iteration, I need to take out the array of maps, then iterate over that array and take out the values of the map. How do I

Iterate over list of list of maps in terraform

半世苍凉 提交于 2020-05-13 06:27:49
问题 Consider I have a variable that is a list of list of maps. Example: processes = [ [ {start_cmd: "a-server-start", attribute2:"type_a"}, {start_cmd: "a-worker-start", attribute2:"type_b"} {start_cmd: "a--different-worker-start", attribute2:"type_c"} ], [ {start_cmd: "b-server-start", attribute2:"type_a"}, {start_cmd: "b-worker-start", attribute2:"type_b"} ] ] In each iteration, I need to take out the array of maps, then iterate over that array and take out the values of the map. How do I

Filtering Characters from a String [duplicate]

孤街醉人 提交于 2020-05-10 14:29:09
问题 This question already has answers here : Remove specific characters from a string in Python (27 answers) Closed 6 years ago . I need to make a function that takes two strings as imnput and returns a copy of str 1 with all characters from str2 removed. First thing is to iterate over str1 with a for loop, then compare to str2, to accomplish subtraction I should create a 3rd string in which to store the output but I'm a little lost after that. def filter_string(str1, str2): str3 = str1 for