问:
Is the ThreadPoolExecutor default strategy for max_workers always appropriate?
Here's the complete program:

```<code>import requests
from bs4 import BeautifulSoup as BS
from concurrent.futures import ThreadPoolExecutor
import time

TIMEOUT = 0.2
URL = 'http://ip-api.com/json'
FPURL = 'https://free-proxy-list.net'
CLASS = 'table table-striped table-bordered'

def try_proxy(td):
    try:
        host = td[0].text
        proxy = f'{host}:{td[1].text}'
        (r := requests.get(URL, proxies={'http': proxy}, timeout=TIMEOUT)).raise_for_status()
        if r.json().get('query', '') == host:
            return proxy
    except Exception:
        pass

(r := requests.get(FPURL)).raise_for_status()

proxies = [tr('td') for tr in BS(r.text, 'lxml').find('table', class_=CLASS)('tr')]

print(f'Checking {len(proxies)} proxies')
s = time.perf_counter()
with ThreadPoolExecutor() as executor:
    for f in executor.map(try_proxy, proxies):
        if f is not None:
            print(f)
    print(f'max_workers = {executor._max_workers}')

e = time.perf_counter()
print(f'{e-s:.2f}s')
</code>```

The objective here is to get a list of potential publicly available (free) proxies that seem to be available and also perform in a timely manner.

That list is obtained by scraping free-proxy-list.net. This currently reveals 301 potential proxy addresses. Then, using the requests module we call an API (ip-api.com/json) that responds with information about the origin of the request. In that way we can prove (or disprove) whether the specified proxy was actually used.

Multithreading seems to be the optimum approach to get the job done as fast as possible.

The program works and executes (as written) in ~2.9 seconds.

Now to my point...

Since Python 3.8 the default strategy for max_workers has been min(32, os.cpu_count() + 4) which, in my case, equates to 24 because os.cpu_count() == 20 on a 10-core Xeon processor.

However, I have determined empirically that giving an explicit max_workers value of 100 reduces the execution time to <1s. Clearly, that is very significant.

For reasons of portability I have long argued that unless there's a reason for explicitly setting max_workers to a very low value then it should be left at its default. But this approach appears to be at the cost of lower performance for some scenarios.

Is there a better (portable) way to optimise performance for this and similar applications?

答:
No, it's not.

Generally, ThreadPoolExecutor is OK for tasks that require a lot of I/O while ProcessPoolExecutor does better when there is need for lot of computational power.
Also, ThreadPoolExecutor is limited by the Python Global Interpreter Lock (GIL) (among other resources check this).

If the time of execution is really important devise your own benchmarks or/and use a profiler.
Customise them according to your needs/data.

For really huge data sets the time savings can worth creating your own benchmarks. Also, it might be the case, for instance, that for data sorted in a certain way,  a set amount of workers might be optimal  in the beginning of the data set, while towards the end another amount of workers might be optimal.

For instance, you could do:

import concurrent.futures
import glob
import time

def manipulate_data_function(data):
    result = torture_data(data)
    return result

timings_1 = []
for workers in range(32, 1, -1):
    start_1 = time.perf_counter()
    with concurrent.futures.ProcessPoolExecutor(max_workers = workers) as executor:
    futures = []
        for file in glob.glob('*txt'):
            futures.append(executor.submit(manipulate_data_function, data))
    timings_1.append(time.perf_counter() - start_1)
```

timings_1 will return a list with the duration of your calculations for workers from 32 to 1.

Body:
Here's the complete program:

import requests
from bs4 import BeautifulSoup as BS
from concurrent.futures import ThreadPoolExecutor
import time

TIMEOUT = 0.2
URL = 'http://ip-api.com/json'
FPURL = 'https://free-proxy-list.net'
CLASS = 'table table-striped table-bordered'

def try_proxy(td):
    try:
        host = td[0].text
        proxy = f'{host}:{td[1].text}'
        (r := requests.get(URL, proxies={'http': proxy}, timeout=TIMEOUT)).raise_for_status()
        if r.json().get('query', '') == host:
            return proxy
    except Exception:
        pass

(r := requests.get(FPURL)).raise_for_status()

proxies = [tr('td') for tr in BS(r.text, 'lxml').find('table', class_=CLASS)('tr')]

print(f'Checking {len(proxies)} proxies')
s = time.perf_counter()
with ThreadPoolExecutor() as executor:
    for f in executor.map(try_proxy, proxies):
        if f is not None:
            print(f)
    print(f'max_workers = {executor._max_workers}')

e = time.perf_counter()
print(f'{e-s:.2f}s')

The objective here is to get a list of potential publicly available (free) proxies that seem to be available and also perform in a timely manner.

That list is obtained by scraping free-proxy-list.net. This currently reveals 301 potential proxy addresses. Then, using the requests module we call an API (ip-api.com/json) that responds with information about the origin of the request. In that way we can prove (or disprove) whether the specified proxy was actually used.

Multithreading seems to be the optimum approach to get the job done as fast as possible.

The program works and executes (as written) in ~2.9 seconds.

Now to my point...

Since Python 3.8 the default strategy for max_workers has been min(32, os.cpu_count() + 4) which, in my case, equates to 24 because os.cpu_count() == 20 on a 10-core Xeon processor.

However, I have determined empirically that giving an explicit max_workers value of 100 reduces the execution time to <1s. Clearly, that is very significant.

For reasons of portability I have long argued that unless there's a reason for explicitly setting max_workers to a very low value then it should be left at its default. But this approach appears to be at the cost of lower performance for some scenarios.

Is there a better (portable) way to optimise performance for this and similar applications?

Body:
No, it's not.

Generally, ThreadPoolExecutor is OK for tasks that require a lot of I/O while ProcessPoolExecutor does better when there is need for lot of computational power.
Also, ThreadPoolExecutor is limited by the Python Global Interpreter Lock (GIL) (among other resources check this).

If the time of execution is really important devise your own benchmarks or/and use a profiler.
Customise them according to your needs/data.

For really huge data sets the time savings can worth creating your own benchmarks. Also, it might be the case, for instance, that for data sorted in a certain way,  a set amount of workers might be optimal  in the beginning of the data set, while towards the end another amount of workers might be optimal.

For instance, you could do:

import concurrent.futures
import glob
import time

def manipulate_data_function(data):
    result = torture_data(data)
    return result

timings_1 = []
for workers in range(32, 1, -1):
    start_1 = time.perf_counter()
    with concurrent.futures.ProcessPoolExecutor(max_workers = workers) as executor:
    futures = []
        for file in glob.glob('*txt'):
            futures.append(executor.submit(manipulate_data_function, data))
    timings_1.append(time.perf_counter() - start_1)

timings_1 will return a list with the duration of your calculations for workers from 32 to 1.


来源: stackexchange

问:
How do I get the currently focused element in HtmlUnit
In my javascript I did

```<code>document.getElementById(myID).focus();
</code>```

to focus the element.

Rather than trying to traverse through the DOM tree, I was wondering if I can simply say something like

```<code>var element = (HtmlTextInput) webClient.getActieWindow().getFocusedElement();
</code>```

but I can't seem to find it in the API

答:
I needed the page as well to get the element</p>
```<code>HtmlTextInput textInput =
 (HtmlTextInput) ((HtmlPage) webClient
    .getCurrentWindow()
    .getEnclosedPage())
  .getFocusedElement();
</code>```

ContentLicense: CC BY-SA 4.0

Body:
In my javascript I did

document.getElementById(myID).focus();

to focus the element.

Rather than trying to traverse through the DOM tree, I was wondering if I can simply say something like

var element = (HtmlTextInput) webClient.getActieWindow().getFocusedElement();

but I can't seem to find it in the API

Body:
I needed the page as well to get the element

HtmlTextInput textInput =
 (HtmlTextInput) ((HtmlPage) webClient
    .getCurrentWindow()
    .getEnclosedPage())
  .getFocusedElement();


问:
POST call converting to GET
I am new to apigee.

I am trying to make a post call in apigee with queryparameters.

The flow is below.

```<code>Client send request --> verify API key ---> extract payload --> remove access token --> call target endpoint conditional based on POST
</code>```

I am using curl command to make POST call.But issue is while calling target endpoint its taking as /GET call and condition is evaluating to false.

答:
Apigee can convert in inbound POST to a back-end GET. In Apigee that means using a policy to rebuild the call, to use a GET method (verb) on your target back-end, when you need to receive a POST from your upstream calling client. You will need to use the AssignMessage policy. These product docs are exactly what you need to review:
https://docs.apigee.com/api-platform/reference/policies/assign-message-policy#set-verb

Example, AssignMessage:

```<code><AssignTo type="request" createNew="false"/>
</code>```

and

```<code><Set> ...  <Verb>GET</Verb>
</code>```

Note though that when converting the method (verb) from POST to GET, the policy will not automatically convert the POST's form parameters to query parameters. You will need to use the <Add> and/or <Remove> functionality of the AssignMessage policy to manipulate the message further. Example use in the AssignMessage policy to add the queryparams, referencing the formparams:

```<code><Add>
    <QueryParams>
        <QueryParam name="q1">{request.formparam.q1}</QueryParam>
    </QueryParams>
</Add>
</code>```

Credit to user Michael Russo for his Answer about this from eight years ago.

Title: POST call converting to GET

Body:
I am new to apigee.

I am trying to make a post call in apigee with queryparameters.

The flow is below.

Client send request --> verify API key ---> extract payload --> remove access token --> call target endpoint conditional based on POST

I am using curl command to make POST call.But issue is while calling target endpoint its taking as /GET call and condition is evaluating to false.

Body:
Apigee can convert in inbound POST to a back-end GET. In Apigee that means using a policy to rebuild the call, to use a GET method (verb) on your target back-end, when you need to receive a POST from your upstream calling client. You will need to use the AssignMessage policy. These product docs are exactly what you need to review:
https://docs.apigee.com/api-platform/reference/policies/assign-message-policy#set-verb

Example, AssignMessage:

<AssignTo type="request" createNew="false"/>

and

<Set> ...  <Verb>GET</Verb>

Note though that when converting the method (verb) from POST to GET, the policy will not automatically convert the POST's form parameters to query parameters. You will need to use the <Add> and/or <Remove> functionality of the AssignMessage policy to manipulate the message further. Example use in the AssignMessage policy to add the queryparams, referencing the formparams:

<Add>
    <QueryParams>
        <QueryParam name="q1">{request.formparam.q1}</QueryParam>
    </QueryParams>
</Add>

Credit to user Michael Russo for his Answer about this from eight years ago.


问:
Use cssText to change child style from parent
I have a data object that contains data to find a specific element that exist within a DOM Tree and after I find that element I want to change it css by adding inline css.

```<code>    {
      outerHtml: '<a class="btn btn-pill">\n                        <svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"></path>',
      selector: ".col-auto.ml-auto.d-print-none>.d-flex>.btn.btn-pill",
      style: `background-color: #206bc4; color: white; svg { color: white }`,
    }

