Is additivity necessary for a left exact functor to preserve pullbacks?

Question: I'm having a bit of difficulty with exercise 5.16 from Rotman's An Introduction to Homological Algebra (second edition).
The exercise (at least the relevant part) reads

Prove that every left exact covariant functor $T:{}_R\mathbf{Mod} \to \mathbf{Ab}$ preserves pullbacks.

Now, I went back and checked the definition, and a covariant functor is defined to be left exact if
$$ 0\to A\to B\to C$$ being exact implies
$$ 0\to TA \to TB\to TC$$
is exact, with no mention of additivity.
Now, I'm aware of this question, but the comment on the question doesn't apply given the definition in Rotman, and the accepted answer to the question doesn't get me any further than the work I've already done. Thus this new question.
My work, and a precise statement of exactly where I'm running into trouble without additivity
It's not hard to show that left exact functors preserve direct sums,
and it's also not hard to check that
$$\require{AMScd}\begin{CD}
D @>>> C
\\
@VVV @VVgV
\\
B @>>f> A
\end{CD}$$
is a pullback square if and only if
$$0 \to D \to B\oplus C \newcommand\toby\xrightarrow \toby{f\pi_B-g\pi_C} A $$
is exact.
Then if we apply $T$ to this exact sequence, we get the exact sequence
$$ 0 \to TD \to TB \oplus TC \toby{T(f\pi_B-g\pi_C)} TA. $$
Now when $T$ is additive, we have
$$T(f\pi_B -g\pi_C) = (Tf)\pi_{TB}-(Tg)\pi_{TC},$$
so this exact sequence is the correct exact sequence to show that
$$\begin{CD}
TD @>>> TC
\\
@VVV @VVTgV
\\
TB @>>Tf> TA
\end{CD}
$$
is a pullback square.
However if $T$ is not additive, then it is not clear that the exact sequence we get from applying $T$ to the exact sequence from the pullback square in ${}_R\mathbf{Mod}$ is the correct exact sequence to prove that $TD$ is the pullback in $\mathbf{Ab}$. This is where I am stuck.
The question
Hence my questions:

Is there a way to finish the proof without using additivity?
Is there something special about these particular categories?
Is there perhaps a counterexample, showing that we need to assume additivity? (I'm not aware of any left exact but not additive functors (nor is Google, based on a quick search), so I don't even know where to begin searching for a counterexample)
Comment: A left exact functor is additive (me again for the same comment ;)). Rotman's definition is equivalent to any other. You proved that a left exact functor preserve finite direct sums. If a functor preserve the zero object and direct sums, then it is additive. This is because the addition of morphisms $f,g:A\to B$ is $A\overset{\Delta}\longrightarrow A\oplus A\overset{f\oplus g}\longrightarrow B\oplus B\overset{\nabla}\longrightarrow B$ where $\Delta$ is the diagonal and $\nabla$ the codiagonal.
Comment: @Roland Good point, I forgot about that. Let me see if I can work out the details there.
Comment: @Roland I've added an answer working out the details, but if you post your comment as an answer, then I'll accept that, since it did completely answer my question :)
Comment: That's ok, you wrote the details, I don't see the point of adding another answer.
Answer: Roland left an excellent comment reminding me that the addition in an Abelian category (well technically category with finite biproducts) can be defined purely in terms of the direct sum, diagonal and codiagonal maps.
If $f,g : A\to B$ are morphisms, we define $f+g$ to be the composite
$$ A\newcommand\toby\xrightarrow\toby{\Delta_A} A \oplus A\toby{f\oplus g}B\oplus B \toby{\nabla_B} B,$$
where $\Delta_A : A\to A\oplus A$ is the diagonal map and $\nabla_B : B\oplus B\to B$ is the codiagonal.
Then clearly any functor which satisfies $T(f\oplus g)=Tf\oplus Tg$ and $T\Delta_A = \Delta_{TA}$ and $T\nabla_B = \nabla_{TB}$ is additive.
Then as long as $T$ preserves the biproduct (meaning $T(A\oplus B)=TA\oplus TB$ and $T\iota_X=\iota_{TX}$ and $T\pi_X = \pi_{TX}$ for the structure morphisms $\iota_A,\iota_B,\pi_A,$ and $\pi_B$), and $T$ preserves $0$, meaning that $T0 = 0$, $T$ has these properties. (Equality here means up to natural isomorphism.)
This is because $\Delta_A$ is the map such that $\pi_{A,i}\Delta_A=\newcommand\id{\operatorname{id}}\id_A$ for $i=1,2$,
and then taking $T$ of this, we have that $T\Delta_A$ satisfies $\pi_{TA,i}T(\Delta_A)=\id_{TA}$ for $i=1,2$, thus $T\Delta_A=\Delta_{TA}$. A dual argument shows that $T\nabla_B=\nabla_{TB}$, and finally $f\oplus g$ is defined to be the map such that
$$\pi_{B,1}(f\oplus g)\iota_{A,1} = f,$$
$$\pi_{B,2}(f\oplus g)\iota_{A,2} = g,$$
$$\pi_{B,1}(f\oplus g)\iota_{A,2} = 0,$$
and
$$\pi_{B,2}(f\oplus g)\iota_{A,1} = 0.$$
Again, it's not hard to see that the preservation of 0 and the biproduct ensures that $T(f\oplus g)$ satisfies the requirement to be $Tf\oplus Tg$.
Finally, if $T$ is left exact, then $T$ preserves $0$ and the biproduct, and thus is additive
(As indicated in my question, I've already proved this, but I figured I'd add it for any future readers)
$T$ preserves $0$
Depending on how you interpret the definition, this is either definitional, (since you can read the definition as implying that you take $T$ of the entire left exact sequence to get the resulting exact sequence) or must be proved, if you take it to mean only that extending the sequence $TA\to TB \to TC$ by zero on the left results in an exact sequence, but it turns out that there's no difference.
Consider
$$ 0 \to 0 \toby{\id_0} 0 \toby{\id_0} 0.$$
This is clearly exact,
so
$$ 0 \to T0 \toby{\id_{T0}} T0 \toby{\id_{T0}} T0$$
is exact, which implies that $$0=\ker \id_{T0} = T0=\operatorname{im}_{\id_{T0}},$$
as desired.
$T$ preserves the biproduct
Proof:
We know $\pi_A\iota_A =\id_A$, so $(T\pi_A)(T\iota_A)=\id_{TA}$. This also applies to $\pi_B\iota_B$, so $T\pi_B$ must be an epimorphism. Hence, exactness of
$$0\to A \toby{\iota_A} A\oplus B \toby{\pi_B} B \to 0$$
not only implies exactness of
$$0\to TA \toby{T\iota_A} T(A\oplus B) \toby{T\pi_B} TB, $$
but actually the exactness of
$$0\to TA \toby{T\iota_A} T(A\oplus B) \toby{T\pi_B} TB \to 0,$$
and the fact that $(T\pi_A)(T\iota_A)=\id_{TA}$ implies that this exact sequence is split, giving
$T(A\oplus B) \simeq TA\oplus TB$.


btrfs increase raid capacity by replacing disks (and not adding disks!)

Question: I'm very excited about the new features of btrfs and would like to start tesing it.
Before I get started, I would like to ask if btrfs supports increasing raid capacity by replacing disks by bigger ones (and not adding additional disks).
Example:
A RAID10 consisting of 8x 2TB drives results in a capacity of 8 TB. Then, each 2TB drive gets replaced by a 6 TB drive. After each disk replacement, a rebuild / rebalance is executed. I'm wondering, if after the last disk replacement and rebalance the capacity jumps from 8TB to 24TB?
There is some lecture about it in the internet, but there is no 100% statement like "yes, after rebalance, the capacity gets increased!".
https://btrfs.wiki.kernel.org/index.php/Using_Btrfs_with_Multiple_Devices#Adding_new_devices
The NAS devices from Synology support exactly the feature I'm asking about: https://www.synology.com/en-global/knowledgebase/DSM/help/DSM/StorageManager/volume_diskgroup_expand_replace_disk
But, I'm not sure, if this feature is a native feature of btrfs or if the developers from Synology created it especially for their disk station operating system.
Answer: It should work as you have described it. However, an additional step may be necessary.
For example, if you put 4 drives with 3 GB each in a raid1 configuration, you'll end up with a capacity of 6 GB. Replacing two of those drives with 4 GB drives should give you 7 GB of capacity (btrfs disk usage calculator).
Step 1: Create BTRFS RAID1 volume with 4x 3G = 6G capacity:
<code># mkfs.btrfs -f -draid1 -mraid1 /dev/sdb /dev/sdc /dev/sdd /dev/sde >/dev/null
# mount /dev/sdb BTRFS/
# btrfs fi show BTRFS/
Label: none  uuid: e6dc6a95-ae5e-49c4-bded-77001b445ac7
    Total devices 4 FS bytes used 192.00KiB
    devid    1 size 3.00GiB used 331.12MiB path /dev/sdb
    devid    2 size 3.00GiB used 0.00B path /dev/sdc
    devid    3 size 3.00GiB used 0.00B path /dev/sdd
    devid    4 size 3.00GiB used 0.00B path /dev/sde

