Carrierwave multiple image upload

Question: I'm using multiple file uploads from the master branch and PostgreSQL
My product model hast a string field called "images" and I can attach multiple images just fine.
What I can't figure out though,  how can I remove one image from the products?
I can remove all images as described in the docs:
product.remove_images!
product.save

but didn't find a way how to remove a single image.
Comment: Do you have `images` or `image` as an attribute name?
Comment: "multiple file uploads from the master branch" - is there a gem in addition to carrierwave, or what do you mean by that?
Comment: check https://github.com/carrierwaveuploader/carrierwave#multiple-file-uploads
where it says: Note: You must specify using the master branch to enable this feature:
Comment: @Pavan images is the attribute:
t.string  "images",  array: true
Answer: Have you considered using a nested form for to try to delete an image?
Here is a piece of code from carrierwave's github site...
<%= form_for @product, html: { multipart: true } do |f| %>
  .
  .
  .

      <%= f.check_box :remove_images %>
      Remove images

<% end %>

...which I am sure you have seen. Although, of course, calling remove_images! would not work in your case, since that implies a unified action on all images.
Try the following modification of the above and see if it helps you sort this problem and target each image individually...
<%= nested_form_for @product, :html=>{ :multipart => true } do |f| %>
  .
  .
  .
  <%= f.fields_for :images do |product_form|  %>

    <%= product_form.link_to_remove "Remove this image" %>
  <% end %>
<% end %>

To make this work make sure to include gem 'nested_form', '~> 0.3.2' in your Gemfile.
Hopefully this helps you out.
Comment: there is only one attribute, where should the nested form be used? if you read the readme on the master branch: https://github.com/carrierwaveuploader/carrierwave#multiple-file-uploads
I havea single column which stores an array.

your solution would work if I had a separate model for the images, which I don't.

I started to look at the mount_multiple_spec.rb from carrierwave and I'm starting to think that there is now way.
Answer: I learn from @Cris and use the following code snippet when working with S3.
def remove_image_at_index(index)
  remain_images = @product.images # copy the array
  deleted_image = remain_images.delete_at(index) # delete the target image
  deleted_image.try(:remove!) # delete image from S3
  @product.images = remain_images # re-assign back
end

I wrote a blog post about this , and create a sample project with more concrete codes.
Answer: You should be able to use remove_image on the specified image (from the array). So you'll need to somehow identify the image first (ex. images[0].remove_image!)
Answer: adding something like this to the product model does the trick.
  def delete_image(idx)
    remaining_images = []
    self.images.each_with_index { |img,ii| remaining_images<<File.open(img.path) unless idx == ii}
    self.images=remaining_images
  end

Carrierwave also takes care of removing the file after saving the record.

Carrierwave multiple image upload


Sum of binomial coefficients with index divisible by 4