</code>```

I'm utilizing this data and injecting the style data in to the element once I find it. As you can see in my outerHTML, the parent prop also has a child and in my style I have the css to change the svg color as well. But when using my code below. It only changes the style of the a tag as opposed to the svg as well.

Note: the outerHTML & selector are just for finding the correct element to change. Sort of like unique identifiers

All I'm doing in the below code is using my selectors to find the correct element and then add the CSS to the final node.

```<code>export function findAndInject(remove) {
  const data = getData();
  let selector = data.selector.split(',');
  let select;
  for (const prop in selector) {
    if (prop == 0) {
      select = document.querySelector(selector[prop]);
    } else {
      select = select.querySelector(selector[prop]);
    }
  }
  if (remove) {
    select.style.cssText = null;
  } else {
    select.style.cssText = data.style;
  }
}
</code>```

I know the issue is probably that I'm doing style.cssText but is there any other way I can add an inline style that can also be added to the child without creating a stylesheet or using style tags?

Before Injection:

After Injection:

The Plus icon should be white.

答:
I guess the problem is your svg code – at least your example snippet won't work.

Here is a working example:

.icon {
  display: inline-block;
  height: 1em;
  font-size: 1em;
  position: relative;
  bottom: -0.1em;
}

.btn {
  padding: 0.3em;
}```
<pre class="snippet-code-html lang-html prettyprint-override"><code><p>

    <svg class="icon icon-plus" id="icon-plus" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
      <line x1="12" y1="5" x2="12" y2="19" class="icon-inner-stroke"></line>
      <line x1="5" y1="12" x2="19" y2="12" class="icon-inner-stroke"></line>
    </svg> New Campaign

</p>
<p>

    <svg class="icon icon-square-plus" viewBox="0 0 448 512">
      <path fill="currentColor" d="M319.1 232h-72V160c0-13.25-10.74-24-23.1-24S199.1 146.7 199.1 160v72H127.1C114.7 232 103.1 242.7 103.1 256S114.7 280 127.1 280h71.1V352c0 13.25 10.75 24 24 24s23.1-10.75 23.1-24V280h72c13.25 0 23.1-10.75 23.1-24S333.3 232 319.1 232zM384 32H64C28.65 32 0 60.65 0 96v320c0 35.35 28.65 64 64 64h320c35.35 0 64-28.65 64-64V96C448 60.65 419.3 32 384 32zM400 416c0 8.822-7.178 16-16 16H64c-8.822 0-16-7.178-16-16V96c0-8.822 7.178-16 16-16h320c8.822 0 16 7.178 16 16V416z"></path>
    </svg> New Campaign

</p></code>```

In your example the <path> element had both stroke and fill attributes explicitly set to none.

Therefore the currentColor fill rule was omitted.

Another approach could be the use of a custom property/css variable:

    <svg class="icon icon-plus" id="icon-plus" width="16" height="16" viewBox="0 0 24 24" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round" style="stroke:var(--iconColor)">
      <line x1="12" y1="5" x2="12" y2="19" class="icon-inner-stroke"></line>
      <line x1="5" y1="12" x2="19" y2="12" class="icon-inner-stroke"></line>
    </svg> New Campaign
  ```

Body:
I have a data object that contains data to find a specific element that exist within a DOM Tree and after I find that element I want to change it css by adding inline css.

    {
      outerHtml: '\n                        <svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"></path>',
      selector: ".col-auto.ml-auto.d-print-none>.d-flex>.btn.btn-pill",
      style: `background-color: #206bc4; color: white; svg { color: white }`,
    }

I'm utilizing this data and injecting the style data in to the element once I find it. As you can see in my outerHTML, the parent prop also has a child and in my style I have the css to change the svg color as well. But when using my code below. It only changes the style of the a tag as opposed to the svg as well.

Note: the outerHTML & selector are just for finding the correct element to change. Sort of like unique identifiers

All I'm doing in the below code is using my selectors to find the correct element and then add the CSS to the final node.

export function findAndInject(remove) {
  const data = getData();
  let selector = data.selector.split(',');
  let select;
  for (const prop in selector) {
    if (prop == 0) {
      select = document.querySelector(selector[prop]);
    } else {
      select = select.querySelector(selector[prop]);
    }
  }
  if (remove) {
    select.style.cssText = null;
  } else {
    select.style.cssText = data.style;
  }
}

I know the issue is probably that I'm doing style.cssText but is there any other way I can add an inline style that can also be added to the child without creating a stylesheet or using style tags?

Before Injection:

After Injection:

The Plus icon should be white.

Body:
I guess the problem is your svg code – at least your example snippet won't work.

Here is a working example:

.icon {
  display: inline-block;
  height: 1em;
  font-size: 1em;
  position: relative;
  bottom: -0.1em;
}

.btn {
  padding: 0.3em;
}

<p>
  <a class="btn btn-pill" style="background-color: #206bc4; color: white;">
    <svg class="icon icon-plus" id="icon-plus" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
      <line x1="12" y1="5" x2="12" y2="19" class="icon-inner-stroke"></line>
      <line x1="5" y1="12" x2="19" y2="12" class="icon-inner-stroke"></line>
    </svg> New Campaign
  </a>
</p>
<p>
  <a class="btn btn-pill" style="background-color: #206bc4; color: white;">
    <svg class="icon icon-square-plus" viewBox="0 0 448 512">
      <path fill="currentColor" d="M319.1 232h-72V160c0-13.25-10.74-24-23.1-24S199.1 146.7 199.1 160v72H127.1C114.7 232 103.1 242.7 103.1 256S114.7 280 127.1 280h71.1V352c0 13.25 10.75 24 24 24s23.1-10.75 23.1-24V280h72c13.25 0 23.1-10.75 23.1-24S333.3 232 319.1 232zM384 32H64C28.65 32 0 60.65 0 96v320c0 35.35 28.65 64 64 64h320c35.35 0 64-28.65 64-64V96C448 60.65 419.3 32 384 32zM400 416c0 8.822-7.178 16-16 16H64c-8.822 0-16-7.178-16-16V96c0-8.822 7.178-16 16-16h320c8.822 0 16 7.178 16 16V416z"></path>
    </svg> New Campaign
  </a>
</p>

In your example the <path> element had both stroke and fill attributes explicitly set to none.

Therefore the currentColor fill rule was omitted.

Another approach could be the use of a custom property/css variable:

  <a class="btn btn-pill" style="background-color: #eee; --iconColor: green;">
    <svg class="icon icon-plus" id="icon-plus" width="16" height="16" viewBox="0 0 24 24" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round" style="stroke:var(--iconColor)">
      <line x1="12" y1="5" x2="12" y2="19" class="icon-inner-stroke"></line>
      <line x1="5" y1="12" x2="19" y2="12" class="icon-inner-stroke"></line>
    </svg> New Campaign
  </a>


问:
htaccess url masking
Hey im a total niewbie with htaccess.
On my site (mysite.com) i got a link that directs and sends variable to a page on a secure site (othersite.com/somepage.php?urlink='www.mysite.com').

How can I edit my htaccess file to rewrite othersite.com/somepage.php?urlink='www.mysite.com' to whatever value urlink has.

I have this in my htaccess

```<code>Options +FollowSymlinks<br>
Options All -Indexes<br>
Options Indexes Includes FollowSymLinks ExecCGI
RewriteEngine on
RewriteRule ^&urlink=(.*) http://$1 [P]
</code>```

答:
Curious. Think it won't work that way; if you request a page on an other server, your own htaccess won't provide you the solution. (if you have access / own the othersite.com yourself, their htaccess could do it).

Instead, i would write a small PHP-File redirecting to otherside.com using an HTTP_HEADER.
Therefore, you could tell your htaccess to hide your php-Url.

Did I understand your question the right way?

```<code><?php
$myInputURL = $my_POSTURL;
$myOtherSite = "http://www.otherside.com?urllink=";
$myURL = $myOtherSite . $myInputURL;
header("Location: ".$myURL);
?>
</code>```

ContentLicense: CC BY-SA 3.0

Title: htaccess url masking

Body:
Hey im a total niewbie with htaccess.
On my site (mysite.com) i got a link that directs and sends variable to a page on a secure site (othersite.com/somepage.php?urlink='www.mysite.com').

How can I edit my htaccess file to rewrite othersite.com/somepage.php?urlink='www.mysite.com' to whatever value urlink has.

I have this in my htaccess

Options +FollowSymlinks

Options All -Indexes

Options Indexes Includes FollowSymLinks ExecCGI
RewriteEngine on
RewriteRule ^&urlink=(.*) http://$1 [P]

Body:
Curious. Think it won't work that way; if you request a page on an other server, your own htaccess won't provide you the solution. (if you have access / own the othersite.com yourself, their htaccess could do it).

Instead, i would write a small PHP-File redirecting to otherside.com using an HTTP_HEADER.
Therefore, you could tell your htaccess to hide your php-Url.

Did I understand your question the right way?

<?php
$myInputURL = $my_POSTURL;
$myOtherSite = "http://www.otherside.com?urllink=";
$myURL = $myOtherSite . $myInputURL;
header("Location: ".$myURL);
?>


问:
enter a password if 3 time wrong attempt If the user enters two incorrect passwords, but was able to provide the correct password for the 3rd time
can anyone help me with this im just starting to learn JS
Create a program that will exit if the user enters an incorrect password thrice//If the user enters two incorrect passwords, but was able to provide the correct password for the 3rd time, reset your counter variable into 0

//3 incorrect enter of password, add alert("Your account has been blocked!");
heres what i have done so far

```<code>const userPass = "Password123";
let inputPass = prompt("Please enter your password:");
let counter = 0;

while (inputPass != userPass) {
    inputPass = prompt("Please enter your password:");
}

console.log("Thank you for providing the right password!");
</code>```

答:
Here you have the solution:

const userPass = 'Password123'

let inputPass = prompt('Please enter your password:')
let counter = 1
const MAX_ATTEMPS = 3

while (inputPass != userPass) {
  if (counter < 3) inputPass = prompt('Please enter your password:')
  else {
    alert('Account has been blocked')
    break
  }

  counter++
}

counter = 1

console.log('Thank you for providing the right password!')