# parted -s /dev/sdb print | grep Disk
Disk /dev/sdb: 3221MB
Disk Flags:
# parted -s /dev/sdc print | grep Disk
Disk /dev/sdc: 3221MB
Disk Flags:
# parted -s /dev/sdd print | grep Disk
Disk /dev/sdd: 3221MB
Disk Flags:
# parted -s /dev/sde print | grep Disk
Disk /dev/sde: 3221MB
Disk Flags:
# df -h BTRFS/
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdb        6.0G   17M  5.3G   1% /mnt/BTRFS
# btrfs fi df BTRFS/
Data, RAID1: total=1.00GiB, used=320.00KiB
Data, single: total=1.00GiB, used=0.00B
System, RAID1: total=32.00MiB, used=16.00KiB
Metadata, RAID1: total=256.00MiB, used=112.00KiB
GlobalReserve, single: total=16.00MiB, used=0.00B
</code>
Step 2: Replace 2 3G drives (3rd and 4th drive) with 4G drives:
<code># parted -s /dev/sdf print | grep Disk
Disk /dev/sdf: 4295MB
Disk Flags:
# parted -s /dev/sdg print | grep Disk
Disk /dev/sdg: 4295MB
Disk Flags:
# btrfs replace start -f 3 /dev/sdf BTRFS/
# btrfs replace start -f 4 /dev/sdg BTRFS/
# btrfs fi show BTRFS/
Label: none  uuid: e6dc6a95-ae5e-49c4-bded-77001b445ac7
    Total devices 4 FS bytes used 512.00KiB
    devid    1 size 3.00GiB used 1.28GiB path /dev/sdb
    devid    2 size 3.00GiB used 1.25GiB path /dev/sdc
    devid    3 size 3.00GiB used 1.06GiB path /dev/sdf
    devid    4 size 3.00GiB used 544.00MiB path /dev/sdg

# df -h BTRFS/
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdb        6.0G   17M  5.2G   1% /mnt/BTRFS
</code>
The RAID1 filesystem should have a capacity of 7 GB, but it only has 6 GB.
Solution
It needs to be resized to use all available space (balance won't help). It needs to be resized on every device that has been replaced, so on device #3 and #4.
<code># df -h BTRFS/
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdb        6.0G   17M  5.8G   1% /mnt/BTRFS
# btrfs fi show BTRFS/
Label: none  uuid: e71b4996-5f7c-4b08-b8d8-87163430b643
    Total devices 4 FS bytes used 448.00KiB
    devid    1 size 3.00GiB used 1.00GiB path /dev/sdb
    devid    2 size 3.00GiB used 1.00GiB path /dev/sdc
    devid    3 size 3.00GiB used 288.00MiB path /dev/sdf
    devid    4 size 3.00GiB used 288.00MiB path /dev/sdg

# btrfs fi resize 3:max BTRFS/
Resize 'BTRFS/' of '3:max'
# btrfs fi resize 4:max BTRFS/
Resize 'BTRFS/' of '4:max'
# df -h BTRFS/
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdb        7.0G   17M  6.8G   1% /mnt/BTRFS
</code>
The filesystem now has its expected capacity of 7 GB.
Step 2 (alternative): Remove drives (the old way, not recommended)
Before the replace command was added, the only workaround to replace drives was to add a new drive and remove the old one. However, this may take more time. And it has the drawback that it will leave you with a devid hole, i.e., the removed device's id won't be used anymore and the device ids no longer match their respective position in the raid array.
<code># df -h BTRFS/
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdb        6.0G   17M  5.3G   1% /mnt/BTRFS
# btrfs dev add -f /dev/sdf BTRFS/
# btrfs dev add -f /dev/sdg BTRFS/
# btrfs fi show BTRFS/
Label: none  uuid: ac40a98a-ac3b-4563-9ec9-6135332e5cdc
    Total devices 6 FS bytes used 448.00KiB
    devid    1 size 3.00GiB used 1.03GiB path /dev/sdb
    devid    2 size 3.00GiB used 1.25GiB path /dev/sdc
    devid    3 size 3.00GiB used 1.03GiB path /dev/sdd
    devid    4 size 3.00GiB used 256.00MiB path /dev/sde
    devid    5 size 4.00GiB used 0.00B path /dev/sdf
    devid    6 size 4.00GiB used 0.00B path /dev/sdg

# btrfs dev rem /dev/sdd BTRFS/
# btrfs dev rem /dev/sde BTRFS/
# df -h BTRFS/
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdb        7.0G   17M  6.8G   1% /mnt/BTRFS
# btrfs fi show BTRFS/
Label: none  uuid: efc5d80a-54c6-4bb9-ba8f-f9d392415d3f
    Total devices 4 FS bytes used 640.00KiB
    devid    1 size 3.00GiB used 1.00GiB path /dev/sdb
    devid    2 size 3.00GiB used 1.00GiB path /dev/sdc
    devid    5 size 4.00GiB used 1.03GiB path /dev/sdf
    devid    6 size 4.00GiB used 1.03GiB path /dev/sdg
</code>
When using add/remove, it is not necessary to manually grow the volume.
Note that, when using add/remove, the 3rd drive in the raid array has index 5 instead of 3, which may be confusing when you need to identify a drive based on its slot in your rack.

This is BTRFS version 4.4. Future versions may behave differently.
Comment: Won't `btrfs device remove ` remove the hole?
Comment: @TomHale unfortunately it doesn't. Quite the opposite: `dev rem 3` *creates* a "hole" at #3 as you won't be able to use this id anymore. Maybe not noticeable with 3 drives but it gets chaotic with a real setup (say > 10 drives) because the drive ids don't match the positions in the rack.
Comment: Thanks, the resize step was required indeed.
Answer: Yes, the capacity will grow in btrfs when you replace the drives with bigger ones. But make sure you always have backups! While the RAID0/1 code is not nearly as buggy as the RAID5/6 code in btrfs (as of 07/2016), your device replacement would not be the first one to go horrible wrong.


C Merge sort stuck in an infinite loop

Question: My programme uses recursion for divide and conquer but i am getting an infinite loop due to reasons unknown. and i am still getting the unsorted array as an answer
/**
 * MergeSort.c
 *
 * Uses recursion for divide and conquer
 *
 *
 * Implements merge sort for an array
 */

Initialize the method calls for merge and merge sort
#include<stdio.h>
#include<conio.h>
void merge(int array[6],int beg,int mid,int end);
void mergesort(int array[6],int beg,int end);

Initialize the display functions
void display(int array[6]);

int main(void)
{
     // Initializes the array
     int array[6]={3,1,2,9,5,4};
     // Initialize the beginning and the end
     int beg=0, end=5;
     // Implement the Merge Sort
     mergesort(array,beg,end);
     getch();
}

void mergesort(int array[6],int beg,int end) //Calls Initial merge sort
{
      int mid;
      mid=(beg+end)/2;
      while (beg<end)
      {
            mergesort(array,beg,mid);        //Left part of the array
            mergesort(array,mid+1,end);      //Right part of the array
            merge(array,beg,mid,end);        //merge two sorted arrays
      }
 }

 //merges two subarrays
void merge(int array[6],int beg,int mid,int end)
{
     int temp[6]; //Declare a temp array for storing the sorted elements
     int k=beg;
     int i=beg;   //initialize the pointers for two sub arrays
     int j=mid;

     while (i<mid && j<end)
       {
            if(array[i]<array[j])
              {
                 temp[k]=array[i];
                 i++;
                 k++;
              }
            else
              {
                 temp[k]=array[j];
                 j++;
                 k++;
              }
        }

    //Clearing any remaining elements in the sub array
     while (i<mid)
      {
           temp[k]=array[i];
           i++;
           k++;
      }

    //Clearing any remaining elements in the sub array
    while (j<end)
      {
          temp[k]=array[j];
          j++;
          k++;
      }

    //Reassign the sorted elements to the original array
    for(i=0,k=0;i<end,k<end;i++,k++)
      {
          array[i]=temp[k];
      }
    //prints the individual array elements
    display(array);           //display array
}

//Displays the entire array

void display(int array[6])
{
     //prints the individual array elements
    for (int i=0;i<6;i++)
      {
             printf("%d ",array[i]); //prints the individual array elements
      }
     printf("\n"); //Enter a new line after every iteration
}

Answer: Well, from mergesort function,
while (beg<end)
  {
        mergesort(array,beg,mid);        //Left part of the array
        mergesort(array,mid+1,end);      //Right part of the array
        merge(array,beg,mid,end);        //merge two sorted arrays
  }

beg and end won't be editted. So it will do somethingin this loop forever.
I think you should change from while into if.
And moreover, if your end is 5 (last index in that array/segment) the last element in the segment won't be sorted. That's mean this array won't be sorted. You have to change to...
 while (i<mid && j<=end) //from j<end to j<=end and for another loop
   {
        if(array[i]<array[j])
          {
             temp[k]=array[i];
             i++;
             k++;
          }
        else
          {
             temp[k]=array[j];
             j++;
             k++;
          }
    }

Good luck on DQ<3

C Merge sort stuck in an infinite loop


Qual a diferença entre Integrated Security e Persist Security?

Question: Nas strings de conexão com o SQL Server normalmente vejo as opções Integrated Security e Persist Security, O que significa cada uma delas e quais valores elas podem receber?
Essa dúvida me surgiu vendo os comentários da seguinte pergunta: Erro "Login For User Failed" no serviço windows
Answer: Integrated Security significa a forma de autenticação do banco:
- se "false", você deve informar login e senha na string de conexão;
- se "true", não são necessários login/senha, pois será utilizado o usuário autenticado no Windows.
Persist Security significa se a informação de login será salva ou não quando houver a conexão.
- se "false", a nível de código, uma vez feita a conexão, você pode obter as informações da string de conexão menos login/senha;
- se "true", as informações da string de conexão são retornadas incluindo login/senha;
Claro que se usar Integrated Security=true, Persist Security=true passa a ser irrelevante, uma vez que a informação de login/senha não vai estar presentes na string de conexão.
Mais detalhes aqui: connection string


How to sum first n number of entries in an array?

Question: I'm new to programming and learning about javascript recursion. It's my 5th day with JavaScript and following an online course.
The problem is that I need to sum up first n numbers of entries (first 3 numbers in this case) in an array.
But I'm ending up with sum of all and that also I'm not sure about.
var theArray = [1, 3, 8, 5, 7];

function sum(arr, n) {
  if (n <= 0) {
    return 1;
  } else {
    return arr[n - 1] + sum(arr, n - 2);
    //assuming n is arr.length and n-1 is hence arr.length-1
  }
}

console.log(sum(theArray, 3));

What am I doing wrong?
I checked most people are solving such with reduce method or with for of loop. I didn't learn those yet in the curriculum. But I know 'for loop' but that's good if the numbers are in incremental order I guess. Please explain this problem in my level.
Comment: Step back and think about what your code does. You are saying that the sum of 0 elements is 1. Is that really the right answer, or would another number be better? :)
Comment: When you recurse, you should only subtract `1` from `n`, not `2`. The sum of the first N elements of an array is the Nth element plus the sum of the first N-1 elements. If you subtract 2, you're skipping every other element.
Comment: @Bluebug: yes, it would be `return arr[n] + sum(arr, n-1);` if what you call "the nth element of the array" was `arr[n]`. But it's not. Array indices start at 0. "The first 3 elements" are elements 0, 1 and 2, not elements 1, 2 and 3.
Comment: You missing a closing `}` at the end of your function
Comment: not sure why recursion would be needed here.
Comment: @Dominik Fixed. Thanks for spotting :)
Comment: @RobinZigmond You are right. My bad. I thought to set the base case to just anything.
Comment: @Barmar Hmm that clears up some confusion. But the curriculum was hinting ``return arr[n-1] + sum(arr, n-1); `` That is where I'm getting confused. Shouldn't it be like ``return arr[n] + sum(arr, n-1);`` ?
Comment: @epascarello That's the criteria for the given problem. I must use recursion. :)
Comment: @Stef Oh right! Now I can picture it in my mind! Thanks !
Answer: When implementing something recursively, it's a good idea to think about the base condition.  It does look like that's where you started, but your logic is flawed.  n is the number of elements you would like to sum together.  So let's say you want to sum the first n=3 elements of the array [2,3,4,5,6] and get 9.  Here are the iterations.