Question: I am trying to obtain a closed form expression for
$\sum {n \choose 4k}$
I am trying to use the binomial expansion of $(1 + i)^n$ and $(1 - i)^n$.
$$(1 + i)^n + (1 - i)^n = 2\left(\sum {n \choose 4k} -\sum {n \choose 4k + 2}\right)$$ Stuck at this now. I can't come up with a way to simplify the second term on RHS. Any help would be appreciated.
Comment: Brute-force on the small N and find this sequence on the OEIS?
Comment: https://oeis.org/search?q=1%2C2%2C6%2C16%2C36%2C72%2C136%2C256%2C496%2C992%2C2016%2C4096%2C8256%2C16512%2C32896%2C&language=english&go=Search  - your sequence
Answer: We have the followings :
$$(1+1)^n=\binom n0+\binom n1+\binom n2+\binom n3+\binom n4+\binom n5+\cdots$$
$$(1-1)^n=\binom n0-\binom n1+\binom n2-\binom n3+\binom n4-\binom n5+\cdots$$
$$(1+i)^n=\binom n0+\binom n1i-\binom n2-\binom n3i+\binom n4+\binom n5i-\cdots$$
$$(1-i)^n=\binom n0-\binom n1i-\binom n2+\binom n3i+\binom n4-\binom n5i-\cdots$$
Adding these gives
$$(1+1)^n+(1-1)^n+(1+i)^n+(1-i)^n=4\left(\binom n0+\binom n4+\binom n8+\cdots\right)$$
I think that you can continue from here.
Comment: I came up with a slightly different way to approach this. But your way of visualizing is helpful as well.
Answer: After posting this question, it struck me that
RHS =  $4\sum {n \choose 4k} -2\sum {n \choose 4k} -2\sum {n \choose 4k + 2}) = 4\sum {n \choose 4k} - 2 \sum {n \choose 2k} = 4\sum {n \choose 4k} - 2 * 2^{n - 1} = 4\sum {n \choose 4k} - 2^{n} \implies \sum {n \choose 4k} = \frac{(1 + i)^n + (1 - i)^n + 2^n}{4}$
Answer: $\newcommand{\bbx}[1]{\,\bbox[15px,border:1px groove navy]{\displaystyle{#1}}\,}
 \newcommand{\braces}[1]{\left\lbrace\,{#1}\,\right\rbrace}
 \newcommand{\bracks}[1]{\left\lbrack\,{#1}\,\right\rbrack}
 \newcommand{\dd}{\mathrm{d}}
 \newcommand{\ds}[1]{\displaystyle{#1}}
 \newcommand{\expo}[1]{\,\mathrm{e}^{#1}\,}
 \newcommand{\ic}{\mathrm{i}}
 \newcommand{\mc}[1]{\mathcal{#1}}
 \newcommand{\mrm}[1]{\mathrm{#1}}
 \newcommand{\pars}[1]{\left(\,{#1}\,\right)}
 \newcommand{\partiald}[3][]{\frac{\partial^{#1} #2}{\partial #3^{#1}}}
 \newcommand{\root}[2][]{\,\sqrt[#1]{\,{#2}\,}\,}
 \newcommand{\totald}[3][]{\frac{\mathrm{d}^{#1} #2}{\mathrm{d} #3^{#1}}}
 \newcommand{\verts}[1]{\left\vert\,{#1}\,\right\vert}$
\begin{align}
\left.\sum_{k = 0}^{\infty}{n \choose 4k}
\,\right\vert_{\ n\ \in\ \mathbb{N}_{\large n\ \geq\ 0}} & =
\sum_{k = 0}^{\infty}{n \choose k}{1^{k} + \pars{-1}^{k} + \ic^{k} + \pars{-\ic}^{k} \over 4}
\\[5mm] & =
{1 \over 4}\sum_{k = 0}^{\infty}{n \choose k}\pars{1}^{k} +
{1 \over 4}\sum_{k = 0}^{\infty}{n \choose k}\pars{-1}^{k} +
{1 \over 2}\,\Re\sum_{k = 0}^{\infty}{n \choose k}\ic^{k}
\\[5mm] & =
{1 \over 4}\pars{1 + 1}^{n} + {1 \over 4}\bracks{1 + \pars{-1}}^{n} +
{1 \over 2}\,\Re\pars{1 + \ic}^{n}
\\[5mm] & =
2^{n - 2} + {1 \over 4}\,\delta_{n0} +
{1 \over 2}\,\Re\bracks{\root{1^{2} + 1^{2}}\expo{\ic\arctan\pars{1/1}}}^{n}
\\[5mm] & =
2^{n - 2} + {1 \over 4}\,\delta_{n0} +
{1 \over 2}\,\Re\bracks{2^{1/2}\expo{\ic\pi/4}}^{n}
\\[5mm] & =
\bbx{2^{n - 2} + {1 \over 4}\,\delta_{n0} +
2^{n/2 - 1}\cos\pars{n\pi \over 4}}
\end{align}

Note tat it yields the correct value when $\ds{n = 0}$.
Comment: @YiFan $\displaystyle\texttt{Kronecker Delta}$: $\displaystyle \delta_{n0} = 1$ if $\displaystyle n = 0$ and $\displaystyle = 0$ otherwise.
Comment: What is $\delta$ here?


Matrix dot product in gaussian multivariate distribution

Question: I am having a bit of trouble understanding how the matrix multiplication is carried out in the exponent term of the multivariate gaussian distribution.
I am going to call the covariance matrix C.
In the exponent term for the multivariate gaussian, it looks like -1/2  (x-u)^T  C^-1 (x-u)
Lets assume we have x be a 600x2 dataset (2 features), and the covariance matrix be a 2x2 matrix
In that case, C^-1 would be a 2x2 matrix, (x-u) would be a 600x2 matrix, thus (x-u)^T would be a 2x600 matrix.
In the matrix product order, (x-u)^T * C^-1 is dotting a 2x600 matrix to a 2x2 matrix, which does not match the dimensions. Similarly, if first doing C^-1 * (x-u)^T, this dots a 2x2 with a 600x2, which also is unfeasible dimensionally.  Thus, is the proper way to always compute this to:
First compute (x-u)^T (dot) (x-u) , and then dot  that  with C^-1 , with C^-1 on the right?
Or am I looking past an important detail?
Comment: [Wikipedia](https://en.wikipedia.org/wiki/Multivariate_normal_distribution#Density_function) is very clear that it considers $x$ to be a "k-dimensional column vector."  You seem to be using "x" both for the model matrix "$X$" and for such individual observations $x$.  Case matters!  Doesn't that settle the issue?
Comment: @whuber absolutely, it seems like my mistake was 100% how i interpreted the x matrix!
Answer: Usually the equation you've written is given as $$(x - \mu) C^{-1} (x - \mu)^T,$$ with the transpose on the second $(x-\mu)$ term. Written in this way, $x, \mu$ are assumed to be row vectors. As you've written it, $x$ and $\mu$ must be column vectors. Let us call their dimension (the number of features) $m$. If we have $n$ such observations, then the matrix form of $x$ is $$X=\begin{pmatrix} | & & | \\ x_1 & \cdots & x_n \\ | & & |\end{pmatrix}\in \mathbb R^{m \,\times\, n}$$
So, just transpose your matrix $(600, 2) \rightarrow (2, 600)$ and things work out.
The convention given in the first equation is more common because if $x$ is a row vector, then the design matrix of $n$ observations $X$ has dim $(n, m)$, which is how textbooks usually write it.
Comment: thank you for taking the time to help me out. I would like to point out that every literature I have found on the multivariate gaussian uses the formula that I originally posted, from wikipedia, to stanford.edu links, etc. However, the math naturally makes much more sense if I could move around the transpose, I am assuming that my issue is the way my training data is structured (in my case, it is a matrix with each row representing a data point, consisting of 2 features (so two columns) )
Comment: That's fine-- we apparently use a different set of texts. Either way, my original answer still stands. Transpose your X matrix so that the feature vectors are stacked column-wise, and it works out.


Is it possible to use Harmony (ES6) with JSXTransformer.js?

Question: I've had great luck using React's JSXTransformer.js to develop using JSX in the browser:

To reduce boilerplate, I'd like to use some of the features from Harmony, e.g. arrow functions. Facebook's JSX Compiler Service has a harmony checkbox which transforms ES6 to more traditional JS:
var f = v => this.props[v];
// becomes var f = function(v) { return this.props[v]; }.bind(this);

Is it possible to enable this transformation with the in-browser JSX?
Answer: This feature was added in React v0.11. Instead of type="text/jsx", you set type="text/jsx;harmony=true". For example:

Comment: Note: as of React v0.12, the `@jsx` pragma [is unnecessary](http://facebook.github.io/react/blog/2014/10/16/react-v0.12-rc1.html#the-jsx-pragma-is-gone).
Comment: what is "@jsx pragma"


Songs that move between 6/8 and 4/4?

Question: I am trying to do this for something I've written (4/4 in verse, 6/8 in chorus) but it's difficult to get in and out of smoothly, so I'm wondering how other people have tackled this.
For my song, if you were counting along, the capitalized beats are chord changes, and there is no tempo change in the counts of eighth notes from one section to the next:
Verse (4/4): ONE two three FOUR five six seven eight
Chorus (6/8): ONE two three FOUR five six
Any thoughts on my specific situation or any songs as said that have tackled this type of key change effectively without alienating too many listeners would be appreciated.
Doing time signature changes without losing the listener can be challenging.
Comment: In the 4/4 should it be ONE two three four FIVE six seven eight?
Comment: "The late great Johnny Ace" by Paul Simon is in 4/4, except for the bridges. The transition is extremely rough. "Lucy in the sky with diamonds" has verses in 3/4, chorus in 4/4. I've never figured out what happens in The Band's "Jawbone" - some of the time it's in 4/4, some of the time it's in 6/8.
Comment: I can't think of anything mainstream, but I wrote and recorded an instrumental that switches between these two time signatures. If it helps in any way, feel free to check it out:  http://www.tomweissmusic.com/timetravel/
Comment: Some more songs: "49 bye byes" - Crosby, Stills and Nash; "Lover's lullaby" - Janis Ian. The first starts in 3/4 then moves seamlessly to 4/4, whereas the second starts in 4/4 and moves to 3/4.
Comment: One way to make it easier to sight-read would be to write 4/4 (=8/8) at the start of the 4/4 section. This makes it obvious that the "counting unit" is the eighth note. Otherwise, some people would play the 6/8 section as though it were 2/4 with triplets.
Answer: No examples but some thoughts:

Make sure there is a reason for the change, and not just because you want to. I mean, there should be something happening in the music and/or the lyrics that helps communicate what you are trying to say that means the verse must be 4/4 and the chorus must be 6/8. Creating contrast is effective, so if you know why you are creating this metrical contrast, that will help you make it happen effectively.
Take your 4/4 tempo and apply it to the 6/8 measures on the dotted quarter note. For example, if you've got 60 quarter notes per minute in 4/4, then you want to have 60 dotted quarter notes per minute at 6/8. Making the tempo change along with the meter change will do a lot to smooth out the meter change.
Trying writing a change from 4/4 to 2/4 instead and then see if you feel the need for triplets in the 2/4 section. If the 2/4 section is mostly triplet feel, then you should change it to 6/8 as a compound march (with the tempo adjustment as above). If 2/4 works for the chorus without a lot of triplets, then you really want a 2/4 chorus and not 6/8 at all.
Comment: Just to clarify your 2nd point: if 60bpm is 4/4, then would the 6/8 still be 60bpm (a beat meaning dotted quarter)? If so, would there actually *be* a tempo change? I think not, making the transition pretty smooth.
Comment: @Tim It depends on whether you see that as a tempo change or not. If you're using Musescore, then you will want to explicitly mark that tempo change because it considers quarter note = 60 bpm to apply in the 6/8 measure as well (as opposed to a "beat" running at 60 bpm) and the music will seem to speed up to 90 bpm. So you have to put in a dotted quarter = 60 bpm "tempo change" to keep it smooth. If you're not using software, then whatever makes it clear to the player is all that is required, I assume.
Answer: No time sig. change is going to be smooth. The piece is going along at whatever pace, the listener is tapping along and suddenly there's a change, which throws it all out of kilter. Even going from 4/4 to 3/4 or 5/4 will sound like there's been a mistake until a few bars have elapsed and the listener settled again.
It's one of the ploys used by some composers to add some unrest into a piece.
Going from 4/4 into 6/8 using the pulse as you suggest can be problematic, as the 'beats' in 4/4 are crotchets, while (in your case) the 6/8 'beats' are quavers. So a tempo change too, as well as a change of feel. Although here, you've addressed it by counting the 4/4 as 8/8.
Another way to go would be thinking of 6/8 as two 'beats', each having three triplets. Still a tempo (and feel) change, but at least another option.
Comment: "*No time sig. change is going to be smooth*" False. Just last week I wrote the exact time signature change the asker wants to write and you wouldn't even know it happened. Basically, the melody we want to use for the chorus would be almost entirely triplets in 4/4 time so we changed to 6/8 (there's also a march feel happening there but we could have faked that in 4/4).
Comment: @ToddWilcox - interesting. That 4/4 triplets could have been 12/8 - often written in 4/4 to be played with a swing feel. I do a song in similar vein, and the guitar player swears it speeds up for the swing part. It doesn't, it simply changes feel. I hope I understood what you say.
Comment: Sorry, I might have been unclear - we didn't want triplets at all in the 4/4 section. If we had kept it in 4/4, then the section that became 6/8 would have had a lot of triplets. We were writing to accompany lyrics that we pre-written and the lyrics had an "in-threes" flow to them. We also made a tempo change at the same time as the meter change as mentioned in my answer, which helped keep the music flowing smoothly through the change.
Comment: Smooth is a choice by the composer. Stravinsky used this to create great unease in The Rite of Spring. Conversely King Crimson and many progressive rock bands change time signature with abandon and it sounds smooth as butter. Time signature changes can absolutely be smooth--if the melody carries it through.
Answer: "We Can Work It Out" by The Beatles is a pretty famous song with a 4/4 to 6/8 transition (the eighth note stays constant). Despite the time change, it's been an incredibly popular tune.
"All You Need Is Love" is another Beatles song in odd meter. Depending on your counting preference the verse is in 7/4 or alternating bars of 4/4 and 3/4.
Answer: If you want the transition to be smooth, the eighth note needs to stay the same length. I wrote a piece that went from 2/4 to 6/8 in alternating measures. The beats are a little longer in the 6/8 to accommodate the third eighth notes. (If I had combined the rhythm into one measure based on a quarter note, it would have been in 5/4 time but someone I consulted about the piece preferred reading 2/4 then 6/8.) A triplet is not the same thing because it would cause the 8th notes to be a bit smaller to fit all three into one beat.
Answer: Opeth - Windowpane
The first few parts switch between 6/8 and 4/4. They seem to have introduced two solutions for your problem:

The pulse of parts is matched (as suggested in other answers),
The 4/4 part consists of 6 bars, so if you count beats 1..6 through whole song you should consistently end up on beat 1 when ANY bar begins.

Side note: the off-beat accent in 4/4 seam to ease the transition to me. Again, it stays consistent with 6/8's accents
Here is a link to a drum score for the piece:
https://goo.gl/q6OhXx
Answer: Is the song a cappella or accompanied? If it's accompanied, the instruments can help in the pre-chorus (or end of verse), by establishing the 6/8 metre before the singers get to sing in it. Given the way the 4/4 is divided, you would need at least 2 6/8 bars there, in order to help the singers. (If the singers were still thinking in 4/4, a single 6/8 bar wouldn't get them to come in on the ONE because they'd think of that as the "seven" of a 4/4 bar.)
Answer: Breaking Benjamin- Psycho
Verses and choruses are in 6/8 but there's a part after the intro that gets repeated in places that's in 4/4

Songs that move between 6/8 and 4/4?


Topic Modeling: How many documents "belong" to a topic?

Question: A frequent question that arises when I present results of topic modeling to novices is: "How many documents belong to topic x?".
As topic modeling is probabilistic, I hesitate giving absolute frequencies as an answer. But "mean document-topic-probability" is nothing people unexperienced in topic modeling can deal with.
How would you determine the number of documents per topic? Using a cut-off in document-topic-probability?
Comment: Since I had some situations where a clear number of documents per topic was wished (e.g., share of female authors per topic), I ended up with the following approach:
**Select all docs with document-topic-probability > .5**
Although I agree with @haitao-du, doing it the Bayesian way (mean document-topic-probability of women vs. men) is sometimes hard to communicate. So selecting docs that _mainly_ address the topic might be a workaround for those situations, of course accompanied by stressing the resulting loss of information.
Answer: I think this question related to membership assignment and have little thing to do with topic modeling.
Assign a membership based on probability is a question frequently asked. See here as an example.
To maximize the chance of correctly guessing the result of a coin flip, should I always choose the most probable outcome?
We can assign the membership based on Maximum a posteriori estimation or others.
Think about two examples:

Suppose there are only $2$ topics, we can easily set the threshold to be $0.5$.
Suppose there are $100$ topics, and the probability distribution for a given document is $[0.1, 0.9/99, \cdots, 0.9/99]$. How do we assign the membership? If we assign it to be topic 1 (which is the MAP estimation) but we will get 90% wrong !

So, the answer is just use the probability (Bayesian way), and do not use a threshold to assign membership.


How to create an "All" link in a taxonomy menu (block)?

Question: I have a taxonomy vocabulary and terms as follow:
Food:
  -Meat
  -Veggies
  -Fruit
  -Dairy
I want to create a menu of taxonomy terms that look like this:
-All
-Meat
-Veggies
-Fruit
-Dairy
... when you are on the "Food" page.
I have tried the Taxonomy Menu and Taxonomy Menu Block modules, but none give me the option to have an "All" link. That's a bit daft innit?
How can you show the user posts/nodes of all terms?
Any other solutions?
Answer: My opinion:
You can create a new term called all and add it to all your related nodes.
Or,
You may use Views to create a page to show all nodes, and add that to your menu.


Discrepancy Between Theoretical and Empirical Coefficient of Variation in Log-Normal Distribution

Question: I'm exploring the properties of log-normal distributions and came across a formula (e.g., here) stating that the coefficient of variation (CV) of a variable $X$ following a log-normal distribution is $\sqrt{\exp(\sigma^2) - 1}$, where $\sigma$ is the SD of $\log X$. However, in my simulation, the empirical CVs calculated don't align well with this theoretical expectation.
Here's the R code for my simulation:
<code>nsim <- 1000            # Number of simulation iterations
nobs <- 100             # Number of observations per iteration
true_mu <- 4            # True mean of the normal distribution
true_sigma <- 2         # True SD
cv_obs <- numeric(nsim)
set.seed(1)
for (i in 1:nsim) {
  # Randomly draw 100 values from a normal distribution with the mean of 4 and the SD of 2, and then exponentiate it to turn it into a variable following a log-normal distribution.
  x <- exp(rnorm(nobs, mean = true_mu, sd = true_sigma))
  # Calculate the CV of the variable x that follows a log-normal distribution.
  cv_obs[i] <- sd(x)/mean(x)
}

# Expected value of the CV
cv_expected <- sqrt(exp(true_sigma^2) - 1)
# Proportion of the empirical CVs that exceed the expected value.
mean(cv_obs > cv_expected) # 0.007
# --> Only 0.7% of the empirical CVs exceed the expected value

# Figure below
hist(cv_obs)
abline(v = cv_expected, col = "red")
</code>

The expected CV, based on the formula, appears towards the edge of the empirical distribution of the CVs from my simulation. What might be causing this discrepancy? What am I missing?
Comment: For $\sigma=2$ the log-normal is extremely skewed and when large values in the right tail is missing in the sample, the sample sd will be strongly underestimated, more so than the sample mean.  Hence, the sample CV also underestimate the theoretical CV.  If you either increase the sample size or decrease $\sigma$, things work more as expected.
Comment: Thank you for your intuitive explanation. By increasing sample size and decreasing $\sigma$, empirical CVs indeed approached the expected CV.
Answer: You might be missing that the sample sd is limited by $\sqrt{n}$ times the sample mean when the values are non-negative; this occurs when there's a single extreme outlier.
Consider, for example, $n-1$ zero-values and one larger value (say at 1, for simplicity, since it doesn't matter how large it is).
Your $n$ is only 100, so you won't see a sample CV above 10.  If you try sample sizes of $9$ instead, you'll see your CV stays below $3$ and so on.
In short, sample CV (as an estimator of population CV) is biased downward for distributions on the positive half-line with heavy right tails. As you increase the sample size the impact of this bound is lowered, but it still has an effect compared to the infinite population.
Comment: Some references on the sample size-related bounds of the coefficient of variation are given within https://journals.sagepub.com/doi/pdf/10.1177/1536867X1001000311
Comment: @Akira The distribution you're looking at is quite skewed (the mean is at the 84th percentile for example and the tail is pretty heavy), so you come close to that upper bound of 10 every so often
Comment: Incidentally a plot of the sample's CV against the sample mean for a sufficiently large number of samples of size 100 very clearly hints at the existence of a bound; say, 30000 or 100000; you can use fewer samples if you try a smaller sample size or a larger value for $\sigma$. I'd probably use a log-log scale but it works fine without as well.
Comment: Thank you for your response. I was indeed unaware of the relationship between sample size and the bias in SD estimates when the variable only takes non-negative values.
Comment: @NickCox Thanks, much appreciated. A small warning for readers of the excellent article, because it would be easy to miss if you're just looking for the CV results --  carefully note that the "$s$" being discussed in the article there is $s_n$, the square root of the $n$-denominator version of variance. The $\sqrt{n-1}$ bound for CV given in Nick's article is completely consistent with my $\sqrt{n}$, since I was using the ordinary Bessel-corrected s.d. It makes complete sense to use $s_n$ for many of the bounds being considered.


How to convert a Scheme function that returns a closure to an equivalent Common Lisp function?

Question: I am converting some Scheme code to Common Lisp. I don't know Scheme. I know a bit of Common Lisp.
I think that I understand this Scheme code:
(define (make-cell)
    (let ((local-name '()))
       (define (local-add-name name)
           (set! local-name name))
        (define (me message)
            (cond ((eq? message 'add-name) local-add-name)
                  ((eq? message 'name) local-name)))
     me))

Using that function, I can make two cells:
(define a (make-cell))
(define b (make-cell))

Then I can store a name inside each cell:
((a 'add-name) 'a)
((b 'add-name) 'b)

Then I can retrieve the name stored inside each cell:
(a 'name)

=> a
(b 'name)

=> b
The a-cell has stored inside it the name "a". The b-cell has stored inside it the name "b". I can query the a-cell for its name and it returns "a". I can query the b-cell for its name and it returns "b".
Am I understanding correctly thus far?
Now I would like to achieve the same functionality using Common Lisp. Here's the make-cell function that I created:
(defun make-cell ()
    (let ((local-name nil))
        (defun local-add-name (name)
            (setf local-name name))
        (defun me (message)
            (cond ((eq message 'add-name) #'local-add-name)
                  ((eq message 'name) local-name)))))

Apparently that is wrong, since it doesn't give the desired behavior, as I show next.
I make an a-cell and b-cell:
(setf a (make-cell))
(setf b (make-cell))

I store a name inside each cell:
(funcall (funcall a 'add-name) 'a)
(funcall (funcall b 'add-name) 'b)

When I retrieve the names, both cells return the same name:
(funcall a 'name)

=> b
(funcall b 'name)

=> b
Ouch!
Why do both cells return the same name? What am I doing wrong? How do I get the CL code to behave the same way as the Scheme code?
Answer: Common Lisp does not have internal define like Scheme does (which is, by the way, syntactic sugar for letrec or letrec*†). The Common Lisp equivalent of letrec is labels, so you can use this:
(defun make-cell ()
  (let (local-name)
    (labels ((local-add-name (name)
               (setf local-name name))
             (me (message)
               (ecase message
                 (add-name #'local-add-name)
                 (name local-name))))
      #'me)))

This works as you'd expect (as tested on SBCL):
* (defvar *foo* (make-cell))

*FOO*
* (defvar *bar* (make-cell))

*BAR*
* (funcall (funcall *foo* 'add-name) "foo")

"foo"
* (funcall (funcall *bar* 'add-name) "bar")

"bar"
* (funcall *foo* 'name)

"foo"
* (funcall *bar* 'name)

"bar"

† Here's the letrec version of your code:
(define (make-cell)
  (let ((local-name #f))
    (letrec ((local-add-name (lambda (name)
                               (set! local-name name)))
             (me (lambda (message)
                   (case message
                     ((add-name) local-add-name)
                     ((name) local-name)))))
      me)))

or even:
(define (make-cell)
  (letrec ((local-name #f)
           (local-add-name (lambda (name)
                             (set! local-name name)))
           (me (lambda (message)
                 (case message
                   ((add-name) local-add-name)
                   ((name) local-name)))))
    me))

Answer: Chris Jester-Young already gave a good answer, but alternatively you could restructure your function a little bit. Notice that neither of the functions call ME by name; you could just replace that with a LAMBDA:
(defun make-cell ()
  (let (local-name)
    (flet ((add-name (name) (setf local-name name)))
      (lambda (message)
        (ecase message
          ;; ADD-NAME is only used here, so you could make it a lambda too.
          (add-name #'add-name)
          (name local-name))))))

If you later need to be able to refer to ME by name, Alexandria provides a macro NAMED-LAMBDA that should work.
Of course, this looks suspiciously like implementing objects with closures. Since Common Lisp is a multi-paradigm language, it would be better to just use CLOS here:
(defclass cell ()
  ((local-name :initform "" :initarg :name :reader name :writer add-name)))

(let ((cell (make-instance 'cell)))
  (format t "~&Name: ~a~%Add name: ~a~%Name: ~a~%"
          (name cell)
          (add-name "foo" cell)
          (name cell)))
; Name:
; Add name: foo
; Name: foo

Comment: Thank you very much @jkiiski!
Answer: The other answers give the correct solution to your problem, here I will try to explain why your solution does not produce the intended result.
In Common Lisp there is the distinction between the global environment and the lexical environments.
The global environment (see the manual) is usually modified evaluating top-level forms, like defvar,  defun, defmacro, etc., for instance in REPL, and contains  bindings that have an indefinite extent and scope.
A lexical environment instead is an environment that contains information valid in a certain form (i.e. it is a "local" environment), so that in each point of a program we can talk about of the current lexical environment. It is typically modified by bindings of function parameters or local definitions, like let.
Just for completness: this two kinds of environment are also similar to those available in other languages, but in Common Lisp there is another special kind of environment, the dynamic environment, which however is not of interest in this case.
So, while defun is usually found at top-level, you can give it inside the some other form, but, in any case, it:

Defines a new function named function-name in the global environment. ... Evaluating defun causes function-name to be a global name for the function specified by the lambda expression ... processed in the lexical environment in which defun was executed.

(emphasis is mine). See the manual.
In other words, both the functions that you have defined share the same variable
local-name. The situation is more clear if you think that a (defun ...) form
internal to a function definition can be "moved" to the top level, by moving together with it the definition related to the variables free in it. And since neither make-cell makes explicit use of the variables, you can transform your original definition in the following equivalent one:
(let (local-name)
  (defun local-add-name (name)
    (setf local-name name))
  (defun me (message)
     (cond ((eq message 'add-name) #'local-add-name)
           ((eq message 'name) local-name)))
  (defun make-cell ()
    #'me))

This definition should make clear that the functions share exactly the same variable local-name. And note that this kind of construct, called "let over lambda" can be very useful if, for instance, you have to share a value between two functions, like in:
(let ((id 0))
  (defun get-new-id ()
    (incf id))
  (defun reset-id ()
    (setf id 0)))

(get-new-id) ; => 1

(get-new-id) ; => 2

(reset-id)   ; => 0

(get-new-id) ; => 1

On the other hand, the label and flet special operators modifies the current lexical environment, and are "executed" each time the enclosing function is called. So, each time the make-cell function is called, a new lexical environment is generated containing the new variable local-name, and the two local functions that refer to that variable, and the value returned is the closure of the second function over the new variable.
Comment: True, but I don't believe in that thing. My impression is that Lisp is more maintainable, when things are named objects (-> top-level symbols) with documentation, etc.  and when more complex things are self-identifying objects (-> CLOS). I want to look into an IDE and see it, find it, inspect it. anonymous closures are nice in theory, but for practical maintenance other features are more valuable than closures with little information.
Comment: @RainerJoswig: ok, but even if unexported, such variable could be visible to the package, defying the idea of "hidden state". Certainly you could define a package only for the two functions, but it would be no so convenient. Ok, let's say that IMHO one could tolerate such form of DEFUN, seeing it as an abbreviation to define a sort of "mini-package", just for convenience (and I love the idea that Common Lisp was designed with lot of "conveniency" features :)
Comment: IMHO 'DEFUNS' inside other constructs is a code smell. DEFUN really is supposed to be a top-level function. Common Lisp is not 'secretly Scheme with bad syntax' (Kent Pitman). ;-)
Comment: @RainerJoswig, I agree that it can confuse people. But then, how one could code the last example without DEFUN? I can imagine only complex, not so elegant solutions.
Comment: Renzo: I would not use that at all. For example by embedding them in a LET, it prevents a compiler to recognize these functions as global functions at compile-time. I would just make \*%ID\* an unexported global variable.


coffee script - use parameter vairable in json data

Question: I have a coffee script function
postData: (pattern) ->
  myData =
    a: "a string"
    pattern: 0
    f: false

It is evaluated as
{
  "a": "a string",
  "pattern": 0,
  "f": false
}

It is not what I want. I would like pattern to be a string I passed in
Thanks for any help.
Answer: postData: (pattern) ->
  myData =
    a: "a string"
    f: false
  myData[pattern] = 0
  return myData

See it working here.


Is the Inverse Mills Ratio Strictly Decreasing?

Question: As far as I know, the Inverse Mills ratio, $\lambda(x)=\phi(x)/\Phi(x)$, is decreasing in $x$. Thus, I am curious now whether $\lambda(x)$ is in fact strictly decreasing in $x$.
To see this, I derived the derivative of the inverse Mills ratio:
\begin{align*}\lambda(x)&=\phi(x)/\Phi(x) \\[2pt]
 \Rightarrow \lambda'(x)&=\frac{\phi'(x)\Phi(x)-\phi(x)^2}{\Phi(x)^2} \\[2pt]
&=-x\lambda(x)-\lambda(x)^2\quad\because \phi(x)'=-x\phi(x)\end{align*}
Here, I have no idea how to show whether $\lambda(x)<0$ or $\lambda(x)\leq0$.
Thus, is the inverse Mills ratio in fact "strictly" decreasing in $x$?
Comment: This ratio is named for John P. Mills, who wrote about it. Punctuation allowed is a tricky small issue even for people with English as their first language, but strictly Mill's is wrong but Mills ratio would be fine.
Answer: Theorem $1.$ (Sampford) If $\lambda(x):=\frac{\varphi(x)}{\Phi(x)},$ then $\lambda^\prime(x)\in(-1,0).$
$\frac{e^{-\frac{1}{2} z^2}}{\int^{x}_{-\infty}e^{-\frac{1}{2} z^2}\, \mathrm dz}$  is nothing but the density of a truncated normal with support $z\in (-\infty,x]$. Its variance is $1-x\lambda(x) -\lambda^2(x). $ This implies $\lambda^\prime(x) >-1.$ And by definition, $\lambda(x) > 0; ~x+\lambda(x)> 0 $ for $x\geq 0.$ This is also true if $x< 0.$ The result follows.
$\blacksquare$

Reference:
$\rm [I]$ Some Inequalities on Mill's Ratio and Related Functions, M. R. Sampford, The Annals of Mathematical Statistics, Vol. $24, $ No. $1$ (Mar., $1953$), pp. $130-132.$
Comment: (+1) Helpful, but tiny detail: the Sampford paper was wrong in calling this the Mill's ratio. The original paper was Mills, J.P.  1927, Table of the ratio: area to bounding ordinate, for any portion of normal curve. _Biometrika_ 18: 395–400
Comment: This doesn't really matter in the grand scheme of things, but correct still beats incorrect otherwise!
Comment: I see. Thanks for pointing this out @NickCox. I should have looked into your meta [post](https://stats.meta.stackexchange.com/a/2814/362671).


How to dispatch value onChange

Question: I'm building a search component and I've set up my actions and reducers and so on... but I can't figure out how to dispatch an event in my component. What should be inside the onChangeValue attribute?
Here's the code:
import React from "react";
import { connect } from "react-redux";
import { getListOfUsers, clearDetails } from "../../actions/actions";
import SearchBar from "../../components/search/search";

const SearchBarContainer = onChangeValue => {
  return <SearchBar onChangeValue={onChangeValue} id="search" icon="search" />;
};

const mapStateToProps = state => {
  return {};
};

const mapDispatchToProps = dispatch => {
  return {
    onChangeValue: e => {
      dispatch(getListOfUsers(e.target.value));
      dispatch(clearDetails());
    }
  };
};

const connected = connect(
  mapStateToProps,
  mapDispatchToProps
)(SearchBarContainer);

export default connected;

Comment: Instead of `onChangeValue` use `this.props.onChangeValue` within `SearchBarContainer`
Answer: SearchBarContainer is a functional component and hence it won't have state or this variable. You need to get them from the props. Also dispatch function onChangeValue is available as a prop to the container.
const SearchBarContainer = ({ onChangeValue, value }) => {
  return (
    <SearchBar
      onChangeValue={onChangeValue}
      value={value}
      id="search"
      icon="search"
    />
  );
};

Comment: Thanks, worked perfectly. Can't understand how I missed that out, tired I guess :D
Answer: change
<SearchBar
      onChangeValue={onChangeValue}
      value={this.state.value}
      id="search"
      icon="search"
    />

to
<SearchBar
          onChangeValue={this.props.onChangeValue}
          value={this.state.value}
          id="search"
          icon="search"
        />

How to dispatch value onChange


How return back to same fragment class after complete action in android

Question: I developing an android app.I used fragmentation In my app ,I want to make a call using implicit intent,for making  call I create a separate activity class and in this activity I write code for make Call.after call end I want to return back to my fragment class,but I unable to return back to my fragment class.After call end it return to my home fragment class instead of calling fragment.Could you tell me what I did wrong in the code.Your help is really appreciated.
Here is programming flow
Cardeatils -> Caller
After call complete
Caller->Home.
but I want return like this way Caller->Cardetails.
In Cardetails. Code for Calling Activity
public class CarDetails extends BaseFragment implements OnClickListener
{
   //
    private void Makecall()
    {
        // TODO Auto-generated method stub
        Intent i=new Intent(mActivity,Caller.class);
        startActivityForResult(i, 1);
    }

}

and implement this method in CarDetails
@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        // TODO Auto-generated method stub
        if(requestCode==1)
        {

            if(resultCode==RESULT_OK)
            {
                //
            }
            if(resultCode==RESULT_CANCEL)
            {

            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

Code for make call
public class Caller extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        // call contact app using intent
        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "XXXXXX"));
        setResult(RESULT_CANCELED, intent);
        startActivity(intent);

    }

}

Comment: simply finish() it after work done.
Answer: Try like this:
Fragment f = new myFragment();
                FragmentTransaction t = getFragmentManager().beginTransaction();
                t.replace(R.id.frame_container,f);
                t.addToBackStack(null);
                t.commit();

Comment: thanks for answer. could you tell me details where to use this code
Comment: use instead of your intent to call your previous class
Comment: i used this code instead of intent but not working it also returning to home instead of CarDetails please help me


ScrollSpy with script added ids

Question: First of all I'm sorry if I repeat anything that has been posted elsewhere. I've been looking into solutions other people have found for their problems and I've implemented (or so I think) the things they found but the site I'm working on still doesn't do what it's supposed to.
So, the site I'm working on is http://www.vinck-interieur.be/?lang=en.
What's supposed to happen is that when scrolling down or up, visitors see where they are in the sidebar menu. ScrollSpy seemed the best option to do this but it hasn't been working since I've put it in my code.
I invite you to go into the code that I've put in my website but here's the main idea
<body id="top" <?php body_class(); ?> data-spy="scroll" data-target=".navigation-bar">

                            home

                        ...

...

...

...

...

...

...

                        ...

After my footer I first add jQuery, then call for Bootstrap and then use JS to add certain IDs to every second element with a class of "a13_full-row":

ScrollSpy with script added ids


Do not install core when installing modules with composer

Question: I want to manage my modules with composer, so I created a composer.json file in my sites/my_site directory that looks like this:
<code>{
    "repositories": {
        "drupal": {
            "type": "composer",
            "url": "https://packages.drupal.org/8"
        }
    },
    "require": {
        "composer/installers": "~1.0",
        "drupal/addtoany": "^1.8",
        "drupal/workbench": "^1.0",
    },
    "extra": {
        "installer-paths": {
            "libraries/{$name}": ["type:drupal-library"],
            "modules/contrib/{$name}": ["type:drupal-module"],
            "profiles/contrib/{$name}": ["type:drupal-profile"],
            "themes/contrib/{$name}": ["type:drupal-theme"],
            "drush/{$name}": ["type:drupal-drush"],
            "modules/custom/{$name}": ["type:drupal-custom-module"],
            "themes/custom/{$name}": ["type:drupal-custom-theme"]
        }
    }
}
</code>
The problem is that when I run "composer install" or "composer update", I get a "core" directory with Drupal core created in my sites/my_site directory. Is there any way to avoid pulling in the core and only get the modules using composer from my sites/my_site directory?
Comment: Shouldn't composer be in the root directory?
Comment: It should. In my case though I am using a shared Drupal install and only have permissions to my *sites/my_site* directory, so want to manage the dependencies from there.
Answer: Did not try this personally, but should work. Add this to your composer.json root:
<code>"name": "drupal/core",
 "version": "8.4.2",</code>
(adjust version number to your installed core version)
Be aware though this is not considered a clean solution, the recommended way is for drupal composer is "all or nothing". And I really recommend the "all" way, you do get the comfort of dependency and version and patch management.
More info about this hack: https://github.com/composer/composer/issues/5024
Comment: Tried that, but for some reason it now fails claiming that drupal/core dependency is missing:
Problem 1
    - drupal/migrate_source_csv 2.0.0 requires drupal/core ~8.1 -> satisfiable by drupal/core[8.1.0, 8.1.0-beta1, ...]
Comment: Looks like adding version made it work:
"version": "8.4.2",


Script to delete columns based on list of possible values in row 1

Question: I would like a script to delete columns in a Google Spreadsheet if the contents match a list of approximately 30 possible text strings. e.g. Custom Variable 1, Custom Variable 3, Custom Variable 9, etc.
I'm new to Google Scripts. I've searched this forum but haven't found a starting point that handles my specific situation -- deleting columns based on a list of string values rather than a single value or value input from a dialogue box.
Any help would be greatly appreciated.
Scott C
Comment: Could you upload a copy spreadsheet as an example. It would be great to show to show the experiments you tried as well.
Answer: If I'm understanding what you're asking, you wish to read through a column and compare a list of values to the value in each row of that column.  If that column contains any one of those 30 values, delete that column.
One way you could do it:
Hard code an array that contains all of those values you have in mind.  Loop over the column you desire, storing each value into an array (So you would have two arrays total when you're done.  One with the hard coded values, the other with the column values).  Then, take those values from Array 2, and see if they match any of the words in Array 1.  If one of those values matches your list, delete that column.


Problemas em configurar origem permitida do CORS Spring Boot!

Question: Essa é a mensagem de erro que está dando;

Estou com dificuldade de fazer minha API Front-End ter permissão de acesso a minha API Back-End que é um projeto Spring Boot, eu tenho certeza que meu problema não é minha API Fron-End, o problema está na configuração do CORS em relação a permissão de acesso.
vou deixa as configuração do meu CORS logo abaixo
import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import br.com.mdw.config.MdwApiProperty;

@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CorsFilter implements Filter {

    @Autowired
    private MdwApiProperty mdwApiProperty;

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
            throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;

        response.setHeader("Access-Control-Allow-Origin", mdwApiProperty.getOriginPermitida());
        response.setHeader("Access-Control-Allow-Credentials", "true");

        if ("OPTIONS".equals(request.getMethod()) && mdwApiProperty.getOriginPermitida().equals(request.getHeader("Origin"))) {
            response.setHeader("Access-Control-Allow-Methods", "POST, GET, DELETE, PUT, OPTIONS");
            response.setHeader("Access-Control-Allow-Headers", "Authorization, Content-Type, Accept");
            response.setHeader("Access-Control-Max-Age", "3600");

            response.setStatus(HttpServletResponse.SC_OK);
        } else {
            chain.doFilter(req, resp);
        }

}

    @Override
    public void destroy() {
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
    }

}

E aqui onde configuro o acesso:
import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties("mdw")
public class MdwApiProperty {

    private String originPermitida = "http://localhost:8000";

    private final Seguranca seguranca = new Seguranca();

    public Seguranca getSeguranca() {
        return seguranca;
    }

    public String getOriginPermitida() {
        return originPermitida;
    }

    public void setOriginPermitida(String originPermitida) {
        this.originPermitida = originPermitida;
    }

    public static class Seguranca {

        private boolean enableHttps;

        public boolean isEnableHttps() {
            return enableHttps;
        }

        public void setEnableHttps(boolean enableHttps) {
            this.enableHttps = enableHttps;
        }

}

}

Foi criado uma classe chamada MdwApiProperty
Onde foi colocada a seguinte anotação:
@ConfigurationProperties("mdw")

E por causa dessa anotação é por isso no arquivo application-prod.properties aceitou essa linha de código.
mdw.seguranca.enable-https=true

Se isso for verdade se eu por acaso modificar a anotação da classe MdwApiProperty para
@ConfigurationProperties("habilitando-producao")

Iria fica assim no arquivo application-prod.properties .
habilitando-producao.seguranca.enable-https=true

E nesse arquivos onde habilita o apelido para  acesso ao projeto
package br.com.mdw;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

import br.com.mdw.config.MdwApiProperty;

@SpringBootApplication
@EnableConfigurationProperties(MdwApiProperty.class)
public class MdwApplication {

    public static void main(String[] args) {
        SpringApplication.run(MdwApplication.class, args);
    }
}

É aqui onde fica a anotação

mdw.seguranca.enable-https=true

spring.datasource.url={JDBC_DATABASE_URL}
spring.datasource.username={JDBC_DATABASE_USERNAME}
spring.datasource.password={JDBC_DATABASE_PASSWORD}

mdw.origin-permitida=https://mdw-arm-wladimir.herokuapp.com

Por algum motivo ele não está aceitando o acesso, preciso de ajuda.
Answer: Desativa a sua implementação CORs do SPRING e tenta usar uma destas 2 (duas) soluções:
Solução 1:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import com.example.algamoney.api.config.property.AlgamoneyApiProperty;

@Configuration
public class CorsConfig {

    @Autowired
    private AlgamoneyApiProperty algamoneyApiProperty;

    @Bean
    public FilterRegistrationBean corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

        CorsConfiguration configAutenticacao = new CorsConfiguration();
        configAutenticacao.setAllowCredentials(true);
        configAutenticacao.addAllowedOrigin(algamoneyApiProperty.getOriginPermitida());
        configAutenticacao.addAllowedHeader("Authorization");
        configAutenticacao.addAllowedHeader("Content-Type");
        configAutenticacao.addAllowedHeader("Accept");
        configAutenticacao.addAllowedMethod("POST");
        configAutenticacao.addAllowedMethod("GET");
        configAutenticacao.addAllowedMethod("DELETE");
        configAutenticacao.addAllowedMethod("PUT");
        configAutenticacao.addAllowedMethod("OPTIONS");
        configAutenticacao.setMaxAge(3600L);
        // source.registerCorsConfiguration("/oauth/token", configAutenticacao);
        source.registerCorsConfiguration("/**", configAutenticacao); // Global para todas as URLs da aplicação

        FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
        bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
        return bean;
    }
}

Solução 2:
import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.example.algamoney.api.config.property.AlgamoneyApiProperty;

@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CorsFilter implements Filter {

    @Autowired
    private AlgamoneyApiProperty algamoneyApiProperty;

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
            throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;

        response.setHeader("Access-Control-Allow-Origin", algamoneyApiProperty.getOriginPermitida());
        response.setHeader("Access-Control-Allow-Credentials", "true");

        if ("OPTIONS".equals(request.getMethod()) && algamoneyApiProperty.getOriginPermitida().equals(request.getHeader("Origin"))) {
            response.setHeader("Access-Control-Allow-Methods", "POST, GET, DELETE, PUT, OPTIONS");
            response.setHeader("Access-Control-Allow-Headers", "Authorization, Content-Type, Accept");
            response.setHeader("Access-Control-Max-Age", "3600");

            response.setStatus(HttpServletResponse.SC_OK);
        } else {
            chain.doFilter(req, resp);
        }

    }

    @Override
    public void destroy() {
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
    }

}

Observação: Você deve efetuar o debugger do código e verificar se a chamada está entrando na primeira chamada: "OPTIONS".equals(request.getMethod()), pois o navegador sempre executa uma requisição "OPTIONS" antes da chamada que você requisitou.


MySQL profiling shows slower query time than PHP microtime difference

Question: I'm trying to measure the speed of a particular MySQL query in two ways.
In MySQL
SET profiling = 1;
QUERY;
SHOW PROFILES;

In PHP
$time = microtime(true);
Database::instance()->query(...);
$time = microtime(true) - $time;

The time being shown from SHOW PROFILES is ~0.009s (duration).
The time being shown from $time is ~0.001s.
I am very confused because I was expecting the PHP measurement to be slower than the MySQL measurement. It doesn't make any sense. Can anybody explain?
EDIT:
I am aware of the caching and I've tried it with SQL_NO_CACHE but it doesn't seem to make a difference. What I did notice though is that when I restarted MySQL server, the first few profiling attempts are in line with what PHP is reporting. But as I ran the profiling a few more times, there was a sudden jump in duration:
1   0.00137900  SELECT COUNT(*) AS count ↵
2   0.00138800  SELECT COUNT(*) AS count ↵
3   0.00912700  SELECT COUNT(*) AS count ↵
4   0.00907300  SELECT COUNT(*) AS count ↵
5   0.00878900  SELECT COUNT(*) AS count ↵
6   0.00984200  SELECT SQL_NO_CACHE COUNT(*) AS count ↵
7   0.00895200  SELECT SQL_NO_CACHE COUNT(*) AS count ↵

Breaking down the queries that takes ~0.009s, it appears the jump in time is from creating a tmp table. But the first two times it did this it was really fast (1 and 2). And based on PHP microtime() it's also really fast. Something with the profiler slowed it down after?
starting    0.000055
checking permissions    0.000004
Opening tables  0.000013
System lock 0.000050
optimizing  0.000010
statistics  0.000041
preparing   0.000012
Creating tmp table  0.007696
executing   0.000014
Copying to tmp table    0.000317
Sorting result  0.000031
Sending data    0.000007
removing tmp table  0.000423
Sending data    0.000008
init    0.000007
optimizing  0.000011
executing   0.000005
end 0.000003
query end   0.000011
closing tables  0.000002
removing tmp table  0.000004

closing tables  0.000006
freeing items   0.000054
logging slow query  0.000002
cleaning up 0.000003
Comment: A lot of things could cause this small a discrepancy. Are your MySQL server and PHP running on different machines? Does the query return a lot of data? It's possible your php client hasn't yet retrieved the data when measuring the end of the query interval.
Comment: They are running on the same machine. And the query returns a count, almost no data at all.
Answer: That's because the query becomes cached after the SQL script executes.
So, the SQL script runs the query the first time. The result is cached. Then the PHP script executes. The query is already cached, so the result is retrieved faster.

(Ollie Jones)
Try including the SQL_NO_CACHE option in your query when measuring performance.
E. g.
SELECT SQL_NO_CACHE COUNT(*), SUM(col)
  FROM table

@ Ollie Jones
But there's even caching on the level of hard disk I/O, which is independent of MySQL's caching.
-ktm
Answer: The query cache applies like ktm5124 says but also the profile has to do some other operations on the os and this will slow the query also down in general.
You could test this with the SQL_NO_CACHE parameter and compare the php and mysql result without the profiler.
Comment: I'm leaning towards this answer, as you can see with my edit. The profiler appears to be doing something funny. Are you able to elaborate on your answer?
Comment: SQL_NO_CACHE only says that the result is not stored in cache (but can be still read out of the cache).
So you need to restart mysql and do the same on shell and in php (with the SQL_NO_CACHE paremeter) to have a good result for comparing. And I would rather use a EXPLAIN SELECT than the profiler itself...The profiler is good for analysing slow queries but not a ~0.009 query difference... I hope this helps.
Comment: Thanks. But I am very convinced now that the cause for the discrepancy is due to temporary tables being created on disk. When the PHP scripts run, it creates a new SQL session, so all temp tables get deleted, and it creates temp table on memory. But when I keep my sequel pro session open, and run profiler a few times, it eventually decides to write temp tables to disk. I can't quite confirm this yet cuz I'm not sure how to view temp tables but I believe that would be the answer.


AFP/SSH stopped working on OS X Server

Question: I have 3 Mac OS X servers all bound to AD, all configured in the Golden Triangle setup. All 3 are completely separate from each other in terms of services, but all reside on the same internal network and are all bound to the same Active Directory domain. Two are 10.5.x (latest updates) and one is 10.6.3.
Last weekend, all 3 simultaneously stopped allowing Active Directory users access to certain services, specifically AFP & SSH. SMB still works fine on all 3. I asked the AD admin if anything changed, and he said "Yes, we made a change to user accounts to toughen up security", and suggested I use firstname.lastname@example.com instead of just username. This still didn't work.
I have completely removed one of my servers from AD, and re-joined, but this didn't work either. I can do kinit from command line and get a Kerberos ticket. sudo klist -ke shows all services are configured to use the correct Kerberos principles.
I have been scavenging the logs for any useful info. The AFP log just shows that I'm connecting and disconnecting. The DirectoryService.log shows stuff about misconfigured Kerberos hashes, but my research is showing that's not uncommon. /var/log/system.log isn't showing anything useful that I can see.
I'm not sure where to go from here. Any help/ideas appreciated.
Answer: This turned out to be a security setting regarding kerberos on the AD side.  Made the setting a little less restrictive and everything was OK.

AFP/SSH stopped working on OS X Server


problem in admin_path module with my custom content type paths

Question: I have created a content type "Test".
The content which I created of type "Test" have the paths like this "test/content1, test/content2 etc. means test/*".
So I entered the path "test/*" in the administrative pages.
But when I try to open any content of type "Test", First the url becomes like this "#overlay=test/content1" and then it redirects to "test/content1" and does not appear in overlay.
I tried some other paths of the contents of some other types and they are working fine in overlay.
So what is the problem with my custom content type? Any one who have some idea about t his?
Comment: Or is there any module which I can use?
Answer: Check out overlay_paths module. Should solve your problem. Sounds like admin_paths() isn't accepting up your url aliases. http://drupal.org/project/issues/overlay_paths.


Does hasStableIds and getItemId have any significance if DiffUtil is used?

Question: We are using DiffUtil.CalculateDiff with a custom DiffUtil.Callback to handle changes to the underlying data.

The identity of our elements is rather complex, and that's why we are using custom equality check in areContentsTheSame and areItemsTheSame.
However, the adapter has two things hasStableIds and getItemId.

The problem is that getItemId returns a long and the identity of our elements can't be conflated into a long in a stable way (although otherwise stable considering our custom equality check in diffutil's callback.
The question is this:
What to do with hasStableIds and getItemId in this case, and are those at all used when DiffUtil is pushing changes to the adapter?
Comment: https://github.com/yuriy-budiyev/code-scanner
Answer: I don't know what has stables actually does in the background but it is used for keeping the elements in the order and remove any buggy stuff.
Comment: Thanks for the reply, but unfortunatelly it doesn't help much. The point is: I know what hasStableIds does when using regular notifyDataSetChanged is used with adapter. But my question is specifically whether this is needed, or used at all when diffutil is used.


How to define threadprivate variables for class member variables

Question: I'm writing a multi-threaded object oriented c++ code via OpenMP. For the sake of simplicity and generality I'm obliged to define some of my variables and functions in a template class. I want to make them threadprivate to exploit the persistence features of thread-private variables and be able to use them in other functions or source files embedded in or calling the template class. but I receive the following error:
"... is not file, namespace, or block scope variable"
I know that I can only use threadprivate directive for file, namespace, or block scope variables but I was wondering if there's any alternative to apply threadprivate directive for class members too. My total code layout is like:
template<typename T> class EntContainer
{
private:
T** empties;
e_NumPointers;
e_nLists;
.
.
.
public:
#pragma omp threadprivate(empties, e_NumPointers, e_nLists)
void function
{
// do some paralle stuff using #pragma omp parallel
}
}

Thanks in advance
Comment: If you declare them as `static` class members, will OpenMPI allow them to be threadprivate?
Comment: It looks like you are mistaking the MPI implementation Open MPI with the threading paradigm OpenMP.
Comment: No!! declaring them as static does not help
Answer:
OpenMP 3.0 draft allows threadprivate on static class members, but
   #pragma omp threadprivate must be present inside of the definition of
  the class containing the threadprivate static class member, and must
  lexically preceed all uses. source

Making the members static and moving the #pragma omp threadprivate directive right below their declarations should fix the error.


Probability in a classification problem

Question: In a classification problem (let's say of two categories, cat and dog) with a softmax output, does the probability have any physical meaning other than assigning the category to the input based on the higher probability? Putting another way: Can I say that there is X% probability of the input being a cat and Y% that the input is a dog? Of course, X+Y = 100%.
Comment: Are you asking if a probability is a probability?
Comment: [Does this answer your question?](https://stats.stackexchange.com/a/550549/247274)
Answer: Yes, though to be a little more precise, in this case X% is the probability of being a dog (and 100-X% is the probability of being a cat) according to the model. Models can be very wrong and different models will assign different probabilities.
Comment: I would call it "current belief", because by itself, it's not really a probability IMHO
Comment: @Shaz If you are asking Alberto, you can tag him by typing @ before his username (typed with no spaces), as I have done with yours at the beginning of this comment.
Comment: @AlbertoSinigaglia it is absolutely a probability. The fact that it might be wrong/miscalibrated does not change that
Comment: @AlbertoSinigaglia Your interpretation would seem to exclude Bayesian probability, among other things. https://en.wikipedia.org/wiki/Bayesian_probability & https://en.wikipedia.org/wiki/Probability_interpretations
Comment: Thanks. How would you interpret it otherwise?
Comment: @Firebug if softmax indeed is a categorical distribution, you cannot say that it's the true $p(y|x)$, and you can see this from the fact that it can be badly calibrated https://arxiv.org/pdf/1706.04599.pdf and temperatures can vary your output significantly... my point is not that the output of a softmax is not a distribution, indeed it is, form a mathematical perspective, but it's not the one that most of people would imagine ($p(y|x)$)

Probability in a classification problem


Formatar campo numerico com 2 casas deciamais no datagridview c#

Question: Preciso mostrar uma coluna no datagridview coluna de peso bruto esse formato 0.00, mas não estou conseguindo, esta coluna não e criada em nenhum banco de dados, eu criei a coluna direto no datagridview, já fui nas formatações numéricas da coluna, deixei como N2, e mesmo assim nada de formatar a coluna.Preciso que quando eu digite um número decimal na coluna, EX digito 10 quando sair da célula, precisa mostrar 10.00
Segue meu codigo:
    private void btn_xml_Click(object sender, EventArgs e)
    {
        string FileName = @"C:\Xml_Entrada\" + txt_chave.Text + ".xml";
        List<ClasseItensXml> ListaItens = new List<ClasseItensXml>(); //A lista é do tipo ClasseItensXml
        XmlDocument doc = new XmlDocument();
        doc.Load(FileName);
        var proditens = doc.GetElementsByTagName("prod");

        foreach (XmlElement nodo in proditens)
        {
            ListaItens.Add(
                 new ClasseItensXml()
                 {
                     CodigoProduto = nodo.GetElementsByTagName("cProd")[0].InnerText.Trim(),
                     NomeProduto = nodo.GetElementsByTagName("xProd")[0].InnerText.Trim(),
                     QuantidadeComercializada = nodo.GetElementsByTagName("qCom")[0].InnerText.Trim()
                 });

            //Repare que cada "nodo" é um item, portanto só adiciona um ClasseItensXml na lista.
        }

        dgw_Xml.DataSource = ListaItens; //por fim, usa a lista de source

        dgw_Xml.Columns["PesBruto"].DefaultCellStyle.Format = "N2";
    }

Comment: Jovem, te peço do fundo do meu coração, formate o código desta pergunta.
Answer: A coluna que você criou, não tem nenhuma propriedade associada, assim o DataGridView trata como uma string e não formata com o N2.
Você pode alterar sua classe ClasseItensXml para adicionar a propriedade PesoBruto e associar essa coluna:
public class ClasseItensXml
{
    public string CodigoProduto {get;set;}
    public string NomeProduto {get;set;}
    public string QuantidadeComercializada {get;set;}
    public decimal PesoBruto {get;set;}
}

O restante permanece igual, mas agora o DataGridView sabe que se trata de um tipo numérico e faz a formatação.

Não se esqueça de associar a coluna à propriedade com o DataPropertyName e a formatação você também pode fazer pela interface gráfica do visual studio.
Comment: Muito obrigado Rovann, mais uma vez..


Change the thread count of test plan in JMeter, at run time

Question: I want to change the number of threads for a JMeter test plan at runtime.
I have Googled my problem and found a proposed solution to use JMeter plugins. But in this solution I would have to schedule the thread group before running the test plan, which I don't want. I also found another potential solution  which changes the property, but doesn't affect test plan behavior at run time.
Ultimately, what I am trying to do is change the thread number given in a thread group and have it immediately increase or decrease the number of threads in the current running test plan.
Is this possible?
Comment: Can you describe your requirements? What exactly are you trying to accomplish by dynamically manipulating number of user threads? What's the goal of your test?
Comment: dynamic number of increase and decrease the load on web-app. I can do it via jmeter plugin but i have to pre define the threads and wait time which i dont want.
Comment: I get that, but why? You don't have to wait, you can schedule the threads any way you want with plugins Ultimate or Stepping thread group. You can ramp up hundreds of them in a few seconds. I'm asking because I've never heard of such a request and was wondering why would someone need it.
Comment: mbonaci. There is a software called LoadUI which provides this functioanlity. If we want to test app according to usage environment when user may be 10 or 1000 at a time, then we want this kind of functionality. We can't predict the user of our site.
Answer: IMHO that's just a fancy feature that has no real benefit when doing proper performance testing.
In order to generate relevant test output (report), you need repeatability, and clearly defined test methodology and scenarios. In order to compare impact of any application/server/infrastructure changes, you need repeatability.
What do you mean by

We can't predict the user of our site

That's why we do performance testing at the first place. To find out what is our application/infrastructure limit.
I.e. the most significant metric you can produce is how your application response time changes when number of parallel users change. But not change erratically, in run time.
With jMeter plugins' Ultimate thread group you can cover any imaginable scenario.
Comment: mbonaci. what you are saying is right. But i want to know that is there any functionality available as LoadUI provides..?? I am more interested in that because JMeter provides more feature than LoadUI.
Comment: sometimes it would be useful to have the ability to play with the values to hone in on a range that will be useful in repeatable tests. I have spent hours experimenting with different values for ultimate and stepping threadgroups, where I could have taken minutes if I had the capability requested. Just because you don't have a use case, doesn't mean there isn't one.
Comment: What's the metric you were chasing. I still don't understand why exactly you needed the feature. Could you elaborate on your use case please?
Answer: The short answer is: no, you cannot change the number of threads dynamically during runtime. Each thread count value is only read once when the test plan is first compiled and is not resolved again after this point, so it remains fixed.
Answer: This feature is indeed useful, and surprisingly difficult to implement even with commercial tools such as Loadrunner. I would compare it to finding a loudspeakers maximum volume. You would manually turn the volume up until it started to crackle, then turn it back down slightly to maintain that maximum volume. In the same way, to find the peak capacity of an application, you want control to 'turn the volume up' until errors are seen, then back it down slightly to see if it stabilizes. You can then maintain that load to find where the bottleneck is.
Anyway, to answer the question, what I have done in the past is use an external influence, such as a file name or similar. Then combine that with the thread unique reference you can control which threads run and which are held (by pausing or similar).
For example, if you start with 100 threads, then create a file called '5.txt' in a specific location, you can add code such that if the threads sees that it's own reference is equal to or lower than the number then it can run. If not then it drops into a pause. At the start of this example 5 threads would run, and 95 would pause. You can then rename the file to '25.txt', and threads 6 to 25 would start running. It would work the other way too, changing it to '20.txt' would mean threads 21-25 pause again.
The key is to start enough threads to exceed your expected peak.
Comment: This is one of the "Selling features" of Soasta CloudTest, that you can change the volume to eleven on the fly....  So, if this is a critical feature then you may want to look at CloudTest.
Answer: you can change it based on a variable which you set in a startup thread. See below.
In Jmeter how do I set a variable number of threads using a beanshell sampler variable?
However once the thread group has started you can not modify it. To the guy who said this feature would not be useful I disagree. There are many types of load tests and they do not all have the same number of users running for the duration. Here are just 2 example types of enterprise load tests we conduct at the bank where I work:

duration test - same number of users run the entire time (possibly
with a short ramp-up period)
Break point test - ramp up the number of users incrementally till the
application breaks
Spike test - run with a constant number of users but sporadically
throw in a large number of users

A break point test ramps up the number of users until the application breaks (the point being to see how high your app can scale). You can sort of do this using the thread groups "ramp up period" property. If you set ramp up time to 1000 and the number of threads to 100 it will add 1 thread every 10 seconds.
Spike tests are like duration tests but at some intervals a large number of users log in. This is used to guage the applications response time during peak hours or how it will respond if you all of a sudden get a large number of users (a very real scenario).
I find that Jmeter does not handle all load test scenarios that are needed in enterprise load testing. One work around Im considering is to just start all the threads but find a way to make some of them sleep. So you could set the number of threads to 1000 but somehow make 980 of them sleep or do nothing. Then maybe when the time_in_seconds%5==0 (every 5 minutes) you allow the other threads to run - simulating a spike test. The idea is you can hard code the threads to 1000 and will always have 1000 threads running - but they don't all have to be doing something at all times.
(in other words you can probably find a way but you have to get creative)
Update:
I just found this plugin which allows different types of testing. Have not tried it yet but looks promising:
http://jmeter-plugins.org/wiki/ThroughputShapingTimer/
Answer: You can set/change the number of threads at runtime using command line option...
you can use function calls, or variable references to User Parameters (which in turn could be functions), or variable references to variables set up by functions earlier in the test. There's more than one way to do it.
Suppose you want to be able to vary the number of threads in a test plan. Choose a suitable property name, say group1.threads. Replace the thread count in the GUI (or the JMX, if you're feeling brave!) with the following function call:
Please set below property in JMeter thread group as below
 ${__property(group1.threads)}
Then, when starting JMeter, define the property on the command line:
jmeter -Jgroup1.threads=10
Comment: This is not runtime if you run Jmeter with a parameter at starttime. How can you change the paramter while the test is running?
Answer:
We can't predict the user of our site.

Sure you can.  This is what the HTTP logs of your existing site are for.  You can also use logs from tools like Omniture or your CDN logs.   If you look at the combination of Actual user IP address, request and referer tags in the logs you will be able to build a traversal map of ever single user on your site.  You will be able to profile the high hit unique leaf node pages of a given business process to understand how many times a particular business process happens an hour.   You will be able to examine abandonment by taking a look at the funnel in tools such as Omniture.   If you need tools for this analysis I recommend Splunk. It's easy to install and configure. Time to value is very fast.
The more log data you have which you are using to profile the closer you can come to actual for what users do during a day/week/month/spot sale/end of quarter/end of year/etc....You need to combine actual at a point in time with actual from earlier points in time to project growth over time since you will need to allow for growth in your performance testing model.
If you don't get the values right then the value of your test as a predictor of what will/can happen in production will be quite low. This is not a failure of any given tool, but a failure in process on the planning front for the actual load model used as part of the test requirements. If you cannot build these models then you need to pull someone into your team who can.
This ability to produce a valid load model independent of tool is the difference between tests which reduce risk and throwing load.
Answer: By enabling the BeanShell server you can vary properties at runtime.
Just enable it and telnet on port 9001 (warning: not secure!)
Based on a test I did, unfortnately, it appears that the thread count it's not applied at runtime. However you can still manipulate the load of the test by other means, for example apply a costant throughput timer parametrized with a property named "throughput" and vary it at runtime like this:
<code>setprop("throughput","2000");
</code>
It's well explained in the guide.


negative Euribor rates and stationary time series

Question: When we want to ensure stationary time series of Euribor, how to do this with a negative rates as we can't take the difference log of the variable when it is negative?
Comment: Can you provide a little more context and detail about your question?
Comment: some of the current interbank lending market rates are negative. therefore, it is not possible to take the difference log of the data to ensure  stationarity as we cant take the logarithm of negative real numbers).
Comment: please edit your question to include new information.
Comment: I would expect interest rates to be stationary. I would *not* expect the interest rate to contain a unit root.
Comment: the historical European three months interbank interest rates are not stationary..
Answer: A point of confusion may be what is meant by log returns or log interest rate.
Let's say you have a return or interest rate $R$ like -.01 or .02.
The log return is:
$$ r = \log\left(1 + R \right) $$
If you're doing time series analysis, it's often nice to work with log returns because summing log returns (and taking the exp) is equivalent to multiplying simple returns.
Also note that the log return is equivalent to the difference in log prices.
$$r_{t+1} = \log\left( \frac{P_{t+1}}{P_t} \right)  = \log P_{t+1} - \log P_t$$


Incredibly slow query performance with $lookup and "sub" aggregation pipeline

Question: Let's say I have two collections, tasks and customers.
Customers have a 1:n relation with tasks via a "customerId" field in customers.
I now have a view where I need to display tasks with customer names. AND I also need to be able to filter and sort for customer names. Which means I can't do the $limit or $match stage before $lookup in the following query.
So here is my example query:
<code>db.task.aggregate([
    {
        "$match": {
            "_deleted": false
        }
    },
    "$lookup": {
        "from": "customer",
        "let": {
            "foreignId": "$customerId"
        },
        "pipeline": [
            {
                "$match": {
                    "$expr": {
                        "$and": [
                            {
                                "$eq": [
                                    "$_id",
                                    "$$foreignId"
                                ]
                            },
                            {
                              "$eq": [
                                "$_deleted",
                                false
                              ]
                            }
                        ]
                    }
                }
            }
        ],
        "as": "customer"
    },
    {
        "$unwind": {
            "path": "$customer",
            "preserveNullAndEmptyArrays": true
            }
    },
    {
        "$match": {
            "customer.name": 'some_search_string'
        }
    },
    {
        "$sort": {
            "customer.name": -1
        }
    },
    {
        "$limit": 35
    },
    {
        "$project": {
            "_id": 1,
            "customer._id": 1,
            "customer.name": 1,
            "description": 1,
            "end": 1,
            "start": 1,
            "title": 1
        }
    }
])
</code>
This query is getting incredibly slow when the collections are growing in size. With 1000 tasks and 20 customers it already takes about 500ms to deliver result.
I'm aware, that this happens because the $lookup operator has to do a tablescan for each row that enters the aggregation pipeline's lookup stage.
I have tried to set indexes like described here: Poor lookup aggregation performance but that doesn't seem to have any impact.
My next guess was that the "sub"-pipeline in the $lookup stage is not capable of using indexes, so I replaced it with a simple
<code>"$lookup": {
    "from": "customer",
    "localField": "customerId",
    "foreignField": "_id",
    "as": "customer"
}
</code>
But still the indexes are not used or don't have any impact on performance. (To be honest I don't know which of both is the case since .explain() won't work with aggregation pipelines.)
I have tried the following indexes:

Ascending, desecending, hashed and text index on customerId
Ascending, desecending, hashed and text index on customer.name

I'm grateful for any ideas on what I'm doing wrong or how I could achive the same thing with a better aggregation pipeline.
Additional info:
I'm using a three member replica set. I'm on MongoDB 4.0.
Please note: I'm aware that I'm using a non-relational database to achieve highly relational objectives, but in this project MongoDB was our choice due to it's ChangeStream feature. If anybody knows a different database with a comparable feature (realtime push notifications on changes), which can be run on-premise (so Firebase drops out), I would love to hear about it!
Thanks in advance!
Comment: "I have tried the following indexes: Ascending, desecending, hashed and text index on customerId, Ascending, desecending, hashed and text index on customer.name"
Did you try using a covered index with only the information required in your final projection? (so customId, name) If loading customers in the bottleneck, it might help a bit -- https://docs.mongodb.com/manual/core/query-optimization/#covered-queries
Comment: As far as doing ChangeStream-like things with relational databases, you could also try tailing the db oplog, or using something like mysql's `ON UPDATE CURRENT_TIMESTAMP` https://dev.mysql.com/doc/refman/8.0/en/timestamp-initialization.html to track a `lastUpdated` field and then have a query find the recently updated items.
Comment: My problem is, that no index seems to make any difference in runtime. Seems it is not used in the "sub"-pipeline.
Answer: I found out why my indexes weren't used.
I queried the collection using a different collation than the collection's own collation.
But the id indexes on a collection are always implemented using the collections default collation.
Therefore the indexes were not used.
I changed the collection's collation to the same as for the queries and now the query takes just a fraction of the time (but still slow :)).
(Yes you have to recreate the collections to change the collation, no on-the-fly change is possible.)
Answer: Have you considered having a single collection for customer with tasks as an embedded array in each document? That way, you would be able to index search on both customer and task fields.
Comment: No, unfortunately denormalization is not an option in my case. Tasks have relations to other collections as well.


PHP: Which is more efficient: a concatonated return variable, or returning each line individually?

Question: I have a sytem where I want to build an HTML table in PHP from data retrieved from a databse.
I've previously used two different methods for creating the HTML and echoing it.
Building a return variable, and echoing at the end of the PHP script:
<?php
$data['category']['parts']; // format of the data

$retval = '
';
foreach($data as $category) {
  $retval .= '
';
  foreach($category as $data) {
    $retval .= ''.$data.'	'
  }
  $retval .= '
';
}
$retval .= '
';

echo $retval;

The other method is to echo each line as the code comes to it:
<?php
$data['category']['parts']; // format of the data

echo '
';
foreach($data as $category) {
  echo '
';
  foreach($category as $data) {
    echo ''.$data.'	'
  }
  echo '
';
}
echo '
';

Which of the two methods is more efficient, in terms of processor/memory usage, and also for processing speed?
Is there actually a real difference, rather than just a question of style?
Comment: i would go with echo $retval; because its more readable
Answer: My shot is: whatever you find more readable. Impact on performance is so small that probably you won't see any difference.
However, if you really care, echo should be faster (nothing better than a performance test on your specific scenario) because string concatenation will resize retval multiple times (and this will impact performance).
Even better you should avoid concatenation also in your echo:
<?php
$data['category']['parts']; // format of the data

echo '
';
foreach($data as $category) {
  echo '
';
  foreach($category as $data) {
    echo '', $data, '	';
  }
  echo '
';
}
echo '
';

Do you want to do better? Just construct your own string builder object (but, honestly, gain is so small that you should seriously consider if worth your effort).
Comment: Thanks a lot, I'd not considered the processing time of the string concatenation adjusting the length.
Comment: but still you are calling language construct which also do some stuff to return html ?  :/
Comment: @NullPoiиteя absolutely but `echo` is somehow faster than (let's say) `printf` and for a literal string like this it won't (should?!) perform any new string allocation (string copy by value is optimized and with increasing size of `retval` you won't copy huge strings). I'd agree it's such small difference you won't ever care about (unless you're working on Facebook). Yes, no inline string stuff but allocation & copy are heavier. BTW string concatenation `.` improved a lot in PHP5.
Comment: exactly for almost 99% cases no need to care about this http://codepad.viper-7.com/Y43kKM  http://codepad.viper-7.com/xILtNl s
Comment: Agree, I would highlight that more on my answer. I think even something more readable is better (shorttags, as suggested by dognose, I don't know about their performance).
Answer: If you want to use kind of a templating to generate your page, I would rewrite your code to something like this.
(this can be in a dedicated file, and just included to display output)
<?php
$data['category']['parts']; // format of the data
?>

include ('templates/theFileIWantToShow.php');

---- snip files here. Processing above, template bellow.

  <?foreach($data as $category):?>

    <?foreach($category as $data):?>
       <?=$data?>
    <?endforeach;?>

  <?endforeach;?>

This would offer (imho) best readability when it comes down to large html pages with only a few wildcards.
Advantages:

You get clean html with only a few spots of php in between
You can easily replace the template files without touching the generating code
you can reuse templates. Providing direct output and/or building strings is a mess, when it comes down to reuse the same html-markup for a certain element over and over.

Note that this requires shorttags to be enabled in your php.ini for PHP < 5.4.0.
Comment: you dont require to enable short since PHP 5.4.0, `<?=` is always available
Comment: Thanks for the styling advice, but I was more after efficiency, and the answer above has that covered much better


Very Basic textView Problem in my Kotlin Code

Question: I just started learning Kotlin and decided to write a very basic programm. It's supposed to be a currency converter, but for now, I just want to give out the number I type into "editTextEingabe" ("Eingabe" = german for input) to the textView "textViewAusgabe". I'll link my code below and would be very happy if someone could help me with that. Thanks!
P.S.: "editTextEingabe" is set to be only allowing numbers and decimals to be enterd, but I don't know if that changes anything.
Update 2: The problem is, that the decimal number I type into editTextEingabe isn't appearing in textViewAusgabe after In press the Button buttonBerechnen. More simple: I want to give out text (here: A number) that I just typed into an editText to a textView widget, but that doesn't work.
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        var eingabe = findViewById<EditText>(R.id.editTextEingabe)
        var eingabeString: String = eingabe.toString()
        var ergebnis: String
        val buttonBerechnen: Button = findViewById(R.id.buttonBerechnen)

        buttonBerechnen.setOnClickListener() {
            ergebnis = eingabeString
            textViewAusgabe.text = ("$ergebnis")
        }

    }
}

Update: Here's my XML, if that helps:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textViewAusgabe"
        android:layout_width="108dp"
        android:layout_height="46dp"
        android:layout_marginTop="228dp"
        android:background="#4600BCD4"
        android:textAlignment="center"
        app:layout_constraintStart_toEndOf="@+id/textView3"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editTextEingabe"
        android:layout_width="121dp"
        android:layout_height="46dp"
        android:layout_marginStart="64dp"
        android:layout_marginTop="228dp"
        android:background="#4600BCD4"
        android:ems="10"
        android:hint="$ eingeben"
        android:inputType="number|numberDecimal"
        android:numeric="integer|decimal"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="43dp"
        android:layout_height="30dp"
        android:layout_marginTop="236dp"
        android:text="$ = "
        android:textAlignment="center"
        android:textSize="22sp"
        app:layout_constraintStart_toEndOf="@+id/editTextEingabe"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="57dp"
        android:layout_height="27dp"
        android:layout_marginTop="236dp"
        android:layout_marginEnd="36dp"
        android:text="€"
        android:textSize="22sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toEndOf="@+id/textViewAusgabe"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/buttonBerechnen"
        android:layout_width="116dp"
        android:layout_height="69dp"
        android:layout_marginStart="144dp"
        android:layout_marginBottom="188dp"
        android:background="#4600BCD4"
        android:text="Berechnen"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>```

Comment: 1. What's the behavior currently, is the text just not being updated when you press the button? 2. Have you tried running your code through the debugger? Can you put a breakpoint inside `setOnClickListener` and check what the value of `eingabe.text` is?
Comment: 1. There is text addet to textViewAusgabe, but it's just nonsense android text ("androidx.appcompact.widgetAppCompactEd..") everytime I press the button
2. I did, but it didn't gave me any useful information, at least not for me
3. What do you mean with that?
Answer: By calling eingabe.toString() you're capturing the current input value of eingabe at the time of the call and assigning it to eingabeString. Anything that happens with the input value of eingabe after that will not be reflected on eingabeString. Hence when the OnClickListener fires, your code won't grab the up-to-date value from eingabe, but will instead use the stale value saved in eingabeString. An easy way to fix it in my opinion is to get rid of eingabeString completely and always reference the input value from eingabe when assigning it to ergebnis:
var eingabe = findViewById<EditText>(R.id.editTextEingabe)
var ergebnis: String
buttonBerechnen: Button = findViewById(R.id.buttonBerechnen)

buttonBerechnen.setOnClickListener() {
    ergebnis = eingabe.toString()
    textViewAusgabe.text = ("$ergebnis")
}

Removed Edit
Comment: Thanks for the answer! Like the other comment, this doesn't seem to help though. May the problem be, that I didn't get the conversion from the decimal number I type into the editText to the String I need for the textView?
Comment: Btw. I just saw that I edited your comment, not my original post. I'm really sorry for that, I must missclicked something
Comment: @KenzoVolgmann, no worries, thanks for fixing that - I approved your edit. I'd suggest that you clarify what the problem actually is in your question: it wasn't clear to me so I assumed you're not getting the up-to-date input field contents when the button is clicked. Happy to help once you clarify the problem.
Answer: I think that you just need to retrieve the value of the EditText in the click event.
Update  your code to :
override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        var eingabe = findViewById<EditText>(R.id.editTextEingabe)
        var ergebnis = ""
        val buttonBerechnen: Button = findViewById(R.id.buttonBerechnen)

        buttonBerechnen.setOnClickListener() {
            ergebnis = eingabe.text.toString()
            textViewAusgabe.text = ("$ergebnis")
        }
    }

Comment: Thanks for the answer! Like the other comment, this doesn't seem to help though. May the problem be, that I didn't get the conversion from the decimal number I type into the editText to the String I need for the textView?
Answer: The problem with your code is that you use the variables ergebnis and eingabeString to set the text of the TextView, but these variables are declared inside the onCreate() method and their values are probably empty strings and they never change.
Since you have this import:
import kotlinx.android.synthetic.main.activity_main.*

you don't have to use findViewById() to get references to your widgets.
You can simplify your code to this:
buttonBerechnen.setOnClickListener() {
    textViewAusgabe.text = editTextEingabe.text.toString()
}

and remove all these variable declarations:
var eingabe = findViewById<EditText>(R.id.editTextEingabe)
var eingabeString: String = eingabe.toString()
var ergebnis: String
val buttonBerechnen: Button = findViewById(R.id.buttonBerechnen)

Comment: This worked very well. However, since this is intendet to be a curency converter when it's finished, I need to keep the variables. Can you tell me why it's not working my way besides beeing to complicated?
Comment: I edited my answer with a brief explanation.`eingabeString` is just a string and its value is  set inside onCreate() so it gets only 1 value which never changes. So when you type something in the EditText you don't reassign the text to this variable which then sets the text of the TextView. Anyway you don't need all these variables. Directly set the text of the TextView from the EditText.
Comment: So I've put "eingabe = findViewById(R.id.editTextEingabe).toString()" inside the setOnClickListener, but still, it doesn't do anything. You're right that I can remove a lot of it, but since I need those variables when continuing with my project, I need to know how it works with them too
Comment: If you need these variables then you must declare them in the class level and not inside onCreate. Also, the correct way to get that text is: `editTextEingabe.text.toString()`
Comment: I think I've made it with your last comment, thanks! And did I accept the answer correctly?


"Distribution Option" error in "CREATE TABLE table AS" in SQL Server

Question: I have been facing an issue with using CREATE TABLE AS. I have made three tables in a database. I intend to join the tables based on policy number and create a new table out of the joined data.
CREATE TABLE [dbo].[New_Customer_Segmentation_Data] AS
(
SELECT *
FROM [dbo].[CustomerSegmentation_updated]
INNER JOIN [dbo].[DimLapse]
ON [dbo].[CustomerSegmentation_updated].[PolicyNumber] = [dbo].[DimLapse].[Policy Number]
INNER JOIN [dbo].[dimclaim]
ON [dbo].[DimLapse].[Policy Number]  = [dbo].[dimclaim].[POLICYNUMBER]
)

It returns the error "'Distribution' option must be explicitly specified in "CREATE TABLE AS SELECT" and "CREATE MATERIALIZED VIEW" statements."
Can anyone guide me on this error?
Answer: Just like the error message says, you must specify a Distributed table design option with CTAS, eg from the docs:
CREATE TABLE [dbo].[FactInternetSales_new]
WITH
(
 DISTRIBUTION = ROUND_ROBIN
 ,CLUSTERED COLUMNSTORE INDEX
)
AS
SELECT  *
FROM    [dbo].[FactInternetSales];

CREATE TABLE AS SELECT
Comment: And some useful documentation on Distribution: https://learn.microsoft.com/en-us/azure/synapse-analytics/sql-data-warehouse/sql-data-warehouse-tables-distribute