```

Note that the counter was modified from 0 to 1 and a new constant MAX_ATTEMPS was added.

Also an if statement was added to handle the 3 attemps in the while loop body.

Body:
can anyone help me with this im just starting to learn JS
Create a program that will exit if the user enters an incorrect password thrice//If the user enters two incorrect passwords, but was able to provide the correct password for the 3rd time, reset your counter variable into 0

//3 incorrect enter of password, add alert("Your account has been blocked!");
heres what i have done so far

const userPass = "Password123";
let inputPass = prompt("Please enter your password:");
let counter = 0;

while (inputPass != userPass) {
    inputPass = prompt("Please enter your password:");
}

console.log("Thank you for providing the right password!");

Body:
Here you have the solution:

const userPass = 'Password123'

let inputPass = prompt('Please enter your password:')
let counter = 1
const MAX_ATTEMPS = 3

while (inputPass != userPass) {
  if (counter < 3) inputPass = prompt('Please enter your password:')
  else {
    alert('Account has been blocked')
    break
  }

  counter++
}

counter = 1

console.log('Thank you for providing the right password!')

Note that the counter was modified from 0 to 1 and a new constant MAX_ATTEMPS was added.

Also an if statement was added to handle the 3 attemps in the while loop body.


问:
Microsoft Excel VBA cannot find projects or library
I performed a Windows update yesterday 12/13/2020 and now when I open an Excel file with a macro, I get "Can't find project or library".

In Excel, I choose Developer tab, then click Macros... no macros show up (there should be 2), in fact when I try to type in a name of a new macro it will not give me the option to Create it.

Here is an interesting thing in VBA, I go to Tools then References. A box should show up, but I get what appears to be a partial box, like it was cutoff on the right hand side.

I get the "Can't find project or library" error when exiting the file also.

答:
I have experienced exactly the very same issue on Excel 365 with .xlsm documents located on SharePoint.

"Can't find project or library" prompt fired even when entering values in one cell. It also popped up when exiting the file, preventing the document to be saved. Further, I experienced that the prompt also fires when working on other documents while keeping the affected document open.

The References dialogue box could not be explored just as the OP describes; all the active modules were not viewable/explorable, leaving artifacts (as in figure) when dragged or moved.

The workaround in that case is to have the document opened and saved by another user who has access. This solved the issue every time, with no need to modify any VBA settings.

While I cannot explain the cause of the issue, I confirm that a fresh install of Excel (in line with what reported by @BuilderQ) would still result in the issue to recur.


问:
How do I find all the rows that share column value 2 based on column value 1?
I have a dataframe that looks like this

```<code>Movie          Actor      Year
Ocean's Eleven Brad Pitt  2001
Ocean's Eleven George Clooney 2001
Ocean's Eleven Matt Damon 2001
Pulp Fiction   Samuel L. Jackson 1994
Pulp Fiction   John Travolta 1994
</code>```

etcc...

It basically is a long list of movies with the main actors in that movie
Now my question: how can I find all the movies that share atleast one actor?

For example:

Ocean's Eleven and Ocean's Twelve both share Brad Pitt and George Clooney

Ocean's Eleven and Fury they both have Brad Pitt

Ocean's Eleven and Pulp Fiction do not share any main actors so it should not be found

I know I can use the intersect function to find overlapping elements in vectors/lists however I'm not sure how to do this for all unique movies in my dataframe! I thought of writing a for loop but I figured that there is probably an easier method.

答:
We may use n_distinct after grouping in tidyverse

```<code>library(dplyr)
df1 %>%
 group_by(Actor) %>%
  filter(n_distinct(Movie) > 1) %>%
  ungroup
</code>```

-output

```<code># A tibble: 2 × 3
  Movie          Actor      Year
  <chr>          <chr>     <int>
1 Ocean's Eleven Brad Pitt  2001
2 Fury           Brad Pitt  2222
</code>```

Body:
I have a dataframe that looks like this

Movie          Actor      Year
Ocean's Eleven Brad Pitt  2001
Ocean's Eleven George Clooney 2001
Ocean's Eleven Matt Damon 2001
Pulp Fiction   Samuel L. Jackson 1994
Pulp Fiction   John Travolta 1994

etcc...

It basically is a long list of movies with the main actors in that movie
Now my question: how can I find all the movies that share atleast one actor?

For example:

Ocean's Eleven and Ocean's Twelve both share Brad Pitt and George Clooney

Ocean's Eleven and Fury they both have Brad Pitt

Ocean's Eleven and Pulp Fiction do not share any main actors so it should not be found

I know I can use the intersect function to find overlapping elements in vectors/lists however I'm not sure how to do this for all unique movies in my dataframe! I thought of writing a for loop but I figured that there is probably an easier method.

Body:
We may use n_distinct after grouping in tidyverse

library(dplyr)
df1 %>%
 group_by(Actor) %>%
  filter(n_distinct(Movie) > 1) %>%
  ungroup

-output

# A tibble: 2 × 3
  Movie          Actor      Year
  <chr>          <chr>     <int>
1 Ocean's Eleven Brad Pitt  2001
2 Fury           Brad Pitt  2222


问:
Incremental method to change the cells ID (Name)
I'm trying to setup an excel sheet with different columns and in every column I need the single cells to have a specific ID so I can reach to those by code, the simpler one was to put for ex. "column name1" and incremental from there, do you know a method to make something like that instead of changing all the cells ID by hand?
enter image description here

Like this one for example, but intead of having 'feature1' 'feature2' ecc written as text I need those in the cells IDs/Names

EDIT

example of table
Here I want to use a formula to automatically change the name of the table from "A26" to "step1", I tried to use command ="step"&ROW(A26) but this only change the text of the cell instead of the name

答:
Although I have shared above what ROW Function, does, still for a proper explanation I am sharing here

The ROW function returns the row number for a reference.

In our query, ROW(J26) returns 26, since J26 is the twenty sixth row of the column J in the spreadsheet. When no reference is provided, ROW returns the row number of the cell which contains the formula. So in the above query ROW() returns 26 which on subtracting with 25 shall give us 1 which is required.

ROW function takes just one argument, called reference, which may be empty, a cell reference, or a range.

So the formula used in cell J26

```<code>=J$25&ROW()-25
</code>```

And Fill Down and for the rest just press CTRL R and fill down!

Body:
Although I have shared above what ROW Function, does, still for a proper explanation I am sharing here

The ROW function returns the row number for a reference.

In our query, ROW(J26) returns 26, since J26 is the twenty sixth row of the column J in the spreadsheet. When no reference is provided, ROW returns the row number of the cell which contains the formula. So in the above query ROW() returns 26 which on subtracting with 25 shall give us 1 which is required.

ROW function takes just one argument, called reference, which may be empty, a cell reference, or a range.

So the formula used in cell J26

=J$25&ROW()-25

And Fill Down and for the rest just press CTRL R and fill down!


问:
Navigation Graph(for Game Agent) generation/edition tools
I'm writing a 2d game in which an agent has to go to anywhere of large map, automatically.

Ofcourse, the map's got lots of obstacles, geometries, etc.

I decided to implement it with NavGraph (Navigation Graph), and read a very useful, great article here

After a few minutes, I worried about creating nav graph manually.

I googled hours to look up NavGraph generation tools, but there was nothing. (all result was for NavMesh, not NavGraph)

Is there anybody help me? Any suggestion or reply will be appreciated.

Thanks.

答:
I used libpedsim

It provides a framework to set waypoints and obstacles:

```<code>Ped::Tscene*                    pedscene;
Ped::Twaypoint*                 w1;
Ped::Tobstacle*                 o;
</code>```

Waypoints were simply created by random sampling - we created a texture where red means it is blocked and otherwise the cell is free. The hacky implementation looked like this and worked pretty well:

```<code>// setup
pedscene = new Ped::Tscene(-200, -200, 400, 400);

loadMap();

srand(time(NULL));

int x;
int y;