return arr[n-1] + sum(arr, n-1) // arr[2] + sum(arr, 2) === 4 + sum(arr, 2) === 4 + 5
return arr[n-1] + sum(arr, n-1) // arr[1] + sum(arr, 1) === 3 + sum(arr, 2) === 3 + 2
return arr[n-1] // arr[0] === 2 (base case when n is 1)

I didn't solve the problem for you since this is obviously a school exercise, but the point is that the base case and the recursive call that you have are both slightly off.
Comment: Right, I can visualize it now. Thanks a lot for clearing it out. And I'm not in school... I'm learning on my own while working full time :) But yes you got the point. I should figure out the rest on my own.
Answer: Array of undetermined length, find the largest sum possible for any 3 consecutive numbers.

[2,0,1,100,200,10,7,2, 300,2,1 0] here 100,200,10 = 310 is the correct answer. Here only the combination of these 3 numbers produces the largest sum.

[2,1,0,20,71 = 0+20+7 = 27 is the answer that you should return.

const findMax = (numberArray) => {
let first = 0;
let second = 1;
let third = 2;
let max = 0;

    for(; third < numberArray.length; first++, second++, third++) {
    if(max < (numberArray[first] + numberArray[second] + numberArray[third])) {
        max = numberArray[first] + numberArray[second] + numberArray[third];
    }
    }
  return max;
}

console.log('Max sum of three consecutive numbers in array ===>  ',findMax([2, 0, 100, 200, 10, 7, 2, 300, 2, 10]));
Comment: Welcome to StackOverflow!  Please visit the [Help Center](https://stackoverflow.com/help), take the [tour](https://stackoverflow.com/tour) and read up on [asking good questions](https://stackoverflow.com/help/asking) here.  This raises a mostly-unrelated question, and attempts to answer it.  Please don't do this in answers to questions.  If you want to [post your own question](https://stackoverflow.com/questions/ask), please do do.  (You're free to answer it if you think you have a good solution.)  But don't attach it to an existing question.
Comment: This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/31149599)


Scala specs2 matcher for a File's extension

Question: I'm trying to create a specs2 matcher that asserts the validity of a File extension (by reusing the existing endWith matcher). However I get a type error. How could I get around it?
import java.io.File
import org.specs2.mutable.Specification
import org.specs2.matcher.{ Expectable, Matcher }

class SampleSpec extends Specification {
  def hasExtension(extension: => String) = new Matcher[File] {
    def apply[S <: File](actual: Expectable[S]) = {
      actual.value.getPath must endWith(extension)
    }
  }
}

Here's the compiler error:
<console>:13: error: type mismatch;
 found   : org.specs2.matcher.MatchResult[java.lang.String]
 required: org.specs2.matcher.MatchResult[S]
             actual.value.getPath must endWith(extension)

Answer: You can indeed use the ^^ operator (taking inspiration from the parser combinators operators) and simply write:
def hasExtension(extension: =>String) = endWith(extension) ^^ ((_:File).getPath)

For reference the various ways of creating custom matchers are presented here.
Comment: Thanks! I like the conciseness of it. Is there no operator that does the same but with the arguments swapped? I think it would be a bit more clear as to what it does.
Comment: That's a good suggestion. You can now do it with the latest 1.12-SNAPSHOT: def haveExtension(extension: =>String) = ((_:File).getPath) ^^ endWith(extension)
Answer: Okay, I've got it working by using the ^^ operator which adapts between matcher types. It looks like a functor's map function to me.
def hasExtension(extension: => String) = new Matcher[File] {
  def apply[S <: File](actual: Expectable[S]) = {
    actual must endWith(extension) ^^ ((file: S) => file.getPath)
  }
}


Finding the local address used on a multihomed network

Question: I am running a TCP/IP server and binding it to INADDR_ANY (0.0.0.0). My network is multi-homed so has multiple local IP addresses. So by using INADDR_ANY, the client can connect to me using any of those local addresses.
I now want to construct a full url with my local IP address. I will send this address to the client and (assuming no NAT-traversal), the client should be able to use the sent address to connect to my server. What socket API can I use to find this address? This address has to be used in the IP packet, so should be discoverable.
Comment: Does this answer your question - https://stackoverflow.com/a/3909936/12396017 ? I mean ```getifaddrs```
Answer: There is no common socket API function across platforms for this type of query. You have to use OS-specific APIs instead.

On 'Nix platforms, you can use getifaddrs().
On Windows, you can use GetAdaptersInfo() (XP and earlier), GetAdaptersAddresses() (Vista and later), or WSAIoctl(SIO_GET_INTERFACE_LIST/_EX).
And so on.
Comment: getifaddrs etc will give me all the my ip addresses, I however need to find out the one used by the remote end currently connecting to me
Comment: @doron if a client is already connected to your TCP server, then `accept()` and `getpeername()` give you the client's IP, and `getsockname()` on the socket returned by `accept()` gives you the IP the client connected to. But if the client is already connected, why do you need to send a URL to the client to connect to? If the client needs to make a secondary connection to your server, it can just use the same IP it already used earlier. Your question doesn't make sense. Please clarify your scenario.


Verificar a linha que contém uma string dentro de um arquivo TXT

Question: Preciso de um ajuda com um programa que leia um arquivo e me responda em que linha contém uma string dentro dele.
Exemplo
Eu tenho um arquivo .txt e dentro dele eu tenho 5 linhas:
Arquivo.txt:
arroz
batata
feijão
carne
laranja

Eu preciso que o programa me retorne, por exemplo, se eu procurar laranja linha 5, se eu procurar batata, linha 2.
Answer: Podes fazer o seguinte:
with open('Arquivo.txt') as f:
    for l_num, l in enumerate(f, 1): # percorrer linhas e enumera-las a partir de 1
        if 'laranja' in l: # ver se palavra esta na linha
            print('laranja foi encontrada na linha', l_num)
            break
    else: # caso não haja break
        print('nao foi encontrada a palavra')

Usando uma função:
def get_line(word):
    with open('Arquivo.txt') as f:
        for l_num, l in enumerate(f, 1): # percorrer linhas e enumera-las a partir de 1
            if word in l: # ver se palavra esta na linha
                return l_num
        return False # não foi encontrada

print(get_line('laranja')) # 5
print(get_line('feijão')) # 3
print(get_line('banana')) # False

Se quiseres verificar se a linha é exatamente igual à palavra podes mudar a condição para (strip()):
...
if word == l.strip(): # retirar quebra de linha
...

Answer: Uma forma de fazê-lo em uma versão um pouco mais extensa:
#!/usr/bin/env python3

arquivo = 'arquivo.txt'
pesquisa = 'batata'

achei = False

with open(arquivo,'r') as f:
    linha = 1
    for texto in f:
        if pesquisa == texto.strip():
            achei = True
            break
        linha+=1
f.close()

if achei:
    print('Encontrado "{}" na linha {}'.format(pesquisa, linha))
else:
    print('Não encontrei "{}"'.format(pesquisa))

Ele lê o arquivo linha a linha, procurando pela texto colocado em pesquisa, esta versão não é a mais rápida porém é mais didática.
Comment: De fato, a solução funciona, mas não é a versão mais idiomática. Se foi proposital, por favor, ignore o comentário, mas, caso desconheça mesmo, te convido a ler a [resposta do Miguel](https://pt.stackoverflow.com/a/288005/5878) que é uma versão mais *pythonica*.
Comment: Para simplificar: quando utilizas o gerenciador de contexto para abrir o arquivo não existe a necessidade de fechá-lo manualmente. O próprio `with` se encarrega disso (veja mais [aqui](https://pt.stackoverflow.com/q/49238/5878)); a contagem da linha pode ser simplificada utilizando o `enumerate`; e, por fim, não há a necessidade de utilizar uma *flag* `achei` para verificar o resultado da busca se utilizar o `else` do `for` para tratar o caso de insucesso.
Answer: #!/usr/bin/env python3

import sys

lines = open("arquivo.txt").readlines()
for i in range(0, len(lines)):
    if sys.argv[1] in lines[i].strip():
        print("%s na linha %d" % (lines[i].strip(), i + 1))
        break

Modo de usar:
$ ./teste.py lara
laranja na linha 4

Se quiser trocar o teste do fragmento por um teste exato, substituta "in lines..." por "== lines...".


Demystifying MySQL Read/Write Ratio on RDS

Question: I am in the process of upgrading/selecting resources for an application powered by a MySQL database running on Amazon RDS.  I am considering introducing read replicas and some other strategies to increase availability under increasingly heavy loads. (The oldest story in the book.)  As such, I have been monitoring my DB server and trying to get a grasp on where bottle necks are and such, and I am getting some very conflicting information on what the read/write ratio is:
Firstly, I will say that my application is relatively write heavy compared to many other types of applications.  I know that much.
Using cloud watch in the AWS RSD control panel, I am typically seeing the following metrics:
Read Operation to Write Operation ratio: 1:20 (1 read for every 20 writes)
Read and write ratios for throughput and latency are generally consistent with that ratio.
This seems utterly absurd to me.  I can't imagine how or why my app would be doing so many more writes than reads.
As such, I went looking for a second opinion, in the form of monitoring my DB in real time with MySQL Work Bench Administration Dashboard.  There, the evidence is different.  I am seeing the following on average:
Read Query Execution to Write Query Execution: 6:1 (6 reads queries executed for every 1 write query executed)
Either one of these monitoring tools is wildly inaccurate, or I have a fundamental misunderstanding of the data.
On to the questions:

Why would these two tools be showing such wildly different results?
Is there a better way for me to accurately determine the effective read/write ratio of my Database so that I can make prudent scaling decisions?
Answer: Read/write operations != read/write queries.
One "write" query may result in several disk IO write operations depending on the nature of your query, what engine you're using, what indices you have, etc.
Comment: also maybe RDS cache takes some read I/O, which would lower this value from the disk point of view. Not sure if RDS includes those reads within the IOPS count though
Comment: An appropriately-sized InnoDB buffer pool, once warned up, will of course dramatically reduce disk I/O on read queries, and write I/O is, conversely, further amplified out of proportion to queries by the writes to the redo log and binary log.  (The officially supported storage engine in RDS for MySQL is InnoDB). (+1)
Comment: a read query may cause writes as well
Comment: Roger that. I do acknowledge that the two metrics I presented are different. It's just that the discrepancy seems staggering. My goal is to determine the potential efficacy of scaling read queries independent of writes.  From what i am reading,  select queries that are causing the storage engine to perform write operations could benefit from being routed to a read replica freeing up capacity  to the master for additional write operations. Thoughts?


Como alinhar um formulário ao lado de um iframe?

Question: Queria que o meu formulário começasse ao lado do mapa que coloquei na página, mas não consigo.
Segue o meu código.

Cadastro

                    <form name="form1" action="" method="post">

                            Nome

                                <input type="text" name="nome" class="form-control" placeholder="Digite seu nome" />

                            Endereço
                            <input type="text" name="endereco" class="form-control" placeholder="Digite seu endereço" />

                                    CEP

                                        <input type="text" name="cep" class="form-control" required />

                                            <button type="button" class="btn btn-danger">
                                                Buscar
                                            </button>

                                    Telefone

                                        <input type="text" name="tel1" class="form-control" />

                            Estado
                            <select name="estado" class="form-control" placeholder="Selecione o estado">
                                <option value="ac">Acre</option>
                                <option value="al">Alagoas</option>
                                <option value="am">Amazonas</option>
                                <option value="ap">Amapá</option>
                                <option value="ba">Bahia</option>
                                <option value="ce">Ceará</option>
                                <option value="df">Distrito Federal</option>
                                <option value="es">Espírito Santo</option>
                                <option value="go">Goiás</option>
                                <option value="ma">Maranhão</option>
                                <option value="mt">Mato Grosso</option>
                                <option value="ms">Mato Grosso do Sul</option>
                                <option value="mg">Minas Gerais</option>
                                <option value="pa">Pará</option>
                                <option value="pb">Paraíba</option>
                                <option value="pr">Paraná</option>
                                <option value="pe">Pernambuco</option>
                                <option value="pi">Piauí</option>
                                <option value="rj">Rio de Janeiro</option>
                                <option value="rn">Rio Grande do Norte</option>
                                <option value="ro">Rondônia</option>
                                <option value="rs">Rio Grande do Sul</option>
                                <option value="rr">Roraima</option>
                                <option value="sc">Santa Catarina</option>
                                <option value="se">Sergipe</option>
                                <option value="sp">São Paulo</option>
                                <option value="to">Tocantins</option>
                            </select>

                            Comentários
                            <textarea name="comentarios" class="form-control" rows="4"></textarea>

                            Sexo

                                <input type="radio" name="sexo" value="M" /> Masculino

                                <input type="radio" name="sexo" value="F" /> Feminino

                                <input type="radio" name="sexo" value="O" /> Outros

                                <input type="checkbox" name="chkAtivo" value="1" /> Ativo

                            <input type="submit" class="btn btn-primary" value="Enviar" />
                            <input type="reset" class="btn btn-primary" value="Limpar" />

                    </form>

Answer: Uma forma é usando float: left e um margin-right para dar um espaçamento:

iframe{
   float: left;
   margin-right: 30px;
}

Cadastro

                 <form name="form1" action="" method="post">

                         Nome

                             <input type="text" name="nome" class="form-control" placeholder="Digite seu nome" />

                         Endereço
                         <input type="text" name="endereco" class="form-control" placeholder="Digite seu endereço" />

                                 CEP

                                     <input type="text" name="cep" class="form-control" required />

                                         <button type="button" class="btn btn-danger">
                                             Buscar
                                         </button>

                                 Telefone

                                     <input type="text" name="tel1" class="form-control" />

                         Estado
                         <select name="estado" class="form-control" placeholder="Selecione o estado">
                             <option value="ac">Acre</option>
                             <option value="al">Alagoas</option>
                             <option value="am">Amazonas</option>
                             <option value="ap">Amapá</option>
                             <option value="ba">Bahia</option>
                             <option value="ce">Ceará</option>
                             <option value="df">Distrito Federal</option>
                             <option value="es">Espírito Santo</option>
                             <option value="go">Goiás</option>
                             <option value="ma">Maranhão</option>
                             <option value="mt">Mato Grosso</option>
                             <option value="ms">Mato Grosso do Sul</option>
                             <option value="mg">Minas Gerais</option>
                             <option value="pa">Pará</option>
                             <option value="pb">Paraíba</option>
                             <option value="pr">Paraná</option>
                             <option value="pe">Pernambuco</option>
                             <option value="pi">Piauí</option>
                             <option value="rj">Rio de Janeiro</option>
                             <option value="rn">Rio Grande do Norte</option>
                             <option value="ro">Rondônia</option>
                             <option value="rs">Rio Grande do Sul</option>
                             <option value="rr">Roraima</option>
                             <option value="sc">Santa Catarina</option>
                             <option value="se">Sergipe</option>
                             <option value="sp">São Paulo</option>
                             <option value="to">Tocantins</option>
                         </select>

                         Comentários
                         <textarea name="comentarios" class="form-control" rows="4"></textarea>

                         Sexo

                             <input type="radio" name="sexo" value="M" /> Masculino

                             <input type="radio" name="sexo" value="F" /> Feminino

                             <input type="radio" name="sexo" value="O" /> Outros

                             <input type="checkbox" name="chkAtivo" value="1" /> Ativo

                         <input type="submit" class="btn btn-primary" value="Enviar" />
                         <input type="reset" class="btn btn-primary" value="Limpar" />

                 </form>

Comment: Obrigada !!!!!!
Comment: De nada, mas vc só pode marcar uma resposta. Marcando uma desmarca a outra. :)
Answer: Existe mais de uma forma de chegar no resultado esperado... A forma que eu utilizei foi atraves do display: flex com a propriedade justify-content: space-between que deixa os elementos filhos um ao lado do outro com um espaço entre eles. Veja no exemplo:

.flex{
display: flex;
justify-content: space-between;
}

Cadastro

                    <form name="form1" action="" method="post">

                            Nome

                                <input type="text" name="nome" class="form-control" placeholder="Digite seu nome" />

                            Endereço
                            <input type="text" name="endereco" class="form-control" placeholder="Digite seu endereço" />

                                    CEP

                                        <input type="text" name="cep" class="form-control" required />

                                            <button type="button" class="btn btn-danger">
                                                Buscar
                                            </button>

                                    Telefone

                                        <input type="text" name="tel1" class="form-control" />

                            Estado
                            <select name="estado" class="form-control" placeholder="Selecione o estado">
                                <option value="ac">Acre</option>
                                <option value="al">Alagoas</option>
                                <option value="am">Amazonas</option>
                                <option value="ap">Amapá</option>
                                <option value="ba">Bahia</option>
                                <option value="ce">Ceará</option>
                                <option value="df">Distrito Federal</option>
                                <option value="es">Espírito Santo</option>
                                <option value="go">Goiás</option>
                                <option value="ma">Maranhão</option>
                                <option value="mt">Mato Grosso</option>
                                <option value="ms">Mato Grosso do Sul</option>
                                <option value="mg">Minas Gerais</option>
                                <option value="pa">Pará</option>
                                <option value="pb">Paraíba</option>
                                <option value="pr">Paraná</option>
                                <option value="pe">Pernambuco</option>
                                <option value="pi">Piauí</option>
                                <option value="rj">Rio de Janeiro</option>
                                <option value="rn">Rio Grande do Norte</option>
                                <option value="ro">Rondônia</option>
                                <option value="rs">Rio Grande do Sul</option>
                                <option value="rr">Roraima</option>
                                <option value="sc">Santa Catarina</option>
                                <option value="se">Sergipe</option>
                                <option value="sp">São Paulo</option>
                                <option value="to">Tocantins</option>
                            </select>

                            Comentários
                            <textarea name="comentarios" class="form-control" rows="4"></textarea>

                            Sexo

                                <input type="radio" name="sexo" value="M" /> Masculino

                                <input type="radio" name="sexo" value="F" /> Feminino

                                <input type="radio" name="sexo" value="O" /> Outros

                                <input type="checkbox" name="chkAtivo" value="1" /> Ativo

                            <input type="submit" class="btn btn-primary" value="Enviar" />
                            <input type="reset" class="btn btn-primary" value="Limpar" />

                    </form>

Comment: Valeu e obrigada pela explicação.


Connecting to Flash Debugger when my AIR app comes back from background

Question: I am trying to debug why my AIR ios app take a long time to load when it comes back from the background after an extended period of time.
Currently, I can only connect to the Flash debugger (the popup asks for the IP) when I launch the app for the first time.
Is it possible to debug it when it comes back from the background too? I am not getting the Flash debugger popup when I reinvoke my app from the background.
Thanks!
Answer: Short answer. No this doesn't seem to be possible. As soon as the app goes into the background the debugging session terminates.
You could add a textfield to your app and trace or log messages to it. The messages could include what it is you are measuring.
Comment: lol. It contain a lot of expletives but essentially is the same.


Special Character in Visual Studio Code

Question: How to solve this in Visual Studio Code. The document was created in notepad, it's perfect. See Image.
Sample
Have a nice day!
Comment: what have you try?
Answer: It's solved. In notepad I saved unicode format.
Sample
Thank you guys!

Special Character in Visual Studio Code


Publishing on Google Cloud Marketplace

Question: While deploying a VM solution on Google Cloud Marketplace, is it mandatory to use its password autogeneration feature?
Can I not publish my solution on marketplace without password autogeneration?
Answer: I found that for security reasons you cannot use a default password and ask users to change it, and that there's an admin portal used in your solution, the password should be randomly generated for each instance.
If this is the case,then you wouldn't be able to publish your solution on Marketplace without the password automatically-generated.
Here is the information that I found:
Image & deployment - Component - Admin Password
Setting and retrieving application user credentials

Publishing on Google Cloud Marketplace


main.cpp:63:12: error: invalid conversion from 'int (*)[4]' to 'int' [-fpermissive]. What am I doing wrong?

Question: #include <stdio.h>
#include <limits.h>
#include <iostream>

#define V 5
using namespace std;

void dfs(int ,int );
int kante(int, int);//edge
int G[V][V];// Graph
int i;
int mark[10];// mark the vertex, if it's been visited
int S[V]; // Stack, with the nodes
int ps; // counter of nodes in S
int j,Kn;
int l;

void dfs(int G,int k){
    for(int j=1; j<=V; j++)
       mark[j]=0; // initialise all nodes as unvisited
S[1]=k;  // starting node=k
ps=1; // S has one element
while(ps>0){
    Kn==S[ps];
    for(int i=1;i<=V;i++){// check for neighbour
        if (mark[i]==1) ps=ps-1; // already visited-->delete the node from S
        else
            if(mark[i]==0)// not visited
            {
                if ((G[i][Kn]!=0) || (G[Kn][i]!=0)){// Kn adjacent with i
                    dfs(G,i); // do the same for node i
                    mark[i]=1; // mark it as visited
                    ps++;
                    S[ps]=i; // put it in stack
                }
            }
    }

   }
   }

int main(){
int G[4][4]={{0,1,1,0},
             {0,0,1,0},
             {1,0,0,1},
             {0,0,0,1},
};
cout<<"Tiefensuche, startknoten 0:\n"<<endl; // DFS, starting node

dfs(G,0); // applied dfs on starting node 0

return 0;
}

I don't understand why this doesn't work. This code should be traversing the graph in the depth by visiting all the nodes. How can I redefine the Graph(Matrix) so that it can be passed as a parameter to the function dfs.
I get the following errors:
main.cpp:42:29: error: invalid types 'int[int]' for array subscript
                     if ((G[i][Kn]!=0) || (G[Kn][i]!=0)){
                             ^
main.cpp:42:47: error: invalid types 'int[int]' for array subscript
                     if ((G[i][Kn]!=0) || (G[Kn][i]!=0)){
                                               ^
main.cpp: In function 'int main()':
main.cpp:63:12: error: invalid conversion from 'int (*)[4]' to 'int' [-fpermissive]
     dfs(G,0);
            ^
main.cpp:30:6: note:   initializing argument 1 of 'void dfs(int, int)'
 void dfs(int G,int k){

Comment: Your function declares the parameter `G` to be an `int`, but you are calling the function with an argument of type `int[4][4]`. That doesn't make sense.
Answer: You are using G as a 2D array in dfs. The declaration of the function should reflect that. Instead of
void dfs(int ,int );

use
void dfs(int (*G)[4], int k);


What is the usual topology of Cauchy sequences?

Question: What kind of topology can be induced on $\mathcal{C}(\mathbb{Q})$ (Cauchy sequences on $\Bbb Q$), besides trivial ones ?
Comment: Pick your favourite bijection $\mathcal{C}(\mathbb{Q}) \rightarrow \mathbb{R}$, then you see that your question amounts to: "What are the topologies on $\mathbb{R}$"? I guess you have some restrictions in mind? Otherwise you get a lot of topologies.
Comment: What is the *purpose* of introducing a topology on $C(\mathbb Q)$?
Comment: Discrete topology I'm sure
Comment: I was wanting to compare the metric-induced topology in $\mathbb{R}$ with the quotient topology (looking at $\Bbb R$ as the Cauchy sequence quotient $\mathcal{C}(\Bbb Q)/{\sim}$, where $(x_n)\sim (y_n) \iff (x_n-y_n)\to 0$). Later, i was thinking that i can introduce a topology in $\mathcal{C}(\Bbb Q)$ such those topologies are equivallent.
Answer: There are two main approaches:

Cauchy sequences as meaningful objects. There is a natural map from $\mathcal{C}(\mathbb{Q})$ to $\mathbb{R}$, taking each Cauchy sequence to its limit. The usual topology on $\mathbb{R}$ then "pulls back" to a topology on $\mathcal{C}(\mathbb{Q})$. This topology is of course not $T_0$, since distinct Cauchy sequences with the same limit are topologically equivalent, but we usually care about equivalence classes of Cauchy sequences instead of individual Cauchy sequences anyways so who cares.

Note that this is "ahistorical" - it assumes we've already built $\mathbb{R}$. We can avoid talking about $\mathbb{R}$ directly by instead defining a pseudometric on $\mathcal{C}(\mathbb{Q})$ in the obvious way, and then looking at the topology that induces.

Cauchy sequences as just sequences. On the other hand, given any topological space $X$ there is a natural topology on the set $X^\omega$ of sequences of elements of $X$ - the product topology. The set $\mathcal{C}(\mathbb{Q})$ then can be viewed as a subspace of the space $\mathbb{Q}^\omega$. This topology is $T_0$ (indeed, metrizable) but is much less natural: it ignores the whole point of Cauchy-ness. (E.g. the set of sequences which don't happen to begin with "$17$" is open in this topology.)

It's easy to check that these topologies are incomparable, which is unsurprising given that they're totally unrelated. In almost every situation, the former is the "right" topology (and indeed the "$T_0$ification" of $\mathcal{C}(\mathbb{Q})$ with respect to this topology is more natural than the set $\mathcal{C}(\mathbb{Q})$ itself).


Echelon form of a matrix

Question: As far as I know each matrix has a unique echelon form. But sometimes I find more than one when I change my row operation. Why does it happen?  Or is getting a unique echelon form just for row reduced echelon form?
Answer: You're right, the Echelon form is not unique, but RREF is unique. It happens because what you get is not computationally independent of the algorithm you use to compute it. More specifically, the rows need not be linearly independent. When you reduce to RREF, they are linearly independent. (sorry for editing so many times, I just wanted to be as clear as possible)
Comment: Reference: https://sites.math.washington.edu/~king/coursedir/m308w02/online/answers1-2.html

Echelon form of a matrix


How can I use Mockito to verify that any/none of a set of overloaded methods is called?

Question: I have a BarService with multiple related overloaded update methods on it, like so:
BarService#update(Bar bar);
BarService#update(Bar bar, boolean checkX);
BarService#update(Bar bar, boolean checkX, boolean checkY);

I want to be sure that none of them are called in a particular test. Currently, I'm doing this:
@Test
public test doFoo_shouldNotUpdateBar() {
    setUpTest();
    doFoo();
    verify(barService, never()).update(any(Bar.class)));
    verify(barService, never()).update(any(Bar.class), anyBoolean()));
    verify(barService, never()).update(any(Bar.class), anyBoolean(), anyBoolean()));
    ...
}

I can clean this up a bit by moving all the verify calls into a helper method to check all the overloaded versions:
@Test
public test doFoo_shouldNotUpdateBar() {
    setUpTest();
    doFoo();
    verifyBarServiceNeverUpdates(any(Bar.class));
}

But is there a way to get Mockito to do what I want here? Basically, to check that an overloaded method is / is not called without regard to its arguments?
Comment: I've proposed this as a [new feature](https://github.com/mockito/mockito/issues/2350)  for mockito.
Answer: You absolutely do not to manually put up "verification specs" that list methods that are not used (on such a scale as implied here). What happens when you add a new method tomorrow? Then you would have to turn to all your tests and add the new method there. That is not a sustainable pattern!
Instead:

verify these calls you intend to happen
and then call verifyNoMorInteractions() on your mock object to ensure that well, no other method was called
Comment: This is a viable option, but unfortunately it pollutes my test with a bunch of stuff I don't want. Let's say `barService` does `X` and `Y` before calling `update`. Now I have to explicitly `verify(barService).doX` and `verify(barService).doY` before I can call `verifyNoMoreInteractions(barService)`. But I don't actually know or care about `doX` and `doY` in this test, so they're just distractions.
Answer: If Bar is a Mock and you want absolutely NO interactions, you can use Mockito's verifyZeroInteractions method as follows;
Mockito.verifyZeroInteractions(bar);

Update:
As mentioned in the comments by @DanielPop, verifyZeroInteractions was marked as deprecated and it's no longer available. You should move to use verifyNoInteractions instead.
Comment: This would work but unfortunately I can't count on there being zero interactions; I just want to make sure no `update` method is ever called.
Comment: the method was renamed to `verifyNoInteractions`


Forwarding requests in Apache if necessary based on subdomain

Question: In Apache 2.2, is there a way to forward certain subdomains to another server (in another datacenter) based on a lookup table of subdomain/IP address mappings (provided in a file or database)?
The reason I need this is that I have a SaaS web app where each customer gets their own subdomain, i.e. customer1.mycompany.com, customer2.mycompany.com, etc.  If Apache receives a request for a customer whose data is not stored on the current server, I want Apache to simply forward (proxy?) the request on to the correct server.  The number of customers will be continually increasing (hopefully!) and I cannot reload Apache each time that happens.
UPDATE:
Our current idea is to use GeoIP DNS to send customers to the correct (nearest) server, and if a customer is accidentally sent to the wrong server (e.g. they are traveling), then we would need to do the proxying. However, even if we added a DNS entry for each subdomain, there could still be requests that end up at the wrong server when an account is first created, or when it is moved from one server to another. For various reasons, we don't want to set a really low TTL on the DNS entries.
Comment: Would pointing the DNS of customer50.mycompany.com to the server that does have their data work for you?
Comment: If you don't need a centralized control/access to your web app, you can simply insert a new DNS record for each new customer that points to the right server. That's it!
Comment: Christopher, @Khaled: That's true, and a possible alternative to our current idea of using GeoIP DNS. However, even in that case, there could still be requests that end up at the wrong server when an account is first created, or when it is moved from one server to another. For various reasons, we don't want to set a really low TTL. (updating question with this info)
Answer: One way to implement your idea to create your own index file (using PHP/perl or whatever language you prefer). The index page will simply query the DB/file mapping of domains and send redirect to the appropriate server. In this case, you don't need to worry about DNS caching.
Comment: Thanks.  The trouble with an HTTP redirect is that the redirect needs to be transparent.  That is, a request for `http://customer1.mycompany.com/rails_app/item1` shouldn't simply redirect to `http://18.104.22.168/rails_app/item1` or `http://customer1.mycompanyineurope.com/rails_app/item1`.  The URL needs to be the same no matter what server I hit.  That's why I was thinking a reverse proxy in Apache would be the only way to do this.


How to organize files from dependencies in a Maven shaded jar

Question: When you build a fat jar using the maven-shade-plugin, there might be conflicts between files at the same path but originating from different dependencies. Similarly, it becomes hard to distinguish the origin of various dependency-specific files located at the root of the individual dependencies (LICENSE.txt, native libraries, ...) when they are placed side-by-side at common locations in the shaded jar.
maven-shade-plugin itself and the similar jarjar-maven-plugin appear to provide functionality for automatically relocating classes, but not other files from the dependencies, such as those seen in the following example:
Example of current messy structure
/
  - NOTICE
  - magic.gz
  - LICENSE.txt
  - LICENSE
  - asm-license.txt
  / META-INF
    - LICENSE.TXT
    - about.html
  / com.some.library
    <class files>
  / com.some.other.library
    <class files>
  / com.some.third.library
    <class files>

This is an example from a project I just started. None of the files above were added by me, but all come from various popular third party libraries I'm using. I would like to clean up this mess by having Maven reorganize files in a common package structure like in the following example:
Example of desired clean structure
/
  / com.some.library
    <class files>
    - NOTICE
    - LICENSE
    / META-INF
      - about.html
  / com.some.other.library
    <class files>
    - magic.gz
    - LICENSE.txt
  / com.some.third.library
    <class files>
    - asm-license.txt
    / META-INF
      - LICENSE.TXT

I have many dependencies, so I don't consider writing dependency-specific filters/relocators/... with hard-coded filenames a good solution to this problem.
How do you organize all files from dependencies in your shaded jar automatically? Ideally by relocating all files from a dependency inside a subfolder named after the dependency.
Answer: This doesn't sound like a Maven best practice...but that being said one possible way to do this would be to create your own Maven plugin based on the maven-shade-plugin. The code base is quite straightforward. You could implement org.apache.maven.plugins.shade.resource.ResourceTransformer and call it in the org.apache.maven.plugins.shade.DefaultShader class when it enumerates each Jar & Jar entry (you could organize the resource by jar name, perhaps). If you want a basic implementation, I'll post one.
Comment: What is the Maven best practice then? Please feel free to provide an implementation if you feel like it could solve the problem.
Comment: Sorry for the delay - busy weekend. This would break jars that expect resources in the root directory. That being said, I forked the maven-shade-plugin and edited it to support root resource relocation. It's a very basic implementation, but I did unit test a simple case. Clone the repo here https://github.com/aoetalks/shadeplus-maven-plugin, and run mvn clean install on the shadeplus-maven-plugin project. There is an example project in shadeplus-maven-test-project, in which you can compare my plugin vs the original shade plugin. Let me know if it works for you.
Comment: Thanks! I will definitely check it out! Will report back afterwards.
Comment: I looked at your code. It appears like it only relocates files located in the root of the shaded .jar. What i was asking was "How do you organize *all* files from dependencies in your shaded jar automatically? Ideally by relocating all files from a dependency inside a subfolder named after the dependency."
Comment: My bad. I misread your request because of the description of root files behavior (I did find the relocation of only root resources rather odd :)) I'll make the chance when I get the chance and push it.
Comment: No problem. I added an example to my original post clarifying my issue.


Frobenius Method Equal Roots proof

Question: Can someone point me to a proof for the second solution of a DE using Frobenius method when the roots of the indicial equation are equal?
Comment: I think you should try to see its resemblence with Cauchy-Euler homogeneous equation.
Answer: Think of a Cauchy-Euler homogeneous equation $y''+(p/x)y'+(q/x^2)y=0$, where $p$ and $q$ are constants has a solution of the form $y=A.x^r+B.logx.x^r$ in case of equal roots.
Now, the more general form $y''+(\frac{p_0+p_1x+p_2x^2+...}{x}). y'+(\frac{q_0+q_1x+q_2x^2+...}{x^2}).y=0 $ must have a solution of the form $y=A.x^r(a_0+a_1.x+...)+B.logx.x^r(a_0+a_1.x+....)$ from which you can easily see that second solution can be obtained from the first solution by merely multiplying it with $logx$.

Frobenius Method Equal Roots proof


Issue about the Cauchy problem of separable variables differential equation

Question: Consider the Cauchy problem
$$\begin{cases}x'(t)=f(x) \\ x(0)=x_0\end{cases}$$
with $t\in \mathbb{R}$ and $f:I\subseteq\mathbb{R} \longrightarrow\mathbb{R}$.
If $f(x_0)=0$, then $x(t)=x_0$ is the solution of the equation. If $f(x_0)\neq 0$, then $f(x(t))\neq 0 \, \forall t$ in the maximal interval of definition of the solution, because the solution is unique.
I don't understand the bold part: supposing that $\exists t_1$ such that $f(x(t_1))=0$, what is the contradiction? I was thinking that, defining $x(t_1):=x_1$, if $x_1$ is a zero of $f$, then $x(t)=x_1$ can be another solution of the problem, which contradicts the unicity, but this is not true since $x(0)=x_1\neq x_0$, so $x(t)=x_1$ is not a solution of the problem. What am I missing?
Comment: You just showed the contradiction. You assumed there was a solution with $f(x(t_1))=0$ and showed that such a solution cannot be a solution with $x(0) = x_0$.
Comment: @whpowell96 I don't understand where is the contradiction: $x(t)=x_1$ is not a solution of the Cauchy problem with initial condition $x(0)=x_0$, but this does not prove that a solution with $f(x(t_1))=0$ and $x(0)=x_0$ does not exists. For example, why a nonconstant function $x$ with $x(0)=x_0$ and $t_1$ as a stationary point (so $x'(t_1)=0$) cannot exists in this context?
Comment: If $f(x(t_1))=0$ and $x_1 = x(t_1)$, then $x_1$ is a constant solution both forwards and backwards in time in its interval of existence. If $x(t)$ is constant with value $x_1$, then $x(0) = x_1\neq x_0$.
Comment: Basically, if a constant function is a solution with value $x_1$, then all solutions taking on that value must be constant by uniqueness
Comment: @whpowell96 The problem is that it seems to me that $x(t)=x_1$ is not a solution in the first place. I mean, it's a solution of the differential equation itself, but is not a solution of the given Cauchy problem since it doesn't match the initial condition, and since the uniqueness is relative to the Cauchy problem, the fact that is a solution of the differential equation but not of the Cauchy problem doesn't seems a contradiction to me.
Comment: It is a solution of a different Cauchy problem (with $x(0) = x_1\neq x_0$) and is hence not a solution of the Cauchy problem with $x(0) = x_0$ by uniqueness.
Comment: @whpowell96 Sorry, I still don't understand. Why the fact that $x_1$ is not a solution of the given Cauchy problem implies that cannot exist a solution with $x'(t_1)=0$ at all? How does the fact that $x_1$ is a solution of the differential equation above allow me to use the uniqueness property, which is relative to the Cauchy problem and not the equation itself?
Answer: If $x(t_1)=x_1$ and $f(x_1)=0$, then as you found also $\bar x(t)\equiv x_1$ is a solution.
Now you have the situation that both $x$ and $\bar x$ solve the Cauchy problem with initial condition $x(t_1)=x_1$. This is impossible. So you have to backtrack to the last assumption. Thus $x(t_1)=x_1$ or $\dot x(t_1)=f(x(t_1))=0$ is not possible. And if you want to force this, then $x(0)=x_0$ is not possible, but that was the problem to solve, so we can not throw out this condition.


rewrite flag [L] is not working

Question: This is my .htaccess file i just want to rewrite the page from one url to another.But it moved 404 page
RewriteEngine On
Options +FollowSymLinks
RewriteBase /
RewriteRule ^([^/]+)/article/([^/]+)$ /index.php?option=com_vendorsearch&view=article&title=$2 [L]
RewriteRule ^([^/]+)/polls/([^/]+)$ /index.php?option=com_vendorsearch&view=polls&id=$2 [L]
RewriteRule ^([^/]+)/coupons/([^/]+)$ /index.php?option=com_vendorsearch&view=coupons&id=$2 [L]
RewriteRule ^([^/]+)/images/([^/]+)$ /index.php?option=com_vendorsearch&view=images&id=$2 [L]
RewriteRule ^([^/]+)/videos/([^/]+)$ /index.php?option=com_vendorsearch&view=videos&id=$2 [L]

Comment: what is the name of the file you want to move and what is the url you want to point it to?  What have you tried, because you appear to just be showing us your full .htacceess file.
Comment: @miah actaully i created some views for the vendorsearch component.I want to show the url of that views as www.example.com/qwerty/articles/article-title.
Answer: Probably you need your first rules as:
RewriteRule ^[^/]+/articles/([^/]+)/?$ /index.php?option=com_vendorsearch&view=article&title=$1 [L,QSA,NC]

rewrite flag [L] is not working


Ruby on Rails and AWS S3 Expiring Download Link

Question: I am new to coding and I hope I explain this question correctly. Like most people I am starting my journey with Ruby on Rails.
I am trying to request a url from AWS S3 to pass on to the client browser that expires after 20 minutes. I have installed the aws-sdk gem and ran bundle install etc.
I have a welcome controller and an index view. I have the following code in my controller:
  class WelcomeController < ApplicationController

  def index
  end

  def doc_download

    require 'aws-sdk'

    s3 = Aws::S3::Client.new(
        access_key_id: 'mykey',
        secret_access_key: 'mykey'
    )

    aws_resource = Aws::S3::Resource::new
    aws_resource.bucket('mynewapp.herokuapp.com').object('1.jpg').presigned_url(:get, expires_in: 20.minutes)

    @download = doc_download

  end

end

Then this in my view

<%= link_to 'Download', @download %>

But nothing is happening, the page just refreshes when I click on the 'Download' link.
Any help greatly appreciated, spent a couple of days trying to figure it out from many of the different posts that have attempted to complete this.
Comment: Thanks for the reply! So like this @download = aws_resource.bucket('mynewapp.herokuapp.com').object('1.jpg').presigned_url(:get, expires_in: 20.minutes)
Comment: thanks, tried it but its the same, there is no error in the console, it seems to just render the link as the index view and refreshes the page
Answer: I also had a similar problem using the aws-sdk-s3 gem (version 1.8.2). In my case I had to upload a file to S3 and after that presenting an expiring URL to the user. My resulting code looked like that:
# Upload a file to S3 and return an expiring URL
# @param [IOStream] file
# @param [String] file_path
# @param optional [Integer] expiring_in_seconds (600)
# @returns [String] expiring and pre-signed URL
def upload_file(file, file_path, expiring_in_seconds = 600)

  # upload file to S3
  client = Aws::S3::Client.new
  client.put_object({
                        body: file,
                        bucket: ENV['S3_BUCKET_NAME'],
                        key: file_path
                    })

  # create an S3 object reference
  obj =  Aws::S3::Object.new(ENV['S3_BUCKET_NAME'], file_path)

  # create pre-signed url
  obj.presigned_url(:get, expires_in: expiring_in_seconds)
end

Keep in mind that I had the following AWS credentials and settings within my environment variables:

AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
AWS_REGION
S3_BUCKET_NAME
S3_SIGNATURE_VERSION
Answer: Thanks to Maxence for helping me along the way here, this is the code that works for me:
In my Gemfile
gem 'aws-sdk'

My Controller:
class WelcomeController < ApplicationController

  def index

    doc_download

  end

  def doc_download

    require 'aws-sdk'

    Aws.config.update(
        region: 'eu-west-2',
        endpoint: 'https://s3.eu-west-2.amazonaws.com',
        access_key_id: 'mykey',
        secret_access_key: 'mykey',
    )

    s3_client = Aws::S3::Client.new

    @download = Aws::S3::Object.new(
        key: 'myfilename', bucket_name: 'mybucketname', client: s3_client).presigned_url(:get, expires_in: 60 * 60
    )

  end

end

Then to display the link in my index view (from welcome controller)

<%= link_to 'Download', @download %>

Just to confirm functionality, this will output a secure expirable link to an s3 bucket for a user to download a file where they may need to pay for it first etc.
Answer: Not sure if it works as I am more familiar with the Paperclip way, but you can try this :
s3_client = Aws::S3::Client.new(
        access_key_id: 'mykey',
        secret_access_key: 'mykey'
    )

@download = puts Aws::S3::Object.new(
        key: '1.jpg',
        bucket_name: 'mynewapp.herokuapp.com',
        client: s3_client
).presigned_url(:get, expires_in: 60 * 60)

I got inspiration from here http://docs.ceph.com/docs/jewel/radosgw/s3/ruby/
Comment: Actually, I tweaked a couple of small things and now for this working! Thanks for the inspiration @Maxence! I will post what I changed a bit later today, it is currently printing to my rails console so will assign it to a variable and test the link. Thanks again!
Comment: Great that's good. I recommend some file upload gem like Paperclip, Carrierwave or Shrine, you won't have to deal with SDK no more.
Answer: I think this will now be provided with Rails 5.2 ActiveStorage  http://edgeguides.rubyonrails.org/active_storage_overview.html


using MARS regression on data with outliers - R

Question: In some previous asked questions, I was told to not delete the outliers, because they contain valuable information.
After testing different regression, I came to the conclusion that until now, the MARS regression delivers the "best responses".
I know that MARS is very robust and there is no a priori knowledge about the data distribution needed.
But there are some question which I have about the parameters.
I'm using the earth function implemented in R
data set:
file
So I've got 5 variables, price, livingArea, area, discrete, dummy and I'm trying to explain price using the other ones.

as you can see, there are some outliers and a log doesn't really solve the problem.
Due to the fact that area can be null, a log won't be a good transformation idea.
what I do:
Because the answers from other questions suggested to use the raw data,
I'm running now the regression through my data without doing any changes to it.
so my regression formula looks like this:
earth(price ~ ., data = data[,-1], weights = weights, penalty = -1)

I'm setting penalty = -1 because I saw that doing this, the method defines more knots and also the results look better.
Also I tried to define the variables discrete and dummy as factors and use them as follows in the regression:

independent
livingArea * discrete or livingArea : discrete and dummy as independent
the same as at 3. but changing discrete with dummy
livingArea * discrete * dummy

I must say that I didn't expect, that a regression with this variables as factors, will return such "bad" results.
what I want:
I want to use the model in order to predict the value of new data.
    livingArea area discrete dummy
1         87    0        7    0.5

The prediction of this observation should be ~ 330000, but with what I'm doing now, I ain't coming not even close to this value.
I think that, having more knots increases the precision of the result.
questions:

I don't really understand the parameters
I created my model with different values for the pmethod, but the result was always the same. what's the point in choosing a method when the result will be the same?
how could I determine if I have to set/change the values of different parameters like thresh, minspan, nk, etc.
Answer: This isn't what you ask for, but I think it's pertinent to analysing the data and it won't all fit easily into comments.

What does area mean if some values are 0? As a matter of interest I found that area and square root of area don't help much any way. That matches the evidence of the graphs in the question.
The variable dummy is not (0, 1), so I don't know how to think about it, but it doesn't help much either.

Following earlier suggestions I fitted a generalised linear model with log link. The choice of family seems fairly unimportant, but I haven't explored much. These results are from Stata. Two predictors seem strongly confirmed, but the overall fit is not strong.
. glm price log_livingarea discrete, link(log)

Iteration 0:   log likelihood = -3093.2279
Iteration 1:   log likelihood = -3078.9355
Iteration 2:   log likelihood = -3077.8055
Iteration 3:   log likelihood = -3077.8052
Iteration 4:   log likelihood = -3077.8052

Generalized linear models                         No. of obs      =        238
Optimization     : ML                             Residual df     =        235
                                                  Scale parameter =   1.01e+10
Deviance         =  2.38043e+12                   (1/df) Deviance =   1.01e+10
Pearson          =  2.38043e+12                   (1/df) Pearson  =   1.01e+10

Variance function: V(u) = 1                       [Gaussian]
Link function    : g(u) = ln(u)                   [Log]

                                                  AIC             =   25.88912
Log likelihood   = -3077.805214                   BIC             =   2.38e+12

--------------------------------------------------------------------------------
               |                 OIM
         price |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
---------------+----------------------------------------------------------------
log_livingarea |   .6915059   .0888826     7.78   0.000     .5172992    .8657126
      discrete |   .1974758   .0247692     7.97   0.000      .148929    .2460226
         _cons |   7.811162   .4801921    16.27   0.000     6.870003    8.752321
--------------------------------------------------------------------------------

I am not surprised that the model is not a spectacularly good fit. There are many possible predictors of house prices that are not in your data.
Comment: 1. `area` is the `lot area`. a house will always have a `lot area (garden, yard, etc.)` but an apartment will not always have a `lot area`. Of course that I'm predicting apartments out of apartment data and house out of houses data. 2. yeah I know that `dummy` is between `(0,1)` but I just hadn't a better idea for naming it. The thing is that I had a lot of variables and after working with 12 predictors for ~7-8 months, I saw that combining this predictors and reducing the dimension of the data works fine. Maybe this is not a good statistical approach, but it works.
Comment: the variable `discrete` is build out of 2 discrete variables and the variable `dummy` is build out of 4 dummy variables. `sum(dummies)/4` and that's why it not `0 or 1`.
Comment: I must say that in 85% of the I have a nice data with a few outliers and a robust regression can handle it perfect, but in some cases (like this one) the data is terrible and also is the result.
Comment: Thanks for the detail. As a matter of strategy, I would disentangle the `discrete` and `dummy` variables all the way back to the originals. Fit with all predictors, then slim down the model. The problem for anyone else with what you have done is that they cannot interpret those variables, which are just arbitrary composites.
Comment: Messy data are just part of the job: you do the best you can and move on.
Comment: I also decided to let go variables like `roomCount`, `bathRoomCount`, etc because those are mostly user preferences and they make the model complex. For example if there is an apartment with `livingArea = 90m²`, some people will prefer it with one room (loft) and other with 2 rooms, other with 3 rooms.
Comment: Sure, but in many problems like this, good predictors are hard to find. I don't worry about the complexity of the models I try if I know that I can slim them down.
Comment: my idea behind modelling/transforming the data like this, was to make it in such a way that an increase in any of the predictors should generate an increase in the price.
Comment: what makes my work ever worse is the fact that everything must be automatic calculated and predicted. The program runs, and he receives the data and must return the prediction. So I don't really have the luxury to do it manually. I'm doing manually only those regression which are very bad, in order to discover the problem and try to eliminate it.


setState not work in react-bootstrap modal

Question: When i want to use setState, it will not work. for example close or open bootstrap modal.
Here is the my index.js contents:
import App from "./Modal";

ReactDOM.render(<App />, document.getElementById("root"));

And here is my Modal.js contents:
import React, { Component } from "react";
import socket from "socket.io-client";
import Modal from "react-bootstrap/Modal";

const Socket = socket.connect("ws://127.0.0.1:3004");

class Parent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      show: false
    };
    this.onChange = this.onChange.bind(this);
  }
  onChange(event, type) {
    this.setState({ show: false });
  }
  render() {
    const { show } = this.state;
    return <Child onChange={this.onChange.bind(this, show)} />;
  }
}