for (int i = 0; i<agnentCount_/10; i++)
{
    Ped::Tagent *a = new Ped::Tagent();

    while(true)
    {
        x = rand() % 99;
        y = rand() % 99;

        if(texData[y * tex_->getWidth() + x][0]==255)
        {
            break;
        }

    }
    a->setPosition(
        x,
        -y,
        0.2);

    while(true)
    {
        x = std::rand() % 99;
        y = std::rand() % 99;

        if(texData[y * tex_->getWidth() + x][0]==255)
        {
            break;
        }
    }
    PathFind seeker(
        a->getPosition().x,
        -a->getPosition().y,
        x,
        y,
        tex_->getWidth(),
        tex_->getHeight(),
        texData);

    std::vector<buw::vector2i> path;
    path = seeker.findPath();

    for(std::vector<buw::vector2i>::iterator it = path.begin(); it!=path.end(); it++)
    {
        w1 = new Ped::Twaypoint( it->x(), -it->y(),  5);
        a->addWaypoint(w1);
    }

    pedscene->addAgent(a);
</code>```

Body:
I used libpedsim

It provides a framework to set waypoints and obstacles:

Ped::Tscene*                    pedscene;
Ped::Twaypoint*                 w1;
Ped::Tobstacle*                 o;

Waypoints were simply created by random sampling - we created a texture where red means it is blocked and otherwise the cell is free. The hacky implementation looked like this and worked pretty well:

// setup
pedscene = new Ped::Tscene(-200, -200, 400, 400);

loadMap();

srand(time(NULL));

int x;
int y;

for (int i = 0; i<agnentCount_/10; i++)
{
    Ped::Tagent *a = new Ped::Tagent();

    while(true)
    {
        x = rand() % 99;
        y = rand() % 99;

        if(texData[y * tex_->getWidth() + x][0]==255)
        {
            break;
        }

    }
    a->setPosition(
        x,
        -y,
        0.2);

    while(true)
    {
        x = std::rand() % 99;
        y = std::rand() % 99;

        if(texData[y * tex_->getWidth() + x][0]==255)
        {
            break;
        }
    }
    PathFind seeker(
        a->getPosition().x,
        -a->getPosition().y,
        x,
        y,
        tex_->getWidth(),
        tex_->getHeight(),
        texData);

    std::vector<buw::vector2i> path;
    path = seeker.findPath();

    for(std::vector<buw::vector2i>::iterator it = path.begin(); it!=path.end(); it++)
    {
        w1 = new Ped::Twaypoint( it->x(), -it->y(),  5);
        a->addWaypoint(w1);
    }

    pedscene->addAgent(a);


问:
Measuring OpenMP Fork/Join latency
Since MPI-3 comes with functionality for shared memory parallelism, and it seems to be perfectly matched for my application, I'm critically considering rewriting my hybrid OpemMP-MPI code into a pure MPI implementation.

In order to drive the last nail into the coffin, I decided to run a small program to test the latency of the OpenMP fork/join mechanism. Here's the code (written for Intel compiler):

```<code>void action1(std::vector<double>& t1, std::vector<double>& t2)
{
    #pragma omp parallel for schedule(static) num_threads(std::thread::hardware_concurrency())
    for (auto index = std::size_t{}; index < t1.size(); ++index)
    {
        t1.data()[index] = std::sin(t2.data()[index]) * std::cos(t2.data()[index]);
    }
}

void action2(std::vector<double>& t1, std::vector<double>& t2)
{
    #pragma omp parallel for schedule(static) num_threads(std::thread::hardware_concurrency())
    for (auto index = std::size_t{}; index < t1.size(); ++index)
    {
        t1.data()[index] = t2.data()[index] * std::sin(t2.data()[index]);
    }
}

void action3(std::vector<double>& t1, std::vector<double>& t2)
{
    #pragma omp parallel for schedule(static) num_threads(std::thread::hardware_concurrency())
    for (auto index = std::size_t{}; index < t1.size(); ++index)
    {
        t1.data()[index] = t2.data()[index] * t2.data()[index];
    }
}

void action4(std::vector<double>& t1, std::vector<double>& t2)
{
    #pragma omp parallel for schedule(static) num_threads(std::thread::hardware_concurrency())
    for (auto index = std::size_t{}; index < t1.size(); ++index)
    {
        t1.data()[index] = std::sqrt(t2.data()[index]);
    }
}

void action5(std::vector<double>& t1, std::vector<double>& t2)
{
    #pragma omp parallel for schedule(static) num_threads(std::thread::hardware_concurrency())
    for (auto index = std::size_t{}; index < t1.size(); ++index)
    {
        t1.data()[index] = t2.data()[index] * 2.0;
    }
}

void all_actions(std::vector<double>& t1, std::vector<double>& t2)
{
    #pragma omp parallel for schedule(static) num_threads(std::thread::hardware_concurrency())
    for (auto index = std::size_t{}; index < t1.size(); ++index)
    {
        t1.data()[index] = std::sin(t2.data()[index]) * std::cos(t2.data()[index]);
        t1.data()[index] = t2.data()[index] * std::sin(t2.data()[index]);
        t1.data()[index] = t2.data()[index] * t2.data()[index];
        t1.data()[index] = std::sqrt(t2.data()[index]);
        t1.data()[index] = t2.data()[index] * 2.0;
    }
}

int main()
{
    // decide the process parameters
    const auto n = std::size_t{8000000};
    const auto test_count = std::size_t{500};

    // garbage data...
    auto t1 = std::vector<double>(n);
    auto t2 = std::vector<double>(n);

    //+/////////////////
    // perform actions one after the other
    //+/////////////////

    const auto sp = timer::spot_timer();
    const auto dur1 = sp.duration_in_us();
    for (auto index = std::size_t{}; index < test_count; ++index)
    {
        #pragma noinline
        action1(t1, t2);
        #pragma noinline
        action2(t1, t2);
        #pragma noinline
        action3(t1, t2);
        #pragma noinline
        action4(t1, t2);
        #pragma noinline
        action5(t1, t2);
    }
    const auto dur2 = sp.duration_in_us();

    //+/////////////////
    // perform all actions at once
    //+/////////////////
    const auto dur3 = sp.duration_in_us();
    for (auto index = std::size_t{}; index < test_count; ++index)
    {
        #pragma noinline
        all_actions(t1, t2);
    }
    const auto dur4 = sp.duration_in_us();

    const auto a = dur2 - dur1;
    const auto b = dur4 - dur3;
    if (a < b)
    {
        throw std::logic_error("negative_latency_error");
    }
    const auto fork_join_latency = (a - b) / (test_count * 4);

    // report
    std::cout << "Ran the program with " << omp_get_max_threads() << ", the calculated fork/join latency is: " << fork_join_latency << " us" << std::endl;

    return 0;
}
</code>```

As you can see, the idea is to perform a set of actions separately (each within an OpenMP loop) and to calculate the average duration of this, and then to perform all these actions together (within the same OpenMP loop) and to calculate the average duration of that. Then we have a linear system of equations in two variables, one of which is the latency of the fork/join mechanism, which can be solved to obtain the value.

Questions:

Am I overlooking something?

Currently, I am using "-O0" to prevent smarty-pants compiler from doing its funny business. Which compiler optimizations should I use, would these also have an effect on the latency itself etc etc?

On my Coffee Lake processor with 6 cores, I measured a latency of ~850 us. Does this sound about right?

Edit 3

) I've included a warm-up calculation in the beginning upon @paleonix's suggestion,

) I've reduced the number of actions for simplicity, and,

) I've switched to 'omp_get_wtime' to make it universally understandable.

I am now running the following code with flag -O3:

```<code>void action1(std::vector<double>& t1)
{
    #pragma omp parallel for schedule(static) num_threads(std::thread::hardware_concurrency())
    for (auto index = std::size_t{}; index < t1.size(); ++index)
    {
        t1.data()[index] = std::sin(t1.data()[index]);
    }
}

void action2(std::vector<double>& t1)
{
    #pragma omp parallel for schedule(static) num_threads(std::thread::hardware_concurrency())
    for (auto index = std::size_t{}; index < t1.size(); ++index)
    {
        t1.data()[index] =  std::cos(t1.data()[index]);
    }
}

void action3(std::vector<double>& t1)
{
    #pragma omp parallel for schedule(static) num_threads(std::thread::hardware_concurrency())
    for (auto index = std::size_t{}; index < t1.size(); ++index)
    {
        t1.data()[index] = std::atan(t1.data()[index]);
    }
}

void all_actions(std::vector<double>& t1, std::vector<double>& t2, std::vector<double>& t3)
{
    #pragma omp parallel for schedule(static) num_threads(std::thread::hardware_concurrency())
    for (auto index = std::size_t{}; index < t1.size(); ++index)
    {
        #pragma optimize("", off)
        t1.data()[index] = std::sin(t1.data()[index]);
        t2.data()[index] = std::cos(t2.data()[index]);
        t3.data()[index] = std::atan(t3.data()[index]);
        #pragma optimize("", on)
    }
}

int main()
{
    // decide the process parameters
    const auto n = std::size_t{1500000}; // 12 MB (way too big for any cache)
    const auto experiment_count = std::size_t{1000};

    // garbage data...
    auto t1 = std::vector<double>(n);
    auto t2 = std::vector<double>(n);
    auto t3 = std::vector<double>(n);
    auto t4 = std::vector<double>(n);
    auto t5 = std::vector<double>(n);
    auto t6 = std::vector<double>(n);
    auto t7 = std::vector<double>(n);
    auto t8 = std::vector<double>(n);
    auto t9 = std::vector<double>(n);

    //+/////////////////
    // warum-up, initialization of threads etc.
    //+/////////////////
    for (auto index = std::size_t{}; index < experiment_count / 10; ++index)
    {
        all_actions(t1, t2, t3);
    }

    //+/////////////////
    // perform actions (part A)
    //+/////////////////

    const auto dur1 = omp_get_wtime();
    for (auto index = std::size_t{}; index < experiment_count; ++index)
    {
        action1(t4);
        action2(t5);
        action3(t6);
    }
    const auto dur2 = omp_get_wtime();

    //+/////////////////
    // perform all actions at once (part B)
    //+/////////////////

    const auto dur3 = omp_get_wtime();
    #pragma nofusion
    for (auto index = std::size_t{}; index < experiment_count; ++index)
    {
        all_actions(t7, t8, t9);
    }
    const auto dur4 = omp_get_wtime();

    const auto a = dur2 - dur1;
    const auto b = dur4 - dur3;
    const auto fork_join_latency = (a - b) / (experiment_count * 2);

    // report
    std::cout << "Ran the program with " << omp_get_max_threads() << ", the calculated fork/join latency is: "
        << fork_join_latency * 1E+6 << " us" << std::endl;

    return 0;
}
</code>```

With this, the measured latency is now 115 us. What's puzzling me now is that this value changes when the actions are changed. According to my logic, since I'm doing the same action in both parts A and B, there should actually be no change. Why is this happening?

答:
Here is my attempt at measuring fork-join overhead:

#include <iostream>
#include <string>

#include <omp.h>

constexpr int n_warmup = 10'000;
constexpr int n_measurement = 100'000;
constexpr int n_spins = 1'000;

void spin() {
    volatile bool flag = false;
    for (int i = 0; i < n_spins; ++i) {
        if (flag) {
            break;
        }
    }
}

void bench_fork_join(int num_threads) {
    omp_set_num_threads(num_threads);

    // create threads, warmup
    for (int i = 0; i < n_warmup; ++i) {
        #pragma omp parallel
        spin();
    }

    double const start = omp_get_wtime();
    for (int i = 0; i < n_measurement; ++i) {
        #pragma omp parallel
        spin();
    }
    double const stop = omp_get_wtime();
    double const ptime = (stop - start) * 1e6 / n_measurement;

    // warmup
    for (int i = 0; i < n_warmup; ++i) {
        spin();
    }
    double const sstart = omp_get_wtime();
    for (int i = 0; i < n_measurement; ++i) {
        spin();
    }
    double const sstop = omp_get_wtime();
    double const stime = (sstop - sstart) * 1e6 / n_measurement;

    std::cout << ptime << " us\t- " << stime << " us\t= " << ptime - stime << " us\n";
}

int main(int argc, char **argv) {
    auto const params = argc - 1;
    std::cout << "parallel\t- sequential\t= overhead\n";

    for (int j = 0; j < params; ++j) {
        auto num_threads = std::stoi(argv[1 + j]);
        std::cout << "---------------- num_threads = " << num_threads << " ----------------\n";
        bench_fork_join(num_threads);
    }

    return 0;
}
```
<p>You can call it with multiple different numbers of threads which should not be higher then the number of cores on your machine to give reasonable results. On my machine with 6 cores and compiling with gcc 11.2, I get</p>
```$ g++ -fopenmp -O3 -DNDEBUG -o bench-omp-fork-join bench-omp-fork-join.cpp
$ ./bench-omp-fork-join 6 4 2 1
parallel        - sequential    = overhead
---------------- num_threads = 6 ----------------
1.51439 us      - 0.273195 us   = 1.24119 us
---------------- num_threads = 4 ----------------
1.24683 us      - 0.276122 us   = 0.970708 us
---------------- num_threads = 2 ----------------
1.10637 us      - 0.270865 us   = 0.835501 us
---------------- num_threads = 1 ----------------
0.708679 us     - 0.269508 us   = 0.439171 us
```

In each line the first number is the average (over 100'000 iterations) with threads and the second number is the average without threads. The last number is the difference between the first two and should be an upper bound on the fork-join overhead.

Make sure that the numbers in the middle column (no threads) are approximately the same in every row, as they should be independent of the number of threads. If they aren't, make sure there is nothing else running on the computer and/or increase the number of measurements and/or warmup runs.

In regard to exchanging OpenMP for MPI, keep in mind that MPI is still multiprocessing and not multithreading. You might pay a lot of memory overhead because processes tend to be much bigger than threads.

EDIT:

Revised benchmark to use spinning on a volatile flag instead of sleeping (Thanks @Jérôme Richard). As Jérôme Richard mentioned in his answer, the measured overhead grows with n_spins. Setting n_spins below 1000 didn't significantly change the measurement for me, so that is where I measured. As one can see above, the measured overhead is way lower than what the earlier version of the benchmark measured.

The inaccuracy of sleeping is a problem especially because one will always measure the thread that sleeps the longest and therefore get a bias to longer times, even if sleep times themselves would be distributed symmetrically around the input time.

Title: Measuring OpenMP Fork/Join latency

Body:
Since MPI-3 comes with functionality for shared memory parallelism, and it seems to be perfectly matched for my application, I'm critically considering rewriting my hybrid OpemMP-MPI code into a pure MPI implementation.

In order to drive the last nail into the coffin, I decided to run a small program to test the latency of the OpenMP fork/join mechanism. Here's the code (written for Intel compiler):

void action1(std::vector<double>& t1, std::vector<double>& t2)
{
    #pragma omp parallel for schedule(static) num_threads(std::thread::hardware_concurrency())
    for (auto index = std::size_t{}; index < t1.size(); ++index)
    {
        t1.data()[index] = std::sin(t2.data()[index]) * std::cos(t2.data()[index]);
    }
}

void action2(std::vector<double>& t1, std::vector<double>& t2)
{
    #pragma omp parallel for schedule(static) num_threads(std::thread::hardware_concurrency())
    for (auto index = std::size_t{}; index < t1.size(); ++index)
    {
        t1.data()[index] = t2.data()[index] * std::sin(t2.data()[index]);
    }
}

void action3(std::vector<double>& t1, std::vector<double>& t2)
{
    #pragma omp parallel for schedule(static) num_threads(std::thread::hardware_concurrency())
    for (auto index = std::size_t{}; index < t1.size(); ++index)
    {
        t1.data()[index] = t2.data()[index] * t2.data()[index];
    }
}

void action4(std::vector<double>& t1, std::vector<double>& t2)
{
    #pragma omp parallel for schedule(static) num_threads(std::thread::hardware_concurrency())
    for (auto index = std::size_t{}; index < t1.size(); ++index)
    {
        t1.data()[index] = std::sqrt(t2.data()[index]);
    }
}

void action5(std::vector<double>& t1, std::vector<double>& t2)
{
    #pragma omp parallel for schedule(static) num_threads(std::thread::hardware_concurrency())
    for (auto index = std::size_t{}; index < t1.size(); ++index)
    {
        t1.data()[index] = t2.data()[index] * 2.0;
    }
}

void all_actions(std::vector<double>& t1, std::vector<double>& t2)
{
    #pragma omp parallel for schedule(static) num_threads(std::thread::hardware_concurrency())
    for (auto index = std::size_t{}; index < t1.size(); ++index)
    {
        t1.data()[index] = std::sin(t2.data()[index]) * std::cos(t2.data()[index]);
        t1.data()[index] = t2.data()[index] * std::sin(t2.data()[index]);
        t1.data()[index] = t2.data()[index] * t2.data()[index];
        t1.data()[index] = std::sqrt(t2.data()[index]);
        t1.data()[index] = t2.data()[index] * 2.0;
    }
}

int main()
{
    // decide the process parameters
    const auto n = std::size_t{8000000};
    const auto test_count = std::size_t{500};

    // garbage data...
    auto t1 = std::vector<double>(n);
    auto t2 = std::vector<double>(n);

    //+/////////////////
    // perform actions one after the other
    //+/////////////////

    const auto sp = timer::spot_timer();
    const auto dur1 = sp.duration_in_us();
    for (auto index = std::size_t{}; index < test_count; ++index)
    {
        #pragma noinline
        action1(t1, t2);
        #pragma noinline
        action2(t1, t2);
        #pragma noinline
        action3(t1, t2);
        #pragma noinline
        action4(t1, t2);
        #pragma noinline
        action5(t1, t2);
    }
    const auto dur2 = sp.duration_in_us();

    //+/////////////////
    // perform all actions at once
    //+/////////////////
    const auto dur3 = sp.duration_in_us();
    for (auto index = std::size_t{}; index < test_count; ++index)
    {
        #pragma noinline
        all_actions(t1, t2);
    }
    const auto dur4 = sp.duration_in_us();

    const auto a = dur2 - dur1;
    const auto b = dur4 - dur3;
    if (a < b)
    {
        throw std::logic_error("negative_latency_error");
    }
    const auto fork_join_latency = (a - b) / (test_count * 4);

    // report
    std::cout << "Ran the program with " << omp_get_max_threads() << ", the calculated fork/join latency is: " << fork_join_latency << " us" << std::endl;

    return 0;
}

As you can see, the idea is to perform a set of actions separately (each within an OpenMP loop) and to calculate the average duration of this, and then to perform all these actions together (within the same OpenMP loop) and to calculate the average duration of that. Then we have a linear system of equations in two variables, one of which is the latency of the fork/join mechanism, which can be solved to obtain the value.

Questions:

Am I overlooking something?

Currently, I am using "-O0" to prevent smarty-pants compiler from doing its funny business. Which compiler optimizations should I use, would these also have an effect on the latency itself etc etc?

On my Coffee Lake processor with 6 cores, I measured a latency of ~850 us. Does this sound about right?

Edit 3

) I've included a warm-up calculation in the beginning upon @paleonix's suggestion,

) I've reduced the number of actions for simplicity, and,

) I've switched to 'omp_get_wtime' to make it universally understandable.

I am now running the following code with flag -O3:

void action1(std::vector<double>& t1)
{
    #pragma omp parallel for schedule(static) num_threads(std::thread::hardware_concurrency())
    for (auto index = std::size_t{}; index < t1.size(); ++index)
    {
        t1.data()[index] = std::sin(t1.data()[index]);
    }
}

void action2(std::vector<double>& t1)
{
    #pragma omp parallel for schedule(static) num_threads(std::thread::hardware_concurrency())
    for (auto index = std::size_t{}; index < t1.size(); ++index)
    {
        t1.data()[index] =  std::cos(t1.data()[index]);
    }
}

void action3(std::vector<double>& t1)
{
    #pragma omp parallel for schedule(static) num_threads(std::thread::hardware_concurrency())
    for (auto index = std::size_t{}; index < t1.size(); ++index)
    {
        t1.data()[index] = std::atan(t1.data()[index]);
    }
}

void all_actions(std::vector<double>& t1, std::vector<double>& t2, std::vector<double>& t3)
{
    #pragma omp parallel for schedule(static) num_threads(std::thread::hardware_concurrency())
    for (auto index = std::size_t{}; index < t1.size(); ++index)
    {
        #pragma optimize("", off)
        t1.data()[index] = std::sin(t1.data()[index]);
        t2.data()[index] = std::cos(t2.data()[index]);
        t3.data()[index] = std::atan(t3.data()[index]);
        #pragma optimize("", on)
    }
}

int main()
{
    // decide the process parameters
    const auto n = std::size_t{1500000}; // 12 MB (way too big for any cache)
    const auto experiment_count = std::size_t{1000};

    // garbage data...
    auto t1 = std::vector<double>(n);
    auto t2 = std::vector<double>(n);
    auto t3 = std::vector<double>(n);
    auto t4 = std::vector<double>(n);
    auto t5 = std::vector<double>(n);
    auto t6 = std::vector<double>(n);
    auto t7 = std::vector<double>(n);
    auto t8 = std::vector<double>(n);
    auto t9 = std::vector<double>(n);

    //+/////////////////
    // warum-up, initialization of threads etc.
    //+/////////////////
    for (auto index = std::size_t{}; index < experiment_count / 10; ++index)
    {
        all_actions(t1, t2, t3);
    }

    //+/////////////////
    // perform actions (part A)
    //+/////////////////

    const auto dur1 = omp_get_wtime();
    for (auto index = std::size_t{}; index < experiment_count; ++index)
    {
        action1(t4);
        action2(t5);
        action3(t6);
    }
    const auto dur2 = omp_get_wtime();

    //+/////////////////
    // perform all actions at once (part B)
    //+/////////////////

    const auto dur3 = omp_get_wtime();
    #pragma nofusion
    for (auto index = std::size_t{}; index < experiment_count; ++index)
    {
        all_actions(t7, t8, t9);
    }
    const auto dur4 = omp_get_wtime();

    const auto a = dur2 - dur1;
    const auto b = dur4 - dur3;
    const auto fork_join_latency = (a - b) / (experiment_count * 2);

    // report
    std::cout << "Ran the program with " << omp_get_max_threads() << ", the calculated fork/join latency is: "
        << fork_join_latency * 1E+6 << " us" << std::endl;

    return 0;
}

With this, the measured latency is now 115 us. What's puzzling me now is that this value changes when the actions are changed. According to my logic, since I'm doing the same action in both parts A and B, there should actually be no change. Why is this happening?

Body:
Here is my attempt at measuring fork-join overhead:

#include <iostream>
#include <string>

#include <omp.h>

constexpr int n_warmup = 10'000;
constexpr int n_measurement = 100'000;
constexpr int n_spins = 1'000;

void spin() {
    volatile bool flag = false;
    for (int i = 0; i < n_spins; ++i) {
        if (flag) {
            break;
        }
    }
}

void bench_fork_join(int num_threads) {
    omp_set_num_threads(num_threads);

    // create threads, warmup
    for (int i = 0; i < n_warmup; ++i) {
        #pragma omp parallel
        spin();
    }

    double const start = omp_get_wtime();
    for (int i = 0; i < n_measurement; ++i) {
        #pragma omp parallel
        spin();
    }
    double const stop = omp_get_wtime();
    double const ptime = (stop - start) * 1e6 / n_measurement;

    // warmup
    for (int i = 0; i < n_warmup; ++i) {
        spin();
    }
    double const sstart = omp_get_wtime();
    for (int i = 0; i < n_measurement; ++i) {
        spin();
    }
    double const sstop = omp_get_wtime();
    double const stime = (sstop - sstart) * 1e6 / n_measurement;

    std::cout << ptime << " us\t- " << stime << " us\t= " << ptime - stime << " us\n";
}

int main(int argc, char **argv) {
    auto const params = argc - 1;
    std::cout << "parallel\t- sequential\t= overhead\n";

    for (int j = 0; j < params; ++j) {
        auto num_threads = std::stoi(argv[1 + j]);
        std::cout << "---------------- num_threads = " << num_threads << " ----------------\n";
        bench_fork_join(num_threads);
    }

    return 0;
}

You can call it with multiple different numbers of threads which should not be higher then the number of cores on your machine to give reasonable results. On my machine with 6 cores and compiling with gcc 11.2, I get

$ g++ -fopenmp -O3 -DNDEBUG -o bench-omp-fork-join bench-omp-fork-join.cpp
$ ./bench-omp-fork-join 6 4 2 1
parallel        - sequential    = overhead
---------------- num_threads = 6 ----------------
1.51439 us      - 0.273195 us   = 1.24119 us
---------------- num_threads = 4 ----------------
1.24683 us      - 0.276122 us   = 0.970708 us
---------------- num_threads = 2 ----------------
1.10637 us      - 0.270865 us   = 0.835501 us
---------------- num_threads = 1 ----------------
0.708679 us     - 0.269508 us   = 0.439171 us