class Child extends Component {
  _isMounted = false;
  constructor(props) {
    super(props);
    this.state = {
      show: false
    };
    this.handleShow = this.handleShow.bind(this);
    this.handleClose = this.handleClose.bind(this);
  }
  handleShow() {
    if (this._isMounted) {
      this.setState({ show: true });
      this.props.onChange(true, true);
    }
  }
  handleClose() {
    if (this._isMounted) {
      this.setState({ show: false });
      this.props.onChange(false, false);
    }
  }
  modal(response) {
    if (this._isMounted) {
      this.setState({ show: true });
      this.props.onChange(true, true);
    }
  }
  componentDidMount() {
    this._isMounted = true;
    Socket.on("message", data => this.modal(data));
  }
  render() {
    return (
      <>
        <Modal
          size="lg"
          show={this.handleShow}
          onHide={this.handleClose}
          aria-labelledby="st-lg-modal"
        >
          <Modal.Header closeButton>
            <Modal.Title id="st-lg-modal">Modal Name</Modal.Title>
          </Modal.Header>
          <Modal.Body>Contents</Modal.Body>
        </Modal>
      </>
    );
  }
}

export default Parent;

But setState is not work.
Take a look to my example:
https://codesandbox.io/s/epic-herschel-344dl
Demo:
https://344dl.csb.app/
Close button not work !
How can fix this ?
Answer: Some notice points:

React-bootstrap Modal props show is receiving boolean type value, rather than a callback function.
You don't need to write the handler functions inside your child if you want to reuse it. As well as there is no need for _isMounted.
Use public class field syntax (arrow function) instead of bind(this) can reduce your code and lead to better performance.

The modal now can be closed normally.
import React, { Component } from "react";
import socket from "socket.io-client";
import Modal from "react-bootstrap/Modal";

const Socket = socket.connect("ws://127.0.0.1:3004");

class Parent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      show: true
    };
  }
  onChange = () => {
    this.setState(perv => { show: !prev.show });
  };
  render() {
    const { show } = this.state;
    return <Child onChange={this.onChange} show={show} />;
  }
}

class Child extends Component {
  constructor(props) {
    super(props);
    this.state = {};
  }
  componentDidMount() {
    Socket.on("message", data => this.modal(data));
  }
  render() {
    const { show, onChange } = this.props;
    return (
      <>
        <Modal
          size="lg"
          show={show}
          onHide={onChange}
          aria-labelledby="st-lg-modal"
        >
          <Modal.Header closeButton>
            <Modal.Title id="st-lg-modal">Modal Name</Modal.Title>
          </Modal.Header>
          <Modal.Body>Contents</Modal.Body>
        </Modal>
      </>
    );
  }
}

export default Parent;