In each line the first number is the average (over 100'000 iterations) with threads and the second number is the average without threads. The last number is the difference between the first two and should be an upper bound on the fork-join overhead.

Make sure that the numbers in the middle column (no threads) are approximately the same in every row, as they should be independent of the number of threads. If they aren't, make sure there is nothing else running on the computer and/or increase the number of measurements and/or warmup runs.

In regard to exchanging OpenMP for MPI, keep in mind that MPI is still multiprocessing and not multithreading. You might pay a lot of memory overhead because processes tend to be much bigger than threads.

EDIT:

Revised benchmark to use spinning on a volatile flag instead of sleeping (Thanks @Jérôme Richard). As Jérôme Richard mentioned in his answer, the measured overhead grows with n_spins. Setting n_spins below 1000 didn't significantly change the measurement for me, so that is where I measured. As one can see above, the measured overhead is way lower than what the earlier version of the benchmark measured.

The inaccuracy of sleeping is a problem especially because one will always measure the thread that sleeps the longest and therefore get a bias to longer times, even if sleep times themselves would be distributed symmetrically around the input time.


问:
Typescript error using styled-components
Having the following:

tsconfig

```<code>{
  "compilerOptions": {
    "sourceMap": true,
    "target": "es5",
    "jsx": "react",
    "module": "commonjs",
    "moduleResolution": "node",
    "experimentalDecorators": true,
    "declaration": true,
    "outDir": "lib",
    "lib": ["es2015","dom"]
    }
}
</code>```

Note declaration : true

And .tsx file:

```<code>import * as styledComponents from "styled-components";

export const backColor = (props: MyProps) => props.theme.backColor;

export const Title = styledComponents.div`
   text-align: center;
   background-color:${backColor};
   width: 100%;
`;
</code>```

When I run tsc on the terminal to compile I get:

error TS2339: Property 'div' does not exist on type 'typeof "/root/node_modules/styled-comp...'.

UPDATE

The error happens with both ways of importing styled-components

```<code>import styled from 'styled-components'
import * as styled from 'styled-components'
</code>```

答:
I experienced this error when I used VSCode to rename a div to Div (which was what I named my styled component).

I suspected my rename might have taken effect in some styled-components / React core files and the changes wouldn't show on git tracking since those are git ignored files. Hence, easily missed.

The solution for me was to delete node_modules/ and do npm install again.

Body:
Having the following:

tsconfig

{
  "compilerOptions": {
    "sourceMap": true,
    "target": "es5",
    "jsx": "react",
    "module": "commonjs",
    "moduleResolution": "node",
    "experimentalDecorators": true,
    "declaration": true,
    "outDir": "lib",
    "lib": ["es2015","dom"]
    }
}

Note declaration : true

And .tsx file:

import * as styledComponents from "styled-components";

export const backColor = (props: MyProps) => props.theme.backColor;

export const Title = styledComponents.div`
   text-align: center;
   background-color:${backColor};
   width: 100%;
`;

When I run tsc on the terminal to compile I get:

error TS2339: Property 'div' does not exist on type 'typeof "/root/node_modules/styled-comp...'.

UPDATE

The error happens with both ways of importing styled-components

import styled from 'styled-components'
import * as styled from 'styled-components'


问:
Get javascript object types as console.log outputs in node.js
I need to introspect some objects to know their type. In particular, I need to identify functions and async functions. For example consider the following object:

```<code>f = function(){}
af = async function(){}
class c {}
</code>```

Using typeof won't work because for any of these objects, the type is function

```<code>
> typeof f
'function'
> typeof af
'function'
> typeof c
'function'
</code>```

This improves a little if I use Object.prototype.toString.call() on each object:

```<code>> Object.prototype.toString.call(f)
'[object Function]'
>Object.prototype.toString.call(af)
'[object AsyncFunction]'
>Object.prototype.toString.call(c)
'[object Function]'
</code>```

I still, cannot differentiate a function from a class. However, console.log is able to do it:

```<code>> console.log(f)
[Function: f]
> console.log(af)
[AsyncFunction: af]
> console.log(c)
[class c]
</code>```

So my question is, how can I mimic what console.log is doing to identify the correct type of a class?

答:
Use the util module of Node.js, for instance the inspect function.

Note: You need to import it const util = require("util"). It's not a global object!

> class A {}
> util.inspect(A)
'[class A]'
> f = () => {}
> af = async () => {}
> util.inspect(f)
'[Function: f]'
> util.inspect(af)
'[AsyncFunction: af]'
```

Body:
I need to introspect some objects to know their type. In particular, I need to identify functions and async functions. For example consider the following object:

f = function(){}
af = async function(){}
class c {}

Using typeof won't work because for any of these objects, the type is function

> typeof f
'function'
> typeof af
'function'
> typeof c
'function'

This improves a little if I use Object.prototype.toString.call() on each object:

> Object.prototype.toString.call(f)
'[object Function]'
>Object.prototype.toString.call(af)
'[object AsyncFunction]'
>Object.prototype.toString.call(c)
'[object Function]'

I still, cannot differentiate a function from a class. However, console.log is able to do it:

> console.log(f)
[Function: f]
> console.log(af)
[AsyncFunction: af]
> console.log(c)
[class c]

So my question is, how can I mimic what console.log is doing to identify the correct type of a class?


问:
React Navigation 5, block back navigation after login
I am using React Navigation 5 in a project, and I'm having trouble trying to block a user from navigating back after a certain point.

The app uses a nested navigation structure similar to this:

```<code>ROOT (STACK)
|-- LoginScreens (STACK - options={{ gestureEnabled: false }} )
|   |-- Login (SCREEN) -> when successful navigate to "Home"
|   +-- Register (SCREEN) -> after registration, navigate to "Login"
|
+-- Home (TABS - options={{ gestureEnabled: false }} )
    |-- BlahBlah (SCREEN)
    |-- MyProfile (SCREEN)
    +-- Dashboard (TABS)
        |-- AllTasks (SCREEN)
        +-- SomethingElse (SCREEN)
</code>```

After a successful user login, the user is sent to the Home screen and should not be able to navigate back to the LoginScreens screen.

I have tried to use the componentDidMount lifecycle method on Home, as well as the useFocusEffect hook, with the following:

Placing a callback to React Native's BackHandler, returning true from the handler works (true means back action has been handled, no further back handlers will be called), but it will also block any back navigation within the screens in Home (e.g. I cannot navigate back from Dashboard to MyProfile).

Using navigation.reset({ index: 1, routes: [{ name: "Home" }] }). Without index: 1 the navigation just goes back to ROOT's initialRoute (in this case, LoginScreens). With index: 1, a Maximum update depth exceeded error is thrown.

Instead navigating directly to Home, I have tried using a navigation.reset() (note: no params, clears the entire navigation history), and after that navigate to the Home screen. This doesn't achieve the desired effect since the current route (ROOT's initialRoute, in this case: LoginScreens) is still pushed on the navigation history before navigating to Home.

Combining navigation and reset calls in different ways, I have only managed to get JS angry and throw errors and exceptions at me.

Aaaaand... I have ran out of ideas. Does anyone have any suggestions ?

答:
I like the accepted solution, but the other way to do this is to use React Context.

```<code>const AuthContext = React.createContext();

const setIsLoggedIn = (isLoggedIn) => {
    this.setState({ isLoggedIn });
}
</code>```

and then wrap your entire navigator:

```<code> <AuthContext.Provider value={setIsLoggedIn}>
     <RootStackNavigator>
        // your screens, etc.
     </RootStackNavigator>
 </AuthContext.Provider>
</code>```

then in your screen, you can use:

```<code> const { setIsLoggedIn } = React.useContext(AuthContext);
</code>```

and call it when you want.

See this guide: https://reactnavigation.org/docs/auth-flow/

Body:
I am using React Navigation 5 in a project, and I'm having trouble trying to block a user from navigating back after a certain point.

The app uses a nested navigation structure similar to this:

ROOT (STACK)
|-- LoginScreens (STACK - options={{ gestureEnabled: false }} )
|   |-- Login (SCREEN) -> when successful navigate to "Home"
|   +-- Register (SCREEN) -> after registration, navigate to "Login"
|
+-- Home (TABS - options={{ gestureEnabled: false }} )
    |-- BlahBlah (SCREEN)
    |-- MyProfile (SCREEN)
    +-- Dashboard (TABS)
        |-- AllTasks (SCREEN)
        +-- SomethingElse (SCREEN)

After a successful user login, the user is sent to the Home screen and should not be able to navigate back to the LoginScreens screen.

I have tried to use the componentDidMount lifecycle method on Home, as well as the useFocusEffect hook, with the following:

Placing a callback to React Native's BackHandler, returning true from the handler works (true means back action has been handled, no further back handlers will be called), but it will also block any back navigation within the screens in Home (e.g. I cannot navigate back from Dashboard to MyProfile).

Using navigation.reset({ index: 1, routes: [{ name: "Home" }] }). Without index: 1 the navigation just goes back to ROOT's initialRoute (in this case, LoginScreens). With index: 1, a Maximum update depth exceeded error is thrown.

Instead navigating directly to Home, I have tried using a navigation.reset() (note: no params, clears the entire navigation history), and after that navigate to the Home screen. This doesn't achieve the desired effect since the current route (ROOT's initialRoute, in this case: LoginScreens) is still pushed on the navigation history before navigating to Home.

Combining navigation and reset calls in different ways, I have only managed to get JS angry and throw errors and exceptions at me.

Aaaaand... I have ran out of ideas. Does anyone have any suggestions ?

Body:
I like the accepted solution, but the other way to do this is to use React Context.

const AuthContext = React.createContext();

const setIsLoggedIn = (isLoggedIn) => {
    this.setState({ isLoggedIn });
}

and then wrap your entire navigator:

 <AuthContext.Provider value={setIsLoggedIn}>
     <RootStackNavigator>
        // your screens, etc.
     </RootStackNavigator>
 </AuthContext.Provider>

then in your screen, you can use:

 const { setIsLoggedIn } = React.useContext(AuthContext);

and call it when you want.

See this guide: https://reactnavigation.org/docs/auth-flow/


问:
How compare array of dates with an date string to find count of same days in JavaScript?
I tried this method to check duplicate date in the array with date string array, but didn't work Please anyone can help..

    const dateArray = ["2000-07-13","03/24/2000", "June 7 2021"]
    const compaingDate =new Date("2000-06-13")
    let countOfDays=0
    for(let sameDate of dateArray){
        if(compaingDate.getDate()===sameDate.getDate()){
            countOfDays+=1
        }
    }
    console.log(countOfDays);```

答:
You never convert the array of strings into an array of dates:</p>
```<code>    const dateArray = ["2000-07-13","03/24/2000", "June 7 2021", "June 13 2006"]
    const compaingDate =new Date("2000-06-13")
    let countOfDays=0
    for(let sameDate of dateArray){
    sameDate = new Date(sameDate)
        if(compaingDate.getDate()===sameDate.getDate()){
            countOfDays+=1
        }
    }
    console.log(countOfDays);

</code>```

Body:
You never convert the array of strings into an array of dates:

    const dateArray = ["2000-07-13","03/24/2000", "June 7 2021", "June 13 2006"]
    const compaingDate =new Date("2000-06-13")
    let countOfDays=0
    for(let sameDate of dateArray){
    sameDate = new Date(sameDate)
        if(compaingDate.getDate()===sameDate.getDate()){
            countOfDays+=1
        }
    }
    console.log(countOfDays);


问:
Skip parent if using PHP
Not sure if this is even possible, but the basic idea of the code is as follows:

```<code>if(isset($_POST['submit'])) {
    if($_POST['field'] == 0) {
        // do not process the code in the if statement
    }
    // code to process if the above validation criteria is not met
}
</code>```

I basically want to try and keep it as simple as possible without lots of if's everywhere, just the quick validation statements at the top, and if none of those if statements are triggered, it will process the code to update the database etc.

I have tried the continue statement function with no joy, I believe that only works with loops.

Thanks!

答:
```<code>if(isset($_POST['submit']) && $_POST['field'] != 0) {

}
</code>```

As a function

```<code>function testCondition() {
   return isset($_POST['submit']) &&
          $_POST['field'] != 0;
}

if (testCondition()) { ... }
</code>```

Title: Skip parent if using PHP

Body:
Not sure if this is even possible, but the basic idea of the code is as follows:

if(isset($_POST['submit'])) {
    if($_POST['field'] == 0) {
        // do not process the code in the if statement
    }
    // code to process if the above validation criteria is not met
}

I basically want to try and keep it as simple as possible without lots of if's everywhere, just the quick validation statements at the top, and if none of those if statements are triggered, it will process the code to update the database etc.

I have tried the continue statement function with no joy, I believe that only works with loops.

Thanks!

Body:
if(isset($_POST['submit']) && $_POST['field'] != 0) {

}

As a function

function testCondition() {
   return isset($_POST['submit']) &&
          $_POST['field'] != 0;
}

if (testCondition()) { ... }


问:
After get request, data isn't showing in my screen
In this code data successfully adding to my ''items'' list.
But In the second code, when I call my ''items'' list, It looks empty. What should I do for displaying data?

```<code>import 'dart:convert';

import 'package:http/http.dart' as http;

class IceCream {
  final String id;
  final String? title;
  final String? shortTitle;
  final String? description;
  final String? longDescription;
  final double price;
  final String imageUrl;
  final double? kilos;
  final double? review;
  final int? reviewCount;

  IceCream(
      {required this.id,
      this.title,
      this.shortTitle,
      this.description,
      this.longDescription,
      required this.price,
      required this.imageUrl,
      this.kilos,
      this.review,
      this.reviewCount});
}

class Products {
  List<IceCream> _items = [];

  List<IceCream> get items {
    return [..._items];
  }

  Future<void> fetchProducts() async {
    final url = Uri.parse(
        'https://ice-cream-app-31dce-default-rtdb.europe-west1.firebasedatabase.app/icecreams.json');

    try {
      final response = await http.get(url);
      //    print(response.body);
      final extractedData = json.decode(response.body) as Map<String, dynamic>;
      if (extractedData.length == null) {
        return;
      }
      final List<IceCream> loadedProduct = [];
      extractedData.forEach((prodId, prodData) {
        loadedProduct.add(IceCream(
            id: prodId,
            title: prodData['title'],
            description: prodData['description'],
            price: prodData['price'],
            imageUrl: prodData['imageUrl'],
            shortTitle: prodData['shortTitle'],
            longDescription: prodData['longDescription'],
            kilos: prodData['kilos'],
            review: prodData['review'],
            reviewCount: prodData['reviewCount']));
      });

      _items = loadedProduct;
      //print(items);
    } catch (error) {
      print(error);
      throw (error);
    }
  }
}

</code>```

Second code:

```<code>import 'package:flutter/material.dart';
import 'package:ice_cream_ui_clone/model/ice_cream.dart';
import 'package:ice_cream_ui_clone/screens/detail_screen.dart';

import '../utils/constants.dart';
import 'add_item_button.dart';

class TopFlavoursList extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final cart = Products().items;

    return Expanded(
      flex: 2,
      child: ListView.builder(
          itemCount: cart.length,
          scrollDirection: Axis.horizontal,
          itemBuilder: (ctx, i) => GestureDetector(
                onTap: () {
                  Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (ctx) => DetailScreen(
                                item: cart[i],
                              )));
                },
                child: Card(
                  color: myLightPinkColor,
                  child: Padding(
                      padding: EdgeInsets.all(lowPadding),
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: [
                          Image.network(
                            cart[i].imageUrl,
                            height: midImageHeight,
                            width: midImageWidht,
                          ),
                          SingleChildScrollView(
                            child: Column(
                              children: [
                                Text(cart[i].title!,
                                    style:
                                        Theme.of(context).textTheme.headline5),
                                SizedBox(
                                  height: lowPadding,
                                ),
                                Row(
                                  children: [
                                    Text("${cart[i].kilos.toString()} KG",
                                        style: Theme.of(context)
                                            .textTheme
                                            .headline6),
                                    SizedBox(
                                      width: lowImageWidht,
                                    ),
                                    Icon(
                                      Icons.star,
                                      color: myYellowColor,
                                    ),
                                    Text(cart[i].review.toString(),
                                        style: Theme.of(context)
                                            .textTheme
                                            .headline6)
                                  ],
                                ),
                                Row(
                                  children: [
                                    Text("\$${cart[i].price.toString()}",
                                        style: Theme.of(context)
                                            .textTheme
                                            .headline5),
                                    SizedBox(
                                      width: lowImageWidht,
                                    ),
                                    AddItemButton(
                                      buttonShape: const CircleBorder(),
                                    )
                                  ],
                                )
                              ],
                            ),
                          )
                        ],
                      )),
                ),
              )),
    );
  }
}

</code>```

答:
I think the issue is with the "forEach" statment. Should be something more like

```<code>extractedData.forEach((data) {
  loadedProduct.add(IceCream())
});
</code>```

The function in the forEach should take one argument not 2

Body:
In this code data successfully adding to my ''items'' list.
But In the second code, when I call my ''items'' list, It looks empty. What should I do for displaying data?

import 'dart:convert';

import 'package:http/http.dart' as http;

class IceCream {
  final String id;
  final String? title;
  final String? shortTitle;
  final String? description;
  final String? longDescription;
  final double price;
  final String imageUrl;
  final double? kilos;
  final double? review;
  final int? reviewCount;

  IceCream(
      {required this.id,
      this.title,
      this.shortTitle,
      this.description,
      this.longDescription,
      required this.price,
      required this.imageUrl,
      this.kilos,
      this.review,
      this.reviewCount});
}

class Products {
  List<IceCream> _items = [];

  List<IceCream> get items {
    return [..._items];
  }

  Future<void> fetchProducts() async {
    final url = Uri.parse(
        'https://ice-cream-app-31dce-default-rtdb.europe-west1.firebasedatabase.app/icecreams.json');

    try {
      final response = await http.get(url);
      //    print(response.body);
      final extractedData = json.decode(response.body) as Map<String, dynamic>;
      if (extractedData.length == null) {
        return;
      }
      final List<IceCream> loadedProduct = [];
      extractedData.forEach((prodId, prodData) {
        loadedProduct.add(IceCream(
            id: prodId,
            title: prodData['title'],
            description: prodData['description'],
            price: prodData['price'],
            imageUrl: prodData['imageUrl'],
            shortTitle: prodData['shortTitle'],
            longDescription: prodData['longDescription'],
            kilos: prodData['kilos'],
            review: prodData['review'],
            reviewCount: prodData['reviewCount']));
      });

      _items = loadedProduct;
      //print(items);
    } catch (error) {
      print(error);
      throw (error);
    }
  }
}

Second code:

import 'package:flutter/material.dart';
import 'package:ice_cream_ui_clone/model/ice_cream.dart';
import 'package:ice_cream_ui_clone/screens/detail_screen.dart';

import '../utils/constants.dart';
import 'add_item_button.dart';

class TopFlavoursList extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final cart = Products().items;

    return Expanded(
      flex: 2,
      child: ListView.builder(
          itemCount: cart.length,
          scrollDirection: Axis.horizontal,
          itemBuilder: (ctx, i) => GestureDetector(
                onTap: () {
                  Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (ctx) => DetailScreen(
                                item: cart[i],
                              )));
                },
                child: Card(
                  color: myLightPinkColor,
                  child: Padding(
                      padding: EdgeInsets.all(lowPadding),
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                        children: [
                          Image.network(
                            cart[i].imageUrl,
                            height: midImageHeight,
                            width: midImageWidht,
                          ),
                          SingleChildScrollView(
                            child: Column(
                              children: [
                                Text(cart[i].title!,
                                    style:
                                        Theme.of(context).textTheme.headline5),
                                SizedBox(
                                  height: lowPadding,
                                ),
                                Row(
                                  children: [
                                    Text("${cart[i].kilos.toString()} KG",
                                        style: Theme.of(context)
                                            .textTheme
                                            .headline6),
                                    SizedBox(
                                      width: lowImageWidht,
                                    ),
                                    Icon(
                                      Icons.star,
                                      color: myYellowColor,
                                    ),
                                    Text(cart[i].review.toString(),
                                        style: Theme.of(context)
                                            .textTheme
                                            .headline6)
                                  ],
                                ),
                                Row(
                                  children: [
                                    Text("\$${cart[i].price.toString()}",
                                        style: Theme.of(context)
                                            .textTheme
                                            .headline5),
                                    SizedBox(
                                      width: lowImageWidht,
                                    ),
                                    AddItemButton(
                                      buttonShape: const CircleBorder(),
                                    )
                                  ],
                                )
                              ],
                            ),
                          )
                        ],
                      )),
                ),
              )),
    );
  }
}

Body:
I think the issue is with the "forEach" statment. Should be something more like

extractedData.forEach((data) {
  loadedProduct.add(IceCream())
});

The function in the forEach should take one argument not 2


问:
How to downgrade .net maui from 11 to 5 preview?
I used prism for .net maui and in current .net maui 11 preview its not really working and got reports from here https://github.com/dansiegel/Prism.Maui/issues/11 that it was working in 9th version, so how am i supposed to rollback to previous version?</p>

答:
MAUI is installed alongside with Visual Studio as a workload. AFAIK there's no way to  downgrade a workload or install any version of it other than latest.

Some docs: https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-workload-install

Best course of action for you is to like that issue prism's repo so it will be noticeable, so they will update their NuGet to work with latest MAUI preview faster.

Also you can manually rebuild prism's the NuGet (that's what I usually do), and create a PR if some changes are needed to build it with latest MAUI preview.

But beware that MAUI is still in preview, and breaking changes are expected with each update (so NuGet might break again). There will be preview 13 and 14, and only after them we may see a GA release (after which no breaking changes will be made).


问:
Reveal sidebar in Atom (editor)?
I have a simple question. I've installed Atom and the package nuclide-commons, but when I open a folder, I don't see any sidebar with the list of files in Atom. I'm trying to find a way/shortcut to show it, but I failed. It's even not mentioned in
the main menu...

Any hint? Thanks.

答:
On Windows:
Toggle Tree View [CtrK CtrB]

Or Ctr + Shift + P:
Search: Toggle Tree View

Title: Reveal sidebar in Atom (editor)?


问:
Difference between "$i" and :$("$i") in Julia DataFrame
When i=1, both println(typeof("$i")) and println(typeof(:($("$i")))) give String, and both println("$i") and println(:($("$i"))) give 1. However, the following code

```<code>using DataFrames
df = DataFrame()
i = 1
df.:($("$i")) = [0.0]
println(df)
</code>```

works, but the following code

```<code>using DataFrames
df = DataFrame()
i = 1
df."$i" = [0.0]
println(df)
</code>```

does not work. What is wrong with the latter code?

答:
I would not recommend you to rely on the either syntax. These are corner cases of Julia parser.

The fact that:

```<code>df.:($("$i")) = [0.0]
</code>```

works is most likely not a conscious design decision on Julia parser side. Note that:

```<code>df.:($("$i"))
</code>```

fails.

As it is explained in https://dataframes.juliadata.org/stable/man/getting_started/:

Note that in the forms df."col" and :var"col" variable interpolation into a string using $ does not work.

If you want to use interpolation either directly call setproperty! and getproperty or use getindex/setindex!, in this case:

```<code>df[!, "$i"] = [0.0]
</code>```

Body:
When i=1, both println(typeof("$i")) and println(typeof(:($("$i")))) give String, and both println("$i") and println(:($("$i"))) give 1. However, the following code

using DataFrames
df = DataFrame()
i = 1
df.:($("$i")) = [0.0]
println(df)

works, but the following code

using DataFrames
df = DataFrame()
i = 1
df."$i" = [0.0]
println(df)

does not work. What is wrong with the latter code?

Body:
I would not recommend you to rely on the either syntax. These are corner cases of Julia parser.

The fact that:

df.:($("$i")) = [0.0]

works is most likely not a conscious design decision on Julia parser side. Note that:

df.:($("$i"))

fails.

As it is explained in https://dataframes.juliadata.org/stable/man/getting_started/:

Note that in the forms df."col" and :var"col" variable interpolation into a string using $ does not work.

If you want to use interpolation either directly call setproperty! and getproperty or use getindex/setindex!, in this case:

df[!, "$i"] = [0.0]


问:
Find path between nodes in flat array
I Have a flat array of connections which represent the connection of nodes in a tree.

const connections = [
  {
    id: "first",
    source: "root",
    target: "A_fbb03",
  },
  {
    source: "A_fbb03",
    target: "W_c0f6f",
    id: "A_fbb03_W_c0f6f",
  },
  {
    source: "A_fbb03",
    target: "W_4c2dd",
    id: "A_fbb03_W_4c2dd",
  },
  {
    id: "A_fbb03_W_1f0ac",
    source: "A_fbb03",
    target: "W_1f0ac",
  },
  {
    id: "W_c0f6f_S_007f5",
    source: "W_c0f6f",
    target: "S_007f5",
  },
];
```
<p>When adding a new connection between nodes, I want to check if this connection creating a loop, meaning that the target node has a path to the source node.</p>
<p>I came up with a function:</p>
<pre class="lang-js prettyprint-override"><code>function isLoopConnection = (newConnection, connections) => {
  const theSource = newConnection.source
  let theTarget = newConnection.target
  let isLoop = false

  for (let i = 0; i < connections.length; i++) {
    const nextConnection = connections.filter(item => item.source === theTarget)
    if (nextConnection.length) {
      for (const nc of nextConnection) {
        if (nc.target === theSource) {
          isLoop = true
          break
        } else {
          theTarget = nc.target
        }
      }
    }
  }
  return isLoop
}
</code>```

Obviously this is not working well in certain cases and feels way too complicated.
For example: when I add a new connection which suppose to create a loop between S_007f5
to A_fbb03 because A_fbb03 has a path to S_007f5 through W_c0f6f

```<code>A_fbb03 -> W_c0f6f -> S_007f5 -> A_fbb03
</code>```

{
  id: "S_007f5_A_fbb03",
  source: "S_007f5",
  target: "A_fbb03",
}
```

The function return false

Any suggestions how to improve this solution?

答:
You could take a Set and store seen node identifier.

This approach generates an object whit all children of all nodes. To get result, all items are used to check for circular references.

const
    hasLoop = array => {
        const
           children = array.reduce((r, { source, target }) => ((r[source] ??= []).push(target), r), {}),
           check = (node, seen = new Set) => {
               if (seen.has(node)) return true;
               seen.add(node);
               return (children[node] || []).some(node => check(node, seen));
           };

        return array.some(({ source }) => check(source));
    },
    connections = [{ source: "root", target: "A_fbb03" }, { source: "A_fbb03", target: "W_c0f6f" }, { source: "A_fbb03", target: "W_4c2dd" }, { source: "A_fbb03", target: "W_1f0ac" }, { source: "W_c0f6f", target: "S_007f5" }, { source: "S_007f5", target: "A_fbb03" }];

console.log(hasLoop(connections.slice(0, -1))); // false
console.log(hasLoop(connections));              //  true```

Title: Find path between nodes in flat array

Body:
I Have a flat array of connections which represent the connection of nodes in a tree.

const connections = [
  {
    id: "first",
    source: "root",
    target: "A_fbb03",
  },
  {
    source: "A_fbb03",
    target: "W_c0f6f",
    id: "A_fbb03_W_c0f6f",
  },
  {
    source: "A_fbb03",
    target: "W_4c2dd",
    id: "A_fbb03_W_4c2dd",
  },
  {
    id: "A_fbb03_W_1f0ac",
    source: "A_fbb03",
    target: "W_1f0ac",
  },
  {
    id: "W_c0f6f_S_007f5",
    source: "W_c0f6f",
    target: "S_007f5",
  },
];

When adding a new connection between nodes, I want to check if this connection creating a loop, meaning that the target node has a path to the source node.

I came up with a function:

function isLoopConnection = (newConnection, connections) => {
  const theSource = newConnection.source
  let theTarget = newConnection.target
  let isLoop = false

  for (let i = 0; i < connections.length; i++) {
    const nextConnection = connections.filter(item => item.source === theTarget)
    if (nextConnection.length) {
      for (const nc of nextConnection) {
        if (nc.target === theSource) {
          isLoop = true
          break
        } else {
          theTarget = nc.target
        }
      }
    }
  }
  return isLoop
}

Obviously this is not working well in certain cases and feels way too complicated.
For example: when I add a new connection which suppose to create a loop between S_007f5
to A_fbb03 because A_fbb03 has a path to S_007f5 through W_c0f6f

A_fbb03 -> W_c0f6f -> S_007f5 -> A_fbb03

{
  id: "S_007f5_A_fbb03",
  source: "S_007f5",
  target: "A_fbb03",
}

The function return false

Any suggestions how to improve this solution?


问:
spark dataframe drop duplicates and keep first
Question: in pandas when dropping duplicates you can specify which columns to keep. Is there an equivalent in Spark Dataframes?

Pandas:

```<code>df.sort_values('actual_datetime', ascending=False).drop_duplicates(subset=['scheduled_datetime', 'flt_flightnumber'], keep='first')
</code>```

Spark dataframe (I use Spark 1.6.0) doesn't have the keep option

```<code>df.orderBy(['actual_datetime']).dropDuplicates(subset=['scheduled_datetime', 'flt_flightnumber'])
</code>```

Imagine scheduled_datetime and flt_flightnumber are columns 6 ,17. By creating keys based on the values of these columns we can also deduplicate

```<code>def get_key(x):
    return "{0}{1}".format(x[6],x[17])

df= df.map(lambda x: (get_key(x),x)).reduceByKey(lambda x,y: (x))
</code>```

but how to specify to keep the first row and get rid of the other duplicates ? What about the last row ?

答:
Use window and row_number functions.

Order by ascending or descending to select first or last.

```<code>from pyspark.sql import Window
from pyspark.sql import functions as f

window = Window.partitionBy("col1").orderBy("datestr").asc()
df = (df.withColumn('row', f.row_number().over(window))\
.filter(col('row') == 1)
.drop('row')
.show())
</code>```

Body:
Question: in pandas when dropping duplicates you can specify which columns to keep. Is there an equivalent in Spark Dataframes?

Pandas:

df.sort_values('actual_datetime', ascending=False).drop_duplicates(subset=['scheduled_datetime', 'flt_flightnumber'], keep='first')

Spark dataframe (I use Spark 1.6.0) doesn't have the keep option

df.orderBy(['actual_datetime']).dropDuplicates(subset=['scheduled_datetime', 'flt_flightnumber'])

Imagine scheduled_datetime and flt_flightnumber are columns 6 ,17. By creating keys based on the values of these columns we can also deduplicate

def get_key(x):
    return "{0}{1}".format(x[6],x[17])

df= df.map(lambda x: (get_key(x),x)).reduceByKey(lambda x,y: (x))

but how to specify to keep the first row and get rid of the other duplicates ? What about the last row ?

Body:
Use window and row_number functions.

Order by ascending or descending to select first or last.

from pyspark.sql import Window
from pyspark.sql import functions as f

window = Window.partitionBy("col1").orderBy("datestr").asc()
df = (df.withColumn('row', f.row_number().over(window))\
.filter(col('row') == 1)
.drop('row')
.show())


问:
Django How to Look up querydict in template?
when i look inside querydict it with {{get_copy}} it shows

```<code><QueryDict: {'grub_id__in': ['Yelek', 'Tunik', 'Tulum', 'Tshirt']}>
</code>```

as i know that how to look up querydict

```<code>{%for key,val in get_copy.items%}
{{key}}{{val}}
{%endfor%}
</code>```

but it shows only one of val value

output:

```<code>grub_id__in Tshirt
</code>```

答: Add space befor/after "%":  {%_ .... _%}</p>

Body:
when i look inside querydict it with {{get_copy}} it shows

<QueryDict: {'grub_id__in': ['Yelek', 'Tunik', 'Tulum', 'Tshirt']}>

as i know that how to look up querydict

{%for key,val in get_copy.items%}
{{key}}{{val}}
{%endfor%}

but it shows only one of val value

output:

grub_id__in Tshirt

Body: Add space befor/after "%":  {%_ .... _%}