why isn't Thread class final? Why would you extend a Thread ever?

Question: I got this as an interview question.

why isn't Thread class final? Why would you extend a Thread ever?

I could not come up with real world use cases.
Comment: Note: Note, since JDK 1.5, the recommended way is to use Executors, rather than Threads directly:
http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executor.html
http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executors.html
So usually, you should not only not subclass Thread, you also shouldn't even create a Thread instance directly.
Answer: From Oracle's documentation:

There are two ways to create a new thread of execution. One is to declare a class to be a subclass of Thread. This subclass should override the run method of class Thread. The other way to create a thread is to declare a class that implements the Runnable interface.

So the answer is "you may want to subclass Thread to override its run() method."
The quoted paragraphs have been in the Java documentation going back as far as JDK 1.1. Java has added other convenient classes for managing concurrency, most notably, the executors mentioned in the comments, possibly diminishing or eliminating the need to extend Thread. They cannot make it final, however, because that would break backward compatibility.
As far as practical reasons go, I think the only reason you may want to extend Thread rather than implement Runnable today would be to override its methods other than run(). For example, you may want to add logging or additional clean-up.
Comment: I have found that overriding interrupt() can also be useful if the Thread is executing IO (ie close a socket to interrupt reading/writing).
Answer: This is pretty much just taken from John Vint's comment, but I think it's the best answer.
The only time I can think of where I might extend Thread rather than implementing Runnable -- or, even better, just using an ExecutorService with a Future -- is when I needed to override Thread.interrupt() to do some cleanup.  Otherwise, I can't see any practical reason to actually extend Thread.
Answer: Two cases:

To create a new kind of Thread, perhaps one that cleans up some resource after finishing, etc
To override the run() method, rather than providing a Runnable to the constructor (note: avoid this pattern - it's not the right approach)
Answer: Another reason that Thread is not final is that in the early days of Java, overriding run() was considered to be a good design pattern.  (I guess, in the days before anonymous classes, it was thought to be "neater" to subclass Thread than to create a free-standing class that implements Runnable.)
Anyway, once Java 1.0 was released it because impossible to fix the problem by changing Thread to be final.  That would have broken a lot of existing code.
Answer: Let me put this in the following way: When designing a language, which is a tool like any other, one should think in terms of pros and cons and not just limit the tool because we aren't able to see applicable real use cases. By doing this we are able to understand such decisions theoretically. Basically, the exercise is asking the other way round.

Why Thread should be final?

Let me get to the point.

(I'm not including the backward compatibility argument, personally, I don't like it, as I think we should always be focused on improving locally)
To be able to discuss such subject we need to understand the benefits of declaring a class as final or not.
Performance
Here TofuBeer said:

Virtual (overridden) methods generally are implemented via some sort
  of table (vtable) that is ultimately a function pointer. Each method
  call has the overhead of having to go through that pointer. When
  classes are marked final then all of the methods cannot be overridden
  and the use of a table is not needed anymore - this it is faster.
Some VMs (like HotSpot) may do things more intelligently and know when
  methods are/are not overridden and generate faster code as
  appropriate.

Security
Here oracle highlights:

It is better to design APIs with security in mind. Trying to retrofit
  security into an existing API is more difficult and error prone. For
  example, making a class final prevents a malicious subclass from
  adding finalizers, cloning, and overriding random methods.

There is also issues with invariants and sensitive information.
However, all security concerns are project specific and not applicable to a tool which can be used in non-security-concerned scenarios.
Convenience
andersoj referred here this nice IBM article:

final classes and methods can be a significant inconvenience when
  programming -- they limit your options for reusing existing code and
  extending the functionality of existing classes. While sometimes a
  class is made final for a good reason, such as to enforce
  immutability, the benefits of using final should outweigh the
  inconvenience. Performance enhancement is almost always a bad reason
  to compromise good object-oriented design principles, and when the
  performance enhancement is small or nonexistent, this is a bad
  trade-off indeed.

So, from my point of view, if there is no strong benefit to set the class as final, if JVM's are capable of doing such performance optimisations, I would say the convenient argument wins.
As so, we can extend the Thread class and do whatever we want with it, some initialization/finalization stuff, like logging, change the thread names, the type of thread... It up to you!
Answer: It may be fields to hold the thread-local variables in the derived Thread class. Such variables can be accessible also when run() methods is not overridden and the thread executes Runnables as usual. Such custom threads may be managed by the standard ExecutorService, creating them in the custom ThreadFactory and discarding as required.
I am aware there is also ThreadLocal, but if the fields require cleanup  (say they are the database connections), this can be rather elegantly done by calling super.run() to process Runnables and putting finalization immediately before the run method returns (so the thread terminates).


Timeout crash on launch (0x8badf00d) due to com.apple.locationd.registration.xpcq in iPhone app

Question: I am getting a weird timeout crash via iTunes during app launch. Here is the stack trace. Seems like something to do with the location service (yes, I do ask for a user's location). Does anyone have any experience with such a crash?
<code>Thread 10 name:  Dispatch queue: com.apple.locationd.registration.xpcq
Thread 10:
0   libsystem_kernel.dylib          0x313dd2c8 semaphore_timedwait_trap
1   libdispatch.dylib               0x322db0e6 _dispatch_semaphore_wait_slow
2   libdispatch.dylib               0x322db154 dispatch_semaphore_wait$VARIANT$up
3   CoreLocation                    0x349cd838 CLClientInvokeCallback(__CLClient*, CLClientEvent, __CFDictionary const*)
4   CoreLocation                    0x349cffc0 ___CLClientCreateConnection_block_invoke_0
5   CoreLocation                    0x34a00c06 CLConnection::handleMessage(CLConnectionMessage*)
6   CoreLocation                    0x34a01a14 __setEventHandler_block_invoke_0
7   libxpc.dylib                    0x349c190a __XPC_CONNECTION_FAULT_BOUNDARY__
8   libxpc.dylib                    0x349badae _xpc_connection_recv_message
9   libxpc.dylib                    0x349bab5e _xpc_connection_wakeup_recv
10  libxpc.dylib                    0x349baa24 _xpc_connection_wakeup2
11  libxpc.dylib                    0x349ba462 _xpc_connection_wakeup
12  libdispatch.dylib               0x322dc21e _dispatch_source_invoke
13  libdispatch.dylib               0x322d9b70 _dispatch_queue_invoke$VARIANT$up
14  libdispatch.dylib               0x322d9caa _dispatch_queue_drain
15  libdispatch.dylib               0x322d9b66 _dispatch_queue_invoke$VARIANT$up
16  libdispatch.dylib               0x322d9caa _dispatch_queue_drain
17  libdispatch.dylib               0x322d9b66 _dispatch_queue_invoke$VARIANT$up
18  libdispatch.dylib               0x322da76c _dispatch_worker_thread2
19  libsystem_c.dylib               0x3351d4b0 _pthread_wqthread
20  libsystem_c.dylib               0x3351d384 start_wqthread
</code>
Answer: I had a similar issue. I resolved it by rebooting my iOS device entirely.
I believe the issue is that the remote debug server is in an unexpected state and causing the app to break the running application (libxpc is involved in remote debugging I believe). As a result the best way to clean this up is to simply reboot.
Comment: I encountered the same thing, but I'm not running under the debugger.  This is on the device, untethered.


Access objects in custom UITableViewCells with same tag?

Question: In my app, I use custom UITableViewCells loaded from a XIB. Each cell contains 4 objects, like a UIImageView, UILabel, UITextField, etc...
I set the tag of each object in my cellforrowatindexpath so each object has the same tag which also is the row of the cell in the tableview.
The problem is, how am I suppose to access these objects in a cell if they all have the same tag? How would I properly access these objects in a method that doesn't have a NSIndexPath parameter?
Thanks!
Answer: I personally try avoiding the use of tags as I usually overuse them to the point of utter confusion and have begun finding more elegant ways around it. For instance, in your situation I would create IBOutlets for the custom UITableViewCell's objects making them more semantically relevant. However, your situation might be different - there is not enough in the question for me to say this would definitely work for you.
That said, it sounds like you could accomplish what you're trying to do by:

Giving each cell returned by the cellForRowAtIndexPath method a tag (before returning it)
Giving each of the objects within the cell a tag, as you seem to have already done

That way you can access the cell's objects (presumably UIViews) from (anywhere) in your UITableViewController by:
UITableViewCell *cell = (UITableViewCell *)[self.tableview viewWithTag:CELL_TAG];
UIView *objectInCell = [cell viewWithTag:OBJECT_TAG];

Keep in mind that this would allow you to access an object in a specific cell. If you were trying to access the objects of all your cells, you could simply iterate through all of them. However, I sense that  this would start becoming unnecessarily complicated at this point. It would help if you posted up a bit more information on what exactly you were trying to achieve by utilising the tagging this way. Hope this helps in the meantime.
Comment: Yes most of what you said is right but lets say in one cell I have 4 objects with the tag of 3 is it possible to just get the object by casting the type before I do viewWithTag (they are all different objects).
Comment: That doesn't sound possible, since you will only ever retrieve a single `UIView` with the `viewWithTag` method. I haven't tried this out myself, but based on [this question](http://stackoverflow.com/questions/1978720/what-uiview-is-returned-when-using-viewwithtag-when-several-views-have-same-t), it seems that you will get the view you added first (with the tag 3). You can check the type with `isKindOfClass`, but you won't be able to access the other 3 objects. So in a nutshell, no, it's not possible. You should only use tags to specifically refer to a single other `UIView`.
Comment: Pretty much in the end I want to give EACH object in EACH row a unique tag so I can access it easily. Is this possible, and if so, how?
Comment: I'm beginning to understand what you're trying to achieve. Assuming you're trying to access these objects (essentially `UIView`s of the `UITableViewCell`) from the `UITableViewController`, it would not matter if each object in each row has a unique tag, because they will still only be accessed within the scope of the `UITableViewCell` itself. Going back to my initial answer, you would first need to get a reference to the `UITableViewCell` that has the object you're looking for and then call another `viewWithTag` on that to retrieve the specific object.
Comment: In other words, you will still not be able to do a `[tableView viewWithTag:objectTag]` as the `objectTag` is only understood (and valid) when you call `[cell viewWithTag:objectTag]`. So you will only be able to do a `[tableView viewWithTag:cellTag]`, unless you subview the `cell` objects directly under the `tableView` - which then makes using a `UITableView` redundant in this case.
Comment: So your saying that these objects can have the same tag but as long as they are in different UITableViewCells, correct?
Comment: Also since my UITableViewCells are custom I would do [myCell viewWithTag:objectTag]; etc.. Would that work?
Comment: Yes - you could give them all unique tags, but it would not matter as they're only 'defined' within the scope of the `UITableViewCell` that they are subview'd under.
Comment: Yes it would. `[myCell viewWithTag:objectTag]` would work. May I ask why you unaccepted my answer after I had elaborated it further?
Comment: Sorry, re-accepted. So bottom line here, set tags 1-4 on each object in my cells. Then I would just get the certain cell I need, then just access it with numbers 1-4 right?
Comment: Yup, that's right. However, going back to your initial question (where you specified that you want to do this in a method that doesn't have the `NSIndexPath` parameter available) - you will still need to know the `tag` or `rowIndex` of the cell to be able to get it.
Comment: Hmm thats where it gets complicated. The thing is though, I cannot get the index path easily because the user is actually not tapping on the cell but instead on a UITextField so I am not sure which UITextField it is, what should be my logistic in this scenario?
Comment: Well, assuming the `UITextField` is one of those 4 objects in each cell, you can get a reference to your `MyCell` object with `MyCell *myCell = (MyCell *)[textField superView]`. Then, you could either get another object in that cell with `[myCell viewWithTag:objectTag]`, or (if you need it) determine the `NSIndexPath` with `[tableView indexPathForCell:myCell]`.
Comment: Thanks! You have helped me out so much!
Answer: No idea why one would number all the tags in a row with the same number, but I suppose you have your reasons ([cell1 viewwithTag:0] and [cell2 viewWithTag: 0] should be more than enough to differentiate them.)
Giving all the elements the same tag generally defeats the purpose of tagging them in the first place, though if you MUST include the row number in the tag (and have some idea about the size of the result set), you could tag each element with a ((multiple of size) + item# ) integer (e.g., if you know the table will hold fewer than 10,0000 results, you could tag each element as 10001, 10002, 10003 and 10004 in the first row, and 20001, 20002, 20003 and 20004 in the next, etc... Then to address the third element in the 4th row, you would use [view withTag: 40003].
Comment: Im just wondering the best way to differentiate the objects in a cell row. What do you think?
Comment: I'm not sure I understand the problem. If each object in a row has its own unique number 0 to 3 (instead of giving them all the same number), differentiating them with viewWithTag: should not be an issue. Why exactly are you assigning all the views the same tag?
Comment: The problem is actually setting the values to different values so that I can access ALL the objects in the cell. I could just do tag 1000 etc... But I would rather not, is there any clear way to do it?
Comment: How are you setting the tags currently -- through code? Or through IB? I'm just unclear as to why you're setting them all the same in the first place, when numbering them 1 through 4 should allow you to access each within the scope of the parent cell. I.e., why are you using the row number as a tag at all?
Comment: Through code. I am not sure how to set them all differently in each row. I am using row number currently but it seems that, that is not the right way to do it. Can you show me how to give each object in each cell a unique number so I can access it easily?
Answer: You can access the row of the selected cell with this methode:
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]

but i am not sure if this answers your question. Please be more concrete.
Comment: person is asking about tags inside a tableviewcell, not how to figure out which cell is at an index


await Task.WhenAll como executar vários processos?

Question: Estou tentando criar diversas task (async) elas irão executar a seguinte lógica:

Fazer o Parse de um Html com base em uma url recebida com o HtmlAgilityPack
Retornar uma model produto após o parse
Inserir o Produto no database
Fazer download das imagens do produto
Marcar url como lida

os itens 1 e 4, principalmente o 4 demoram por causa da velocidade do link da internet, então por isso eles devem ser async.
Porém estou com dificuldades, todo meu código roda, porém de forma sincrona.
 private static void Main(string[] args)
{
     IEnumerable<UrlsProdutos> registros = db.UrlsTable.Where(w => w.Lido == false).Take(1000);

  ExecutaTarefasAsync(registros).Wait();
}

  public static async Task ExecutaTarefasAsync(IEnumerable<UrlsProdutos> registros)
        {
            var urlTasks = registros.Select((registro, index) =>
            {
                Task downloadTask = default(Task);

                //parsing html
                var produtoTask =  ExtraiDados.ParseHtml(registro.Url);
                if (produtoTask.IsCompleted)
                {
                    var produto = produtoTask.Result;
                    //aqui faço um insert com Dapper
                    downloadTask = InsertAdo.InsertAdoStpAsync(produto);
                }

                //marca url como lida, igual ao insert do produto
                InsertAdo.MarcaComoLido(registro.UrlProdutoId);

                Output(index);

                return downloadTask;
            });

            await Task.WhenAll(urlTasks);
        }

        public static void Output(int id)
        {
            Console.WriteLine($"Executando {id.ToString()}");
        }

O insert fiz um fixo só para testar
public static async Task InsertAdoStpAsync(Imovel imovel)
{
    var stringConnection = db.Database.Connection.ConnectionString;
    var con = new SqlConnection(stringConnection);
    var sqlQuery = "insert tblProdutos...etc..etc"
    con.ExecuteAsync(sqlQuery);
}

Não sei se cada função deve ser async. ou se eu poderia selecionar tipo o Download e o parse ser async..
O meu sistema de download de fotos async funciona perfeitamente.
  public static async Task DownloadData(IEnumerable<FotosProdutos> urls)
        {
            var urlTasks = urls.Select((url, index) =>
            {
                var filename = "";

                var wc = new WebClient();
                string path = "C:\teste\" + url.FileName;

                var downloadTask = wc.DownloadFileTaskAsync(new Uri(url), path);
                return downloadTask;
            });

            await Task.WhenAll(urlTasks);
        }

Preciso de ajuda para fazer e entender como o ExecutaTarefasAsync ser realmente async igual ao fotos que hj nem consegui ainda incorporar nesse projeto.
OBS: Não sei se o download das fotos faço lá no parse ou se coloco nessa task.
Answer: Um ponto que eu sempre reforço em questões sobre async/await: ele não torna a execução de um método assíncrona por si só, apenas permite ao programador escrever métodos em um fluxo de execução próximo ao que seria escrito para métodos síncronos. A grande questão, é que async/await sinaliza ao compilador que ao encontrar um await, irá esperar até que a execução seja finalizada, mas sem bloquear a Thread principal (Thead de UI em Windows Forms ou Thread do pipeline do IIS em aplicações web, por exemplo).
Sobre o seu caso em específico, o uso de async/await não está correto. Quando um método é async, apesar de você dizer que ele retorna um Task, não significa que quem usa ele necessita pegar essa Task como um retorno. Ao utilizar o await em sua chamada, você já está dizendo ao compilador que ele deve esperar a execução do método. Não sei se consegui explicar de maneira clara, mas acho que com o exemplo vai dar pra entender essa dinâmica.
Para tornar a execução realmente assíncrona, em questão de fazer com que cada registro execute em Threads diferentes de forma "paralela", eu fiz o seguinte:
// fiz as classes aqui só pra conseguir executar e mostrar um caso de execução com 5 segundos de duração pra cada chamada

public class FotosProdutos
{
    public string FileName { get; set; }
    public string Url { get; set; }
}

public class UrlsProdutos
{
    public int UrlProdutoId { get; set; }
    public string Url { get; set; }
}

public class Imovel
{

}

public class InsertAdo
{
    public static async Task InsertAdoStpAsync(Imovel imovel, int index)
    {
        await Task.Delay(TimeSpan.FromSeconds(5));

        Console.WriteLine(String.Format("{0} - InsertAdoStpAsync {1}", DateTime.Now, index));
    }

    public static async Task MarcaComoLido(int urlProdutoId, int index)
    {
        await Task.Delay(TimeSpan.FromSeconds(5));

        Console.WriteLine(String.Format("{0} - MarcaComoLido {1}", DateTime.Now, index));
    }
}

public class ExtraiDados
{
    public static async Task<Imovel> ParseHtml(string url, int index)
    {
        await Task.Delay(TimeSpan.FromSeconds(5));

        Console.WriteLine(String.Format("{0} - ParseHtml {1}", DateTime.Now, index));

        return new Imovel();
    }
}

class Program
{
    static void Main(string[] args)
    {
        // coloquei qualquer coisa aqui só pra eu conseguir reproduzir sem a sua dependência de Dapper
        IEnumerable<UrlsProdutos> registros = new List<UrlsProdutos>() { new UrlsProdutos { UrlProdutoId = 1 }, new UrlsProdutos { UrlProdutoId = 2 }, new UrlsProdutos { UrlProdutoId = 3 } };

        // Roda uma Task diferente pra cada registro.
        // Do jeito que você estava fazendo, sem o Task.Run(), acontecia basicamente a mesma coisa que um loop for executando item a item sincronamente a sua coleção
        var tarefas = registros.Select((registro, index) =>
        {
            return Task.Run(async () => await ExecutaTarefaAsync(registro, index));
        });

        Task.WaitAll(tarefas.ToArray());

        // espera mais um pouco só pra vermos uma diferença até o log de fim
        Task.Delay(TimeSpan.FromSeconds(5));

        Console.WriteLine(String.Format("{0} - Acabou!", DateTime.Now));

        Console.Read();
    }

    // Mudei o seu método pra se referir apenas a um registro só pra ser mais didático
    public static async Task ExecutaTarefaAsync(UrlsProdutos registro, int index)
    {
        Output(index);

        // chama o seu método de parse, falando pra esperar tudo o que tiver de assíncrono nele, e pega o retorno logo em seguida
        var produto = await ExtraiDados.ParseHtml(registro.Url, index);

        // como o seu parse já acabou, insere o registro com o resultado dele
        await InsertAdo.InsertAdoStpAsync(produto, index);

        // por fim, marcar todo mundo como lido
        await InsertAdo.MarcaComoLido(registro.UrlProdutoId, index);

        Output(index);
    }

    // não usei, mas mudei ele pra você ver a questão do await
    public static async Task DownloadData(FotosProdutos url, int index)
    {
        var wc = new WebClient();
        string path = @"C:\teste\" + url.FileName;

        // aqui você não precisa pegar a Task, ao usar o await ele já entende que você quer esperar o resultado do método async pra prosseguir na execução do método
        await wc.DownloadFileTaskAsync(new Uri(url.Url), path);

        Console.WriteLine(String.Format("{0} - DownloadData {1}", DateTime.Now, index.ToString()));
    }

    public static void Output(int id)
    {
        Console.WriteLine(String.Format("{0} - Executando {1}", DateTime.Now, id.ToString()));
    }
}

Ao executar esse exemplo, tive a seguinte saída:
13/10/2016 00:56:27 - Executando 1
13/10/2016 00:56:27 - Executando 2
13/10/2016 00:56:27 - Executando 0
13/10/2016 00:56:32 - ParseHtml 1
13/10/2016 00:56:32 - ParseHtml 2
13/10/2016 00:56:32 - ParseHtml 0
13/10/2016 00:56:37 - InsertAdoStpAsync 0
13/10/2016 00:56:37 - InsertAdoStpAsync 1
13/10/2016 00:56:37 - InsertAdoStpAsync 2
13/10/2016 00:56:42 - MarcaComoLido 2
13/10/2016 00:56:42 - MarcaComoLido 1
13/10/2016 00:56:42 - Executando 2
13/10/2016 00:56:42 - Executando 1
13/10/2016 00:56:42 - MarcaComoLido 0
13/10/2016 00:56:42 - Executando 0
13/10/2016 00:56:42 - Acabou!

Ou seja: como cada método leva 5 segundos pra executar, podemos observar que ele criou uma Thread diferente pra cada registro.
Comment: @Dorathoto como você falou que faz parte dos seus passos fazer o download baseado no seu parse, eu colocaria uma chamada dentro do ExecutaTarefaAsync().

Como o processamento de cada imagem parece ser independente, eu testaria a diferença entre executar a lista em paralelo ou um a um pra ver em qual a aplicação renderia melhor.
Comment: vou reproduzir seu código aqui, mas de ante-mão obrigado pela aula!, muito bom, bem detalhado...
Comment: só uma dúvida o DownloadData, qual seria o local correto? no parse produtos? ou lá na raiz no program.cs no executar tarefas? pq ele é o que mais demora e ele só pode ser executado depois que o parseHtml é concluido e retorna o model produto.
Answer: Para fazer uma funcao assincrona, voce utiliza a Task, como voce ja descobriu. O jeito certo de fazer algo desse tipo é assim:
public static Task MakeRequest(int i) {

    return Task.Run(() => {

       // seu codigo aqui
    });

}

public static void Main(string[]) {

    var tasks = new List<Task>();

    for (int i = 0; i < 10; i++)
    {
        tasks.Add(MakeRequestAsync(i));
    }

    // Aguarda todos MakeRequestAsync terminarem.
    Task.WaitAll(tasks.ToArray());
}

Dessa maneira, quando voce chamar MakeRequestAsync voce pode usar o await como é esperado com métodos assíncronos:
var resp = await MakeRequestAsync(i);

Vale a pena dizer também que uma Task não dá garantias nenhuma de quando vai ser executada: pode tanto começar a ser executada imediatamente quanto ficar na fila.
Outra coisa importante é entender a diferença de Task.WhenAll e Task.WaitAll.
Task.WhenAll retorna uma outra Task que você pode esperar (await) no momento em que for interessante, e o código continua rodando, ou seja, é uma função "não bloqueante". Já a Task.WaitAll vai parar o fluxo do código até que todas Tasks sejam executadas.
Resumindo: seu método que deve ser assíncrono deve, na verdade, disparar uma Task e retornar um objeto awaitable. A maneira mais fácil de se fazer isso é utilizando Task.Run.


Can configuration of Log4j be changed on the fly?

Question: I need to know if configuration changes on log4j like changing the logging level from INFO to DEBUG etc ... can/do effect the application on the fly?
Thanks.
Answer: It might, see http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PropertyConfigurator.html#configureAndWatch%28java.lang.String,%20long%29.
This requires changing your log4j initialization code since default initialization does not watch for changes (follow the code in line 127 in http://grepcode.com/file/repo1.maven.org/maven2/log4j/log4j/1.2.16/org/apache/log4j/LogManager.java to see it)
Answer: Yes. You may programmatically add/remove appenders, change logging levels etc. And yes, they take effect on the fly.
Comment: ... but changing the logging config file won't have any effect, unless you have some extra infrastructure that notices the file and reruns the logger configuration process.
Comment: maybe I'm misreading, but that doesn't appear to be what he's asking
Comment: The point is that the question just says "on the fly" without saying how he intends to implement the on the fly change.  It is possible that the OP is thinking of modifying the properties file to make this happen.  (And it appears my comment could have triggered Oded Peer to tell us that it is possible to do this in HIS answer.  That's a good outcome IMO.)
Answer: It does not support this behaviour by default but seems to be configurable with the api. Another solution could be Logback. It is the successor to Log4j and does support automatic reloading of configuration files out of the box. See http://logback.qos.ch/reasonsToSwitch.html for more info about this and other reasons to prefer logback over log4j.
Comment: Also, Log4j version 2  also supports dynamic config changes with a 30-second listener configured in the log4j.properties itself.
Answer: Log4j version 2 has a monitorInterval arg option for the log4j.properties file.


html javascript collapse accordion

Question: i have an accordion inside of a side navigation window. If I open all the panels they stay open if i close the side navigation panel. I want my accordion to collapse when my side navigation closes.
my code is attached.

<!DOCTYPE html>

  ×
  <button class="accordion">Section 1</button>

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

<button class="accordion">Section 2</button>

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

<button class="accordion">Section 3</button>

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

  ☰ open

Comment: can anybody help?
Answer: You can create a function that opens/closes panels, and in closeNav() just loop through the panels and close them all.

<!DOCTYPE html>

    ×
    <button class="accordion">Section 1</button>

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

    <button class="accordion">Section 2</button>

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

    <button class="accordion">Section 3</button>

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

    ☰ open

html javascript collapse accordion


Can I use conditions directly to assign a variable in C

Question: bool bVal;
int a = 5;
int b = 2;

bVal = (bool)a>b;

Is the statement bVal =a>b; acceptable. Of course, this is working fine for me.
But I see people using something like this:
#define CHECK_bIsTrue(value)  ((value) ? TRUE : FALSE)

Why do we have use ternary operator if it are just boolean values?
Comment: `bVal = (bool)a>b;` is wrong (and will give the wrong result here), but `bVal = a>b;` is correct.
Comment: `bVal = (bool)a>b;` means `bVal = ((bool)a)>b;` so it won't return the correct result.
Comment: I think the `#define` macro "normalizes" true values (anything != 0) to TRUE. Seems redundant if `value` is already boolean.
Comment: @vigneswaran j Sometimes it is necessary that an expression returns exactly either 1 or 0. Consider for example bitwise AND operator x & 8. If the corresponding bit is indeed set then the expression does not return 1.
Comment: Alternatively you can use `!!()` (eg `!!(x & 8)`) to fold the result to 0 or 1. Though it's somewhat more cryptic.
Comment: @VladfromMoscow to normalize values to 1 or 0 `!!` is  better. For example `#define CHECK_bIsTrue(value)  (!!(value))`
Comment: `a < b` doesn't need any normalization, its result is always `0` or `1`. For the macro don't use self invented names for Boolean values. Either use `0` or `1` or use the names `true` and `false` that `stdbool.h` provide.
Comment: @Jens Gustedt It is less readabele and sometimes confuses readers of the code.
Comment: @VladfromMoscow, what is less readable? Are you trying to say that `TRUE` is more readable than `true`?
Comment: @Jens Gustedt I want to say that TRUE or true is more readable then !!.
Answer: Yes, bVal = a>b; is perfectly correct.
The use of the ternary for such a simple assignment is overkill, and the use of a macro is unjustified.
Answer: Sometimes you need exactly 1 or 0 for some expression.
Consider an example.
Let's assume that you decided to write some general function applied to integer arrays that will count something according to the given condition.
The function can look the following way
int count( int a[], size_t n, int predicate( int x ) )
{
    int cnt = 0;

    for ( size_t i = 0; i < n; i++ ) cnt += predicate( a[i] );

    return cnt;
}

As you see the function is implemented such a way that it just adds the result of the predicate to variable cnt.
cnt += predicate( a[i] );

However it is not necessary that predicate returns exactly 1 or 0.
For example some standard C functions that could be used like predicates  usually return some non-zero value to specify that the condition is true. For example according to the description of such functions from <ctype.h> like isdigit, isalpha and so on there is written in the C Standard (7.4.1 Character classification functions)

1 The functions in this subclause return nonzero (true) if and
  only if the value of the argument c conforms to that in the
  description of the function. For example let's assume that there

And there are many such functions in the C Standard (consider for example one more function strcmp).
Thus returning to the example of the function count if it will be used as it is written the result of its execution can be incorrect
For example let's assme that there exists already function that returns a non-zero value if the second bit of a number is set
int is_2nd_bit_set( int x ) { return x & 2; }

In this case if you run this program
#include <stdio.h>

int count( int a[], size_t n, int predicate( int x ) )
{
    int cnt = 0;

    for ( size_t i = 0; i < n; i++ ) cnt += predicate( a[i] );

    return cnt;
}

int is_2nd_bit_set( int x ) { return x & 2; }

int main( void )
{
    int a[] = { 0, 2, 3, 4, 5, 6, 7, 8, 9, 0, 10 };
    const size_t N = sizeof( a ) / sizeof( *a );

    printf( "there are %d numbers that have 2nd bit set\n", count( a, N, is_2nd_bit_set ) );
}

you get wrong result
there are 10 numbers that have 2nd bit set

To get the correct result you have to rewrite the corresponding statement in the function the following way
for ( size_t i = 0; i < n; i++ ) cnt += ( predicate( a[i] ) ? 1 : 0 );

Some programmers used the negation operator ! applied two times to an expression to get exactly 1 or 0. For example
for ( size_t i = 0; i < n; i++ ) cnt += !!predicate( a[i] );

But such a code can confuse readers.
Now when type _Bool was introduced in the C Standard the statement can be written simpler
for ( size_t i = 0; i < n; i++ ) cnt += ( _Bool )predicate( a[i] );

Answer: The statement a > b is guaranteed to produce either non-zero or zero which is sufficient for C operators as if, while etc. But sometimes people for whatever reason want to get definitely either 1 (or TRUE) or 0 (or FALSE) which is the why they use macros.
Comment: `true` and `false` from stdbool.h are preferred. Don't use `TRUE` and `FALSE`, it suggests that your code is obsolete pre-C99 style.
Answer: For historical reasons, all C operands that yield a boolean result, such as the > operator, actually returns type int with value 1 or 0. This makes C odd, because even though there is nowadays a boolean type in the language, all logical and boolean operations still work on an int type.
(bool)a>b is wrong because it is pointless: it converts the a operand to bool before the operation. But the operation itself promotes both operands to int, so the only thing the cast could have achieved was a (unlikely and irrelevant) loss of value.
If you are overly picky about type correctness, you could explicitly cast the result type to bool, by using bVal = (bool)(a>b); which essentially is the very same thing as bVal = a>b;. Either is fine.
The macro you posted is not only pointless, it is bad practice. One should not attempt to invent a personal version of the C language with icky function-like macros. Also, use the pre-defined macros true and false, rather than the non-standard TRUE and FALSE.


CombineLatest Infinite Loop pluck operator

Question: NOTE: I'm calling the set function in a component that has an ngFor. So when I call set function the ngOnInit gets executed multiple times so the set function executes as well multiple times.
I have an observable with an object that I update to keep data consistent.
private app= this.subject.asObservable().distinctUntilChanged();

I access the data using this method
select<T>(name: string): Observable<T> {
   return this.app.pluck(name);
}

When I update a this "app" on every user selection then I enter an infinite loop. So $info starts printing the same information over and over again. And the "set" gets called over and over again.
    I'm using the async pipe to subscribe and unsubscribe in the html view. ANY IDEAS ?
updateFruits(fruit) {
  this.app.set('Fruits', fruit);
}

set(name: string, state: any) {
  this.subject.next({ ...this.value, [name]: state });
}

   this.info$ = combineLatest(fruits$)
  .pipe(
      distinctUntilChanged(),
      debounceTime(0)
      map(fruits => fruits)
   );

Answer: I think to use operators like pluck or distinctUntilChanged you have to use pipe.
private app = this.subject.asObservable().pipe(distinctUntilChanged());

select<T>(name: string): Observable<T> {
   return this.app.pipe(pluck(name));
}

I've never seen before that method set for observables too, only next.
-- Edit
I see, I had thought that this.app was as observable.
I tried to reproduce the bug but everything seems fine:
Hope that helps better: https://stackblitz.com/edit/rxjs-stack
Comment: "set" refers to this method I defined, where I update the value

set(name: string, state: any) {
  this.subject.next({ ...this.value, [name]: state });
}
Comment: Thanks! Could it be because the component where I try to "set" the value uses ngFor to display the info? I see that after calling set function the ngOnInit runs over and over again :o


MSAL React not showing roles, but they are in the token

Question: I'm trying to implement App Roles in a single-tenant React + .NET Core app of ours. This app has successfully been authenticating users via MSAL, so this is just an incremental addition.
I have the app roles set up in Azure AD, and I have the Authorize attribute with role restrictions working in the .NET back-end, but for some reason I'm unable to get the roles via the react MSAL library, even though when I manually decode the token I see them in there.
I was referring to this MS sample for my code. In my index.js, I have the following:
export const msalInstance = new PublicClientApplication(msalConfig);

const accounts = msalInstance.getAllAccounts();

if (accounts.length > 0) {
  msalInstance.setActiveAccount(accounts[0]);
}

Then, in the test page I have, I'm trying to access the roles array in this way (just as a test to print them out):
const TestComponent = () => {
  const { instance } = useMsal();

  useEffect(() => {
    const activeAccount = instance.getActiveAccount();
    setTokenRoles(activeAccount?.idTokenClaims?.roles);
    // I've also tried:
    // setTokenRoles(activeAccount?.idTokenClaims['roles']);
  }, [instance]);

  return (

      ROLES: {JSON.stringify(tokenRoles)}

  );
};

Unfortunately, tokenRoles is null. When I inspect entire idTokenClaims object, I see all the other claims, but no roles. However, I do see them in the token itself:
{
  ...
  "roles": [
    "Packages.Manage"
  ],
  ...
}

I'm really hoping to avoid manually decoding the token. There has to be a way to get it out of MSAL.
Comment: @JoshAnderson ok that explains. So if you enable/create app roles on your web API's app registration, they will appear on the access token that your client SPA app acquires. But if you want the roles claim in the ID token, you'll need to enable/create same app roles on your SPA app's app registration. Basically if you have a SPA + web API setup, you'll need to create app roles twice, one in each app's registration. The sample discusses this more [here](https://github.com/Azure-Samples/ms-identity-javascript-react-tutorial/tree/main/5-AccessControl/1-call-api-roles#registration)
Comment: If you believe there is a bug in MSAL.js here, please file a bug on Github. https://github.com/AzureAD/microsoft-authentication-library-for-js/issues
Comment: Any debugging you can do in the library to help determine the root cause also appreciated, thanks!
Comment: @josh-anderson sorry for being pedantic, but just to clarify, are you decoding the ID token or the access token?
Comment: I decoded the access token. What I get via the acquiretokensilent call. I assumed that's the same basic content tied to the account, but perhaps I'm checking the wrong thing.
Comment: I'm not ready to say it's a bug because I'm not sure I'm using the correct method to get the app roles the authenticated user has.
Comment: This morning I checked the access and account tokens. Neither JSON object returned by MSAL have the `roles` array. Manually decoding the `idToken` and `accessToken`, the roles are only encoded in the access token JWT. If MSAL doesn't natively decode and include in any objects, I guess I can manually decode it. Just doesn't feel like the best way to tackle it.
Answer: Jason Nutter's comments provided the answer, and in case this helps others I figured I'd give it a write-up.
Per the MS docs, I put the app roles on the back-end app registration. This is why I am able to have the Authorize(Roles = "Role") attribute work on the back-end. The reason I can see the roles in the access token is that the token is retrieved with the scope for that back-end API. But because I don't have those roles mirrored on the front-end app registration, I don't see anything in the id token.
There would be two options if you wanted to use the Azure app roles:

Mirror the app roles in the front-end app registration. In this way you'd have access to the roles in the id token. This sounds not good because I could foresee a typo or mismatch causing weird issues. I'm sure there might be a way using the Azure API to have a process that would sync the roles, but that's not worth it in my opinion.
Manually decode the access token on the front-end. The cleanest way I could think of to do this would be to create a roles context that would pull an access token, decode it, and store the roles for child components to refer to.

Another alternative would be to manage roles in the app itself. For us, the application in question is single-tenant, so there's not much need to do that. However, we do have a multitenant app we are moving to MSAL, and in that case we will already need to do things like validate that the tenant is authorized, and we will need more granular permissions than what this internal app needs, so we will likely have role system and have the front-end retrieve role and profile data from the back-end upon authentication through MSAL.
EDIT: What I ultimately did...
I did indeed keep the roles in the back-end only, then created a user context object that the front-end would retrieve. This user context includes the app roles, as well as other convenience data points like nickname, and is used by a React context and provider that I wrap my app in.
Comment: Good find on mirroring the app roles, but agree that could be error-prone. Is this approach documented by MS?
Comment: Not explicitly as a solution to the problem. There are obviously docs on setting up the roles on apps, but nothing on coordination between two connected systems that I could find. I updated the answer with a bit about what we ultimately did — let the back end own the access piece and send the front end a user context object that tells is what the logged-in user can do.
Answer: Josh's answer helped me a lot, but I'm adding my solution as neither was ideal for me.
I went a slightly different route. I didnt want to manually decode the token as the frontend needs the roles before calling an api (for conditionals in template etc.) and the msalService only retrieves the token with the roles when it implicitly calls an api with the scope which contain the roles i need.
Mirroring the app roles isnt ideal either as its messy and bad practice IMO.
So instead I changed it so that I only have a single App Registration for both the Angular App and and API. Then I use the same clientId in both. Just remember to add the Single-Page-Application redirects in Manage -> Authentication.
This way when the Angular MSAL lib gets the scope for itself on login, it also gets the roles for the API. This is also a bit neater and makes it easier to split it out for other instances like Staging / UAT.
Meaning you make a seperate app registration for Staging with the same app roles & scope name.
Then you can just swap the client Id's in both the API & FE and you have a entirely seperate instance of roles etc. This way you can modify a users app role on staging without it effecting their access on Prod. Very important for UAT, and for creating & modifiying Roles with prod still working normally.
Otherwise, with two app registrations for a single instance, its a bit messy to get working. (Also insanely messy if you went the mirroring of app roles route)


how to get total orders of a user?

Question: how can I get total price of all orders which owned by a single user? I want to assign different type of discounts by finding about total amount of their orders.
I'm using

hook_rules_action_info

.
should I use commerce_order_load_multiple() ?
Answer: Try this
<code>$orders = commerce_order_load_multiple(array(), array('uid' => $user_id));
</code>
You can use the conditions parameter of commerce_order_load_multiple.
Comment: tanx your code works fine. tanx
Answer: I had a different approach to a similar scenario.
I created a field in the user profile that would store the total amount of orders so far. This field would be increased every time a new order was placed. Also created 3 different roles (silver, gold, platinum) Each role was entitled to different discounts. When the total order would pass the threshold value for the group, the user will be given that role.
Comment: your solution is totally perfect. tanx a lot.

how to get total orders of a user?


Erro ao tentar passar ticket[i] = i;

Question: using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Testes
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] ticket = new int[5];
            string[] nome = new string[5];
            for(int i = 1; i < 6; i++)
            {
                Console.WriteLine("Bem vindo, digite seu nome: ");
                ticket[i] = i;
                nome[i] = Console.ReadLine();
                Console.WriteLine("Ticket número {0} do Sr. {1}", ticket[i], nome[i]);
                Console.WriteLine("Pressione Enter");

            }
            Console.ReadKey();
        }
    }
}

O erro que aparece é:

An unhandled exception of type 'System.IndexOutOfRangeException' occurred in Testes.exe
Comment: O erro ocorre na primeira execução ou executa corretamente um x de iterações?
Answer: O seu array está declarado como tendo 5 items:
int[] ticket = new int[5];

O problema do seu código é estar a assumir que o primeiro item do array tem como indice 1.
Os indices dos arrays em C# e na maioria das linguagens começam em 0.
Da forma como está o seu for o código irá aceder ao item indice 5 o que ultrapassa o comprimento do array.
Altere o seu ciclo for para:
for(int i = 0; i < 5; i++)

Uma forma de evitar este tipo de erro é usar, na condição, a propriedade Length do array:
for(int i = 0; i < ticket.Length; i++)

Erro ao tentar passar ticket[i] = i;


Convert videos in ffmpeg filter_complex with different timebases to use xfade

Question: I'd like to use ffmpeg's new xfade filter to crossfade between two videos. In theory this is easy:
ffmpeg -i v1.mp4 -i v2.mp4 \
  -filter_complex "[0][1]xfade=duration=1:offset=5,format=yuv420p" \
  out.mp4

However, this fails with the error:
First input link main timebase (1/1500) do not match the corresponding second input link xfade timebase (1/30000)
Obviously these two videos do have different timebases. This is confirmed by the ffmpeg output:
Stream #0:0(eng): Video: h264 (High 4:4:4 Predictive) (avc1 / 0x31637661), yuv444p, 1280x720 [SAR 1:1 DAR 16:9], 536 kb/s, 15 fps, 15 tbr, 1500 tbn, 30 tbc (default)
...
Stream #1:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709/unknown/bt709), 1280x720 [SAR 1:1 DAR 16:9], 1117 kb/s, 29.97 fps, 29.97 tbr, 30k tbn, 59.94 tbc (default)

I don't want to convert each video and save the results and then combine the two new temporary disk files. Is it possible to use -filter_complex to convert the videos before xfade so that they have the same timebase? So that I can run one command which takes any two videos and crossfades between them?
Attempting to set the timebase explicitly with the settb filter seems to work at first but then gets "stuck" on frame 3 and never makes any more progress:
$ ffmpeg -i v1.mp4 -i v2.mp4 -filter_complex "[0]settb=1/AVTB[v0];[1]settb=1/AVTB[v1];[v0][v1]xfade=duration=1:offset=5,format=yuv420p" out.mp4
ffmpeg version N-53546-g5eb4405fc5-static https://johnvansickle.com/ffmpeg/  Copyright (c) 2000-2020 the FFmpeg developers
  built with gcc 8 (Debian 8.3.0-6)
  configuration: --enable-gpl --enable-version3 --enable-static --disable-debug --disable-ffplay --disable-indev=sndio --disable-outdev=sndio --cc=gcc --enable-fontconfig --enable-frei0r --enable-gnutls --enable-gmp --enable-libgme --enable-gray --enable-libaom --enable-libfribidi --enable-libass --enable-libvmaf --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librubberband --enable-libsoxr --enable-libspeex --enable-libsrt --enable-libvorbis --enable-libopus --enable-libtheora --enable-libvidstab --enable-libvo-amrwbenc --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libdav1d --enable-libxvid --enable-libzvbi --enable-libzimg
  libavutil      56. 56.100 / 56. 56.100
  libavcodec     58. 97.100 / 58. 97.100
  libavformat    58. 49.100 / 58. 49.100
  libavdevice    58. 11.101 / 58. 11.101
  libavfilter     7. 87.100 /  7. 87.100
  libswscale      5.  8.100 /  5.  8.100
  libswresample   3.  8.100 /  3.  8.100
  libpostproc    55.  8.100 / 55.  8.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'v1.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: mp42mp41isomiso2
    creation_time   : 2014-08-20T20:47:56.000000Z
    encoder         : x264
  Duration: 00:00:12.67, start: 0.000000, bitrate: 538 kb/s
    Stream #0:0(eng): Video: h264 (High 4:4:4 Predictive) (avc1 / 0x31637661), yuv444p, 1280x720 [SAR 1:1 DAR 16:9], 536 kb/s, 15 fps, 15 tbr, 1500 tbn, 30 tbc (default)
    Metadata:
      creation_time   : 2014-08-20T20:47:56.000000Z
      handler_name    : VideoHandler
Input #1, mov,mp4,m4a,3gp,3g2,mj2, from 'v2.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf57.83.100
  Duration: 00:00:04.31, start: 0.000000, bitrate: 1286 kb/s
    Stream #1:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709/unknown/bt709), 1280x720 [SAR 1:1 DAR 16:9], 1117 kb/s, 29.97 fps, 29.97 tbr, 30k tbn, 59.94 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #1:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 159 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
File 'out.mp4' already exists. Overwrite? [y/N] y
Stream mapping:
  Stream #0:0 (h264) -> settb (graph 0)
  Stream #1:0 (h264) -> settb (graph 0)
  format (graph 0) -> Stream #0:0 (libx264)
  Stream #1:1 -> #0:1 (aac (native) -> aac (native))
Press [q] to stop, [?] for help
[libx264 @ 0x69aaac0] using SAR=1/1
[libx264 @ 0x69aaac0] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
[libx264 @ 0x69aaac0] profile High, level 3.1, 4:2:0, 8-bit
[libx264 @ 0x69aaac0] 264 - core 161 r3018 db0d417 - H.264/MPEG-4 AVC codec - Copyleft 2003-2020 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=6 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=15 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to 'out.mp4':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: mp42mp41isomiso2
    encoder         : Lavf58.49.100
    Stream #0:0: Video: h264 (libx264) (avc1 / 0x31637661), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], q=-1--1, 15 fps, 15360 tbn, 15 tbc (default)
    Metadata:
      encoder         : Lavc58.97.100 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: N/A
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
      encoder         : Lavc58.97.100 aac
frame=    3 fps=0.0 q=0.0 size=       0kB time=00:00:02.94 bitrate=   0.1kbits/s
frame=    3 fps=3.0 q=0.0 size=       0kB time=00:00:04.24 bitrate=   0.1kbits/s
frame=    3 fps=2.0 q=0.0 size=       0kB time=00:00:04.24 bitrate=   0.1kbits/s
frame=    3 fps=1.5 q=0.0 size=       0kB time=00:00:04.24 bitrate=   0.1kbits/s
(and repeat this line forever)

Answer: You can use the settb filter to set a common TB.
ffmpeg -i v1.mp4 -i v2.mp4 \
  -filter_complex "[0]settb=AVTB[v0];[1]settb=AVTB[v1];\
                   [v0][v1]xfade=duration=1:offset=5,format=yuv420p" \
  out.mp4

Comment: I also got the same error for a differing frame rate which I resolved by adding the `fps` filter and setting it to 30/1. Is there an equivalent to AVTB for the fps filter? Unfortunately the xfade documentation makes no mention of any of these factors that need to be normalised before the crossfade can be applied.
Comment: Unfortunately this doesn't seem to work as I'd hoped: the encode gets to frame 3 and then repeats the same line over and over again forever: "frame=    3 fps=0.0 q=0.0 size=       0kB time=00:00:04.24 bitrate=   0.1kbits/s dup=0 drop=125".
Comment: Share full log.
Comment: Excellent. That works, thank you! It doesn't offset the _sound_ from the videos, but that'll need to be done separately, of course!
Comment: Use the acrossfade filter for that. But trim the audio streams first as per the xfade offset.
Comment: What's the reason that the TB needs to be the same when they're being encoded (i.e. not concatted)?
Comment: What did you do to fix the hanging and repeating the same line forever? I have the same problem when using `settb=AVTB`


O que é "expressão idiomática" na programação?

Question: Já me deparei com o termo "expressão idiomática" ou "forma mais idiomática". Basta pesquisar no site que irá achar várias referências: https://pt.stackoverflow.com/search?q=idiomatica
Afinal o que é "expressão idiomática"? Tem relação com programação elegante?
Comment: https://pt.wikipedia.org/wiki/Express%C3%A3o_idiom%C3%A1tica
Answer: Tem relação com programação elegante sim. Não é elegante produzir códigos não idiomáticos. Mas elegância não é tudo em programação.
A expressão ou forma idiomática é o jeito mais elegante de se fazer uma tarefa comum naquela linguagem.
Vamos pegar o inglês.

Eu separei da minha namorada

Poderia ser traduzido como:

I splited with my girlfriend

Isso pode ser entendido, mas não é idiomático. Ficaria melhor falar:

I broke up with my girlfriend

Em programação é assim também, você pode fazer algo que funciona bem, o código executa e produz o resultado esperado sempre, provavelmente de forma eficiente, mas naquela linguagem específica tem um jeito mais elegante de fazer, talvez gere até mais eficiência ou evite algum problema que só existe nessa linguagem.
Um idioma pode se aproximar de um padrão de projeto (design pattern), mas não chega ser porque ele é algo mais específico. Algumas pessoas poderão confundir com boas práticas, e em certo sentido é, pelo menos na forma de escrever o código para uma tarefa bem simples. Também poderia ser confundido com uma receita de bolo, mas idioma é mais sobre a forma de escrita do que a tarefa em si.
Idioma aqui significa um jeito específico de usar uma linguagem. É a forma peculiar de expressar algo em determinada linguagem.
Códigos não idiomáticos costumam ser produzidos quando é pego uma solução de uma linguagem e porta na outra. Ou se o programador experiente em outra linguagem não está acostumado com o padrão daquela linguagem. Ou ainda se ele está produzindo códigos, mas não é programador de verdade. Mesmo linguagens parecidas, como Java e C#, possuem idiomas muito diferentes.
Algumas linguagens se beneficiam bastante de códigos idiomáticos, outras ocorre mais pela estética, é conformação com aquela comunidade específica.
C#
Por exemplo, você pode programar em C# de forma 100% imperativa, abandonar OOP. Mas não é idiomático.
Ou pode abusar de dynamic ou object pra flexibilizar a aplicação, mas não é idiomático.
Pode preferir varrer coleções com for no lugar de um foreach ou mesmo LINQ, mas não é idiomático.
Pode usar o padrão de projeto Observer e não um event que é o idiomático.
Note que escrever de um jeito ruim vai além de não ser idiomático. Se em vez de usar using para dispor de recursos, usar um try-finally não é idiomático, mas se nem usar o try-finally, aí já é erro mesmo.
Você pode criar um padrão de nomenclatura seu, mas a linguagem tem um padrão que é mais idiomático.
C# tem uma forma idiomática moderna e outra forma legada. E isso muda em cada versão. Por exemplo, em C# 7 tuplas substituíram em muitos casos pelo menos o uso de Tuple<> e out em parâmetros.
Python
Um exemplo não idiomático em Python:
mylist = [1, 2, 3, 4]
newlist = []
for i in mylist:
    newlist.append(i * 2)

Agora idiomático, dito jeito pythônico:
mylist = [1, 2, 3, 4]
newlist = [(i * 2) for i in mylist]

Coloquei no GitHub para referência futura.
Idiomas universais
Tem idiomas que não são tão inerentes à linguagem. Um exemplo é a tal da fluent interface.
Conclusão
Nem sempre as pessoas concordam qual é o jeito idiomático. E a mesma linguagem pode ter jeitos diferentes. Muitas vezes existe o jeito idiomático legado e o jeito moderno com o advento de alguma forma nova de expressar aquilo mais elegantemente. E alguns domínios podem exigir um idioma específico.
Um dicionário de idiomas de uma linguagem poderia agrupar as mais diversas formas de escrever o código de certas tarefas que são comuns para um programador. Sinto falta disto na internet.
Programadores profissionais procuram fazer do jeito idiomático até onde faz sentido. Programadores amadores fazem na base do bumba-meu-boi. Programadores profissionais não usam os idiomas sem questioná-los antes e sabe quando evitar o idioma em favor de uma solução melhor para aquele caso. O profissional sabe qual idioma usar.
Tem um artigo na Wikipedia sobre o assunto.
Comment: @Murilo é sempre bom esperar mesmo, não tenha pressa alguma. Eu mesmo gostaria que tivesse outras respostas.
Comment: Não aceitei ainda pra ver se vai ter outra resposta. Ótima resposta.


How to call a task in product flavours

Question: I am trying to copy a db file in assets based on build flavours, for this, I have created a task in build.gradle (app level)
 flavorDimensions("default")

    productFlavors {
        dev {
            applicationIdSuffix ".dev"
            buildConfigField("String", "DB_FILE_DIR", '"stage/"')

            delete"$rootProject.projectDir/app/src/main/assets/app_db.db"

        }
        stage {
            applicationIdSuffix ".stage"
            buildConfigField("String", "DB_FILE_DIR", '"stage/"')

            delete "$rootProject.projectDir/app/src/main/assets/app_db.db"

        }
        production {
            applicationIdSuffix ".production"
          delete"$rootProject.projectDir/app/src/main/assets/app_db.db"
        }
    }

    variantFilter { variant ->
        if (variant.buildType.name.equals('release') || variant.buildType.name.equals('debug')) {
            variant.setIgnore(true)
        }
    }

Task to copy the database files.
task copyProductionDB(type: Copy) {
    from file("$rootProject.projectDir/production/app_db.db")
    into "$rootProject.projectDir/app/src/main/assets/"
    println("Production DB copied")
}
task copyStageDB(type: Copy) {
    from file("$rootProject.projectDir/stage/app_db.db")
    into "$rootProject.projectDir/app/src/main/assets/"
    println("Stage DB copied")
}

And my problem is on switching from one flavour to another,I have to replace the db file to assets folder. How can I achieve this. Please help me. Thanks in advance.
Answer: You can check application variant and get its direcotry:
android.applicationVariants.all { variant ->
    // get directory
    variant.dirName // it returns debug/dev or relese/dev , debug/stage or relese/stage
}

Answer: I solved the my problem with the following code.
 tasks.whenTaskAdded { task ->
        if (task.name ==~ /preDevMyAppBuild.*/) {
            task.doFirst() {
                copyDB("stage")
            }
        } else if (task.name ==~ /preStageMyAppBuild.*/) {
            task.doFirst() {
                copyDB("stage")
            }
        } else if (task.name ==~ /preProductionMyAppBuild.*/) {
            task.doFirst() {
                copyDB("production")
            }
        }
    }

    def copyDB(copyType) {
        copy {
              from file("$rootProject.projectDir/"+copyType+"/app_db.db")
              into "$rootProject.projectDir/app/src/main/assets/"
              println(copyType+" DB copied")
             }
    }

How to call a task in product flavours


Has anyone studied linear regression where the covariance matrix of the error is a function of the parameters being estimated?

Question: Consider the multivariate linear model: $$y = X\beta + e$$ $y$ is the measured output, $X$ is the model matrix, $\beta$ is the parameter vector, and $e$ is the zero-mean error vector: $$E[e] = 0 \qquad E[ee^T] = V$$ The goal is to estimate $\beta$ from $y$. It is well known that the minimum variance, affine unbiased estimator of $\beta$ exits if and only if $X$ has linear independent columns, in which case it is $$\hat\beta = [X^T (V + XX^T)^+ X]^{-1} X^T (V + XX^T)^+y$$ Many statisticians, mathematicians, econometricians, etc. have studied this problem where the error covariance matrix $V$ is partially or completely unknown. There are plenty of approaches on how to estimate $\beta$ and/or $V$ in this case.
My situation: I have an application where $V$ is unknown, but $V$ is a known function of $\beta$: $$V = f(\beta)$$ I have conducted a literature search looking for sources that have studied this problem, but I haven't found anything. It seems this situation has not been widely considered, if at all, but I am not a statistician; the literature is vast and I could just be searching for the wrong thing. However, if those sources are out there, I would like to cite them.
My question: Does anyone know of any papers/books/whatever that consider this problem?
Comment: Being a non-statistician myself I suggest that you look for the literature referring to this specific problem in the relevant field, rather than for a general solution by statisticians/mathematicians/etc. The problem might be well explored by the practitioners for the realistic choices of $f(\beta)$, even if a general solution is impossible.
Comment: @Vadim I appreciate the comment. Your suggestion is exactly what I am trying to do; my issue is that I don't really know where to start looking for such sources, and that is where I need help.
Comment: I suppose that there is an actual (not purely mathematical) problem in biology/economics/physics or another field that is behind the question?
Comment: @Vadim actually, not really. The problem I am studying is noise covariance estimation for discrete time, time invariant linear systems. The work is not motivated by any particular physical system in particular. Essentially, $\beta$ is the covariance matrices of the noises which drive the system. This is the source of all of the uncertainty in the system, so this is why the case $V = f(\beta)$ occurs, in a nutshell.
Answer: This problem is actually quite ubiquitous.  It occurs in regression models where there is heteroscedasticity that is described by some parametric function of the data.  It also occurs in standard time-series models where there is an auto-regressive parameter on the error terms.  There are certainly many books and articles that look at this type of problem, but you will probably need to narrow it down to the type of parametric dependence you are interested in.  Unless you have a contrary interest, I would suggest you start by looking at Gaussian AR models, and then work your way up from there.
Without knowing more about your background (e.g., your existing mathematical and statistical knowledge) it is quite difficult to suggest a reference that is pitched at the right level.  If you have quite a good maths background, you might be able to read a useful primer on the linear AR(1) model by Grunwald, Hyndman and Tedesco (1996).  This article examines various formulations of the linear AR(1) model, and looks at some relevant literature.
Comment: Thanks! As a non-statistician, that is a bit of a mouthful for me. Without going into too much detail, what I am doing does involve a Gaussian AR model (though I'd just call it a discrete time linear model, i.e. $x_{k+1} = Ax_k + w_k$ and $y_k = Cx_k + v_k$. Can you suggest a reference I can start with? I know how I want to tackle the estimation problem for my application, so I don't care a ton about the details. Rather, I was about to write "the problem where $V = f(\beta)$ seems not to have been considered by many authors", so if that's not actually true, I just want to cite an example.


How to check if my postfix is running

Question: We do a relay to another server which we use only for sending mails. Since we send a lot of mails daily (no spam), the relaying server's postfix tends to crash (I'm not that technical in this matter to know how to give you more info on this), and I have to restart the service.
Now, I'm wondering what could I do in order to check the postfix service on the relaying server every "now and then" to see if it's running - and if not have it send me an email with the error, or maybe even potentially (if this is possible) restart the postfix service automaticaly.
Sure, I know I should "write a script to do that" - but actually here I'm stuck - what script, which language, etc - any help is appreciated.
Comment: Wow. Postfix crashing? Then you must send _very_ much e-mail. Do you send something like million mails per minute around the clock? :) You truly should let someone investigate your Postfix/CentOS setup, something's wrong if Postfix regularly crashes. It's one of the most reliable workhorses I've seen.
Comment: If you're not that technical, I'd recommend getting someone who *is* that technical in to fix it.  We're not going to be able to teach you how to debug segfaults.
Comment: @JannePikkarainen: no, we don't send millions, we send about 50k in 3 hours.
Comment: That's definitely not a lot of e-mail. It's about 5 e-mails per second and no, that should not hurt even your grandma's 486DX2/66 MHz. :-) So something's fundamentally wrong. Do you know if your Postfix uses something like a database for recipient address mapping?
Comment: @JannePikkarainen: actually, I would say no. This server is just as a "relay" so all it does is it takes the mail sent from our "new" server and sends it (if I understood the terminology right). The reson we kept the "old" one (this relaying one) is because it has good IP rep.
Answer: You can use monit. It is a nice tool to monitor your running process and can take specific actions like restarting the service and alerting you by sending an email, etc..
Comment: I'd add that using Monit is a good idea just for monitoring's sake, but really...Postfix shouldn't be crashing like that. In this particular use case it sounds like a band aid over a bigger problem that should be focused on fixing.
Comment: @BartSilverstrim: I totally agree with you. Monit should not be used as a solution for a continuously crashing process.

How to check if my postfix is running


Testing if there is any new knowledge in a third variable

Question: I am trying to predict the outcome $X$ of an event given a big set of input variables $A$, $B$, $C$, $D$, etc.
Both $X$ and $A$, $B$, $C$, etc. are categorical variables, some of them with a high number of categories.
We can think of $A$ as the main input because there is a hard correlation between $X$ and $A$. Also, most of the other variables $B$, $C$, $D$, etc. have a high correlation with $A$ too.
In order to reduce the size of my input vector I was thinking about some way to remove those variables that don't increase my knowledge about $X$ farther once I know $A$.
I am considering performing a $\chi^2$ test for every triplet $(X, A, anyOtherVar)$. My reasoning is that $p_{(A,B,X)} = p_A*p_{(B,X|A)}$ and if we assume that $B$ does not increase my knowledge about $X$ once I know $A$, then $p_{(A,B,X)} = p_A*p_{(B|A)}*p_{(X|A)}$ which becomes my null hypothesis.
And so, I can build the 3-dimensional contingency table using the probability from my null hypothesis $p_A*p_{(B|A)}*p_{(X|A)}$ instead of the usual $p_A*p_B*p_X$.
Does that procedure looks sound to you? Are there any other known and better approaches to attain my objective?
Update: Because of the hard correlations, I have a lot of zeros in the $p_{B|A}$ and $p_{X|A}$ matrices which result in divisions by zero in the $\chi^2$ addends. I can remove the cells with 0 predicted elements from the table, but then I am finding quite difficult to come with a method to calculate the degrees of freedom.
Answer: A reasonable approach for your problem is forward selection, a type of Stepwise Regression.
You could fit a regression with just the $A$ variables, and then compare this to the regression with the $(A,B)$, $(A,C)$, and $(A,D)$ variables via F-test where, for example
$$F_{A \; \text{vs.} \, (A,B)}=\frac{(SSE_A-SSE_{(A,B)})/(df_{A}-df_{(A,B)})}{SSE_{(A,B)}/df_{(A,B)}}.$$
Doing this for $(A,B)$, $(A,C)$, and $(A,D)$, you then add the variable to the model which had the highest value in the $F$ test (or if no variable was significant in the $F$ test you are done and can just stick with using $A$). You can then continue on in this manner to see if adding a third variable makes sense.
You can also try backward selection to see if it gives similar results.
Answer: The best method to achieve this would be a PCA or factor analysis on the variables before you do the regression. Think about it this way: If $A$ and $X$ are highly correlated, and $B$ and $X$ are also highly correlated, then very likely (but not necessarily) $A$ and $B$ are also correlated. So your variables $A,B,C,\ldots$ don't really capture independent information. This leads to problems in the analysis. So it is better to first remove this correlation and identify the underlying actual information in the variables. A PCA can achieve this.
Comment: @salva I missed that you said you had categorical variables. PCA with categorical variables is possible, but never a great thing to do.
Comment: PCA for large categorical variables? Also, I would prefer a method that allows me to eliminate input variables instead of doing some transformation with all of them.
Answer: It is not true that $\Pr(X|A, B) = \Pr(X|A)$ implies $\Pr(X|A, B, C) = \Pr(X|A, C)$: Given, $A$, a variable might predict $X$ together with another variable $C$ but not on it's own. And the same could hold for $C$.
Here's a stark example illustrating the point. Let $B$ be a coinflip. $C$ is another, independent, one. $X$ is a light. It turns on if $B$ and $C$ take the same value. $B$ is clearly useless for predicting $X$. As is $C$. But if you know both, you can perfectly predict $X$.
Considering one variable at a time in addition to $A$ would fail to uncover such interactions. It might be too aggressive and remove variables that would help.
But it might also be too conservative and fail to remove variables that are useless. If one variable is a copy of another, they'd both be kept even as only one of them is useful. From what you describe, this is likely in your case (the variables are highly correlated)
An alternative approach I'd suggest would be to estimate the additional predictive powers of large sets of variables in addition to $A$ jointly. Consider using a model with Lasso or Elastic Net penalty.
Answer: As @David Veich already mentioned a good way to proceed further would be feature selection methods like Forward selection,Backward elimination or Recursive feature elimination.
In place of these embedded Elimination methods you can also apply a Lasso Regression model.
You can also look into  discriminant correspondence analysis which is an extension of Linear Discriminant Analysis for Categorical Variables.
Do view this links for more information:
Lasso Regression for Categorical Variables
Discriminant Correspondence Analysis


VS Code launch.json variables regex replacement

Question: In launch.json I have a debug configuration that runs mocha for the current file:
{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Mocha Current File",
            "program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
            "args": [
                "--timeout",
                "999999",
                "--colors",
                "${file}"
            ],
            "console": "integratedTerminal",
            "internalConsoleOptions": "neverOpen"
        }
    ]
}

I am using typescript and the ts file is at folder src/... root whereas the test.js file is at folder root dist/... I want to have the ts file open, run the debug mocha configuration and have rewrite the src path to the dist path.
The following gets met almost there: "dist/${relativeFileDirname}/${fileBasenameNoExtension}.js"
but relativeFileDirname still contains src/... as the root. I need to do a regex replacement.
Extension Command Variable has more or less what I need but it does directory up patterns whereas I need directory down patterns, ie it can take src/folder1/folder2 and return src/folder1 whereas I want folder1/folder2.
Answer: you can use the extension.commandvariable.transform command.
  "inputs": [
    {
      "id": "down1Dir",
      "type": "command",
      "command": "extension.commandvariable.transform",
      "args": {
        "text": "${relativeFileDirname}",
        "find": ".*?/(.*)",
        "replace": "$1",
      }
    }
  ]


Re-ranking a list

Question: I have a ranked list say A,B,C,D,E,F,G and a subset of this list ranked with another strategy
lets say D,B,C,F. And my assumption is that the strategy used for ranking the sublist is better because its a feedback strategy. Now I want to re-rank my initial ranked list according to feedback ranking of sublist and initial ranking available. Please, suggest some good algorithm in this direction
Comment: Please try to give a more formal definition of what you want to achieve. You want to order a list by two rankings: are these rankings consistent? what happens when they conflict? does one have priority over the other?
Comment: I suppose the ranking you want is A,D,B,C,E,F,G?
Comment: The first ranking is the expected ranking, and from that expected ranking I choose elements from the top and pitch them, from there I get second ranking i.e feedback ranking, now I want to use this feedback ranking to improve my initial ranking system, so that I do not loose upon good elements just because their expectation was low.
main issue is that feedback is available for only elements that are expected to be good. So other elements never get feedback.
Answer: You can treat this as a Reinforcement Learning problem, specifically as multi armed bandit problem where every item in the list represents a one armed bandit.
Basic algorithms to solve this can be found in chapter 2 of Sutton,Barto - Reinforcement Learning: An introduction.
In order to apply these algorithms, one has to attach some sort of quality value to each item / arm, for example its (normalized) position in the list. By using smaller initial quality values (based on the initial ranking) than the quality values gained by feedback, you can model your trust in the feedback strategy.
You have stated:

main issue is that feedback is available for only elements that are expected to be good. So other elements never get feedback.

This is called exploration-exploitation tradeoff: Look for new, may be better arms / items (explore) or stick with those which are considered good right now (exploit).
If for example you never gets feedback for A meanwhile everytime D is submitted for feedback it gets ranked at top position, how long are you willing to keep A at the top position ?  By setting the initial / feedback quality values and the algorithm related parameters one can model the sensitivity in this regard.

Re-ranking a list


Find a response vector that minimizes the distance to the true line

Question: I have a system of equation of the following form:
$$ {\bf W} \vec{x} = \vec{y} $$
W is a thin matrix (i.e. has more rows than columns) and $\vec{x}$, $\vec{y}$ are vectors of appropriate size.
I solved this equation using the pseudo-inverse:
$$ \vec{x} = ({\bf W})^\dagger \  \vec{y} $$
We know that by definition the pseudo-inverse gave the solution that minimized: $$|| {\bf W} \vec{x} - \vec{y}||$$
However, now that I found the values of my vector $\vec{x}$, I would like to find the maximum amount of change that I could apply to the vector $\vec{y}$ that would keep the values of $\vec{x}$ constant and would minimize the distance $|| {\bf W} \vec{x} - \vec{y}|| = || {\bf W} \vec{x} -  \vec{y}||$ further.
Is there any way that I could find an analytical solution to this problem?
Clarification:: Let $\epsilon =  || {\bf W} \vec{x} -   \vec{y}||$.
I have two steps to my problem: First I would like to find my response variable $\vec{x}$ which I simply find by calculating $({\bf W})^\dagger \  \vec{y}$. Now, my problem arises here.
After this step and getting my value $\vec{y}$, I would now like to change the values of $\vec{y}$ to reduce $\epsilon$ while keeping the values of $\vec{x}$ constant.
Edit 1: fixed a typo, the solution to the system is simple the pseudo-inverse of ${\bf W}$ and not the ${\bf W}^T$.
Edit 2: added a clarification.
Comment: Your pseudoinverse solution has an erroneous transpose and is dimensionally  incompatible. The general solution is $$x=W^+y + (I-W^+W)z$$ where $z$ is an arbitrary vector. The minimal length of $x$ coincides with $z=0$.
Comment: If you consider $y$ to be a function of $x$, the solution becomes
$$y=Wx + (I-WW^+)u$$ where $u$ is arbitrary. You can vary $y$ by $(I-WW^+)u$ without affecting the value of $x$.
Comment: Hey thanks for the answer, the transpose was a typo, just fixed it, there is no dimensionality mismatch in that case right ?
Answer: You can make sure that the residual doesn't change at all simply by moving along a level set. Assume this path is of the form y+f(tau), f(0)=0, simply calculate the gradient of the residual with respect to tau and set to 0: grad ((y+f)'(y+f) - 2(y+f)'Wx + x'W'Wx) = 0 => 2f_t'f + 2f_t'y - 2f_t'Wx = 0 => f_t'(f+y-Wx) = 0
So simply find any vector valued function of tau such that the above is satisfied, then you'll have a level set and the residual will be unchanged.
Comment: I guess to better put it : I want to reduce the residuals as much as I can without changing the values of $\vec{x}$ but only by the values of $\vec{y}$.
Comment: then set y = Wx


Em qual contexto o padrão de máquina de estado é indicado?

Question: Eu andei pesquisando algumas implementações de máquina de estado e gostaria de saber em qual situação a maquina de estado se encaixa e qual o beneficio de usá-la?
Comment: Relacionado: [O que é uma máquina de estado?](https://pt.stackoverflow.com/questions/208328/o-que-%c3%a9-uma-m%c3%a1quina-de-estado)
Answer: Introdução
Programar é achar uma solução automatizada para um problema. Certos tipos de problemas em programação, que envolvem entidades que transitam a cada momento para um estado diferente dentro de um conjunto finito de estados possíveis, podem ser modelados (representados) na forma de uma máquina de estado finita, que é basicamente um certo tipo de diagrama que irei descrever. Essa representação em princípio não chega a entrar no mérito de como esse problema vai ser solucionado no código, mas facilita essa transição, sugerindo um algoritmo e eventualmente algumas abstrações.
A definição de máquina de estado finita (FSM) pode ser mais rigorosa que isso, mas irei tentar explicar de uma maneira mais visual.
Também não estou entrando no mérito do outro tipo de máquina de estado, que são as máquinas de estado infinitas. Como se vê aqui no SOpt, é comum que ao se falar em máquina de estado em programação esteja subentendendo as finitas.
Pois bem. Uma vez modelado o problema ou cenário em questão, a forma de implementação irá depender essencialmente do tipo de aplicação que você está desenvolvendo, do(s) paradigma(s) de programação adotados (em outras palavras, da linguagem) e, claro, do problema em si.
O que é e onde se aplica
É um conceito bem simples na verdade, em geral faz sentido modelar o problema como uma máquina de estado finita quando você tiver uma entidade (uma "coisa" qualquer) que pode a cada momento estar em um determinado estado dentro de um conjunto de diversos estados possíveis, como no caso de uma lâmpada (ligada ou desligada), um semáforo (vermelho, verde, amarelo, e não esqueçam também do estado de "defeito") ou outras coisas assim (na verdade acho que três ou mais estados é mais comum, se forem só dois a gente tende a se virar com booleanos para resolver o problema), e existe um critério para se passar de um estado para outro que respeita uma certa lógica e também ações que devem acontecer quando se chega a um estado "X", bem como a lógica para escolher qual vai ser o próximo estado.
Qualquer situação envolvendo algum tipo de fluxo ou etapas (sejam elas obrigatórias ou opcionais), onde exista uma etapa de partida e uma de chegada ou então as transições de uma para outra entre em ciclo, pode caber aplicar a ideia de máquina de estados finita, que não passa de uma brincadeira de ligue-os-pontos com bolinhas e setas (as bolinhas são os tais estados possíveis, e as setas são as transições de um para outro, normalmente com o critério para a transição ocorrer anotado perto de cada seta).
Se quiser visualizar como é, tem uma figura mais abaixo.
Benefícios
Pode ser interessante modelar a solução assim porque é isso que o problema representa, então essa seria a vantagem, a modelagem da solução ficar mais próxima à representação do problema e portanto mais fácil de implementar, estender e dar manutenção. O diagrama subentende um algoritmo que pode ser implementado de maneira clara.
Outro possível benefício é aproveitar as abstrações que ela sugere. Em orientação a objetos por exemplo, se você implementar o conceito de "estado" como um objeto, pode achar interessante no seu código manter a escolha do critério de transição vinculada ao objeto que representa aquele estado. Também pode tirar proveito de herança e polimorfismo para acrescentar novos estados que não estavam previstos inicialmente e invocar seus métodos de maneira a obedecer o princípio do Aberto/Fechado (aberto para extensão, fechado para modificação).
Formas de implementar
A implementação em si pode ser feita de várias maneiras, vai depender do problema e do tipo de aplicação que você está desenvolvendo.
Se for para funcionar em uma thread (linha de execução) só, pode ser feito por exemplo com um simples switch(estadoAtual) contido em um loop, para checar o estado atual e cada case executar determinadas ações quando se chega naquele estado, seguido da lógica de mudar de um estado para outro, que irá variar conforme a situação.
Também podem ser ifs:

(Exemplo bem simples de visualizar que peguei desavergonhadamente da Internet)
Ou também usando-se certos padrões de projeto, se não me engano o padrão State é um que pode ser aplicado neste cenário. Outro possível conforme o caso é o Builder, que consiste em ir construindo um objeto em passos graduais.
Exemplos de uso
Máquinas de estado fazem sentido em vários cenários, por exemplo num contexto de estabelecer ou autenticar uma conexão de rede em que é feito um handshake ("aperto de mão" entre as partes que envolve vários passos) e implica em vários estados intermediários que cada lado pode assumir (como ocorre no nível da implementação do protocolo TCP, ou no dos protocolos SSL/TLS, ou mesmo no nível da aplicação final conforme o protocolo de autenticação que se está implementando), na verdade não só o handshake mas o ciclo de  vida todo da conexão.
Ou num contexto de executar uma sequência de passos qualquer e ir assumindo estados intermediários (um possível exemplo seria um carrinho de compras ou processo de compra num site de e-commerce ou de delivery, que do lado do servidor seriam bons candidatos para um Builder).
Só lembrando que uma máquina de estados não é necessariamente sequencial, ela pode ser cíclica ou conter transições de um estado para outro que formam ciclos ou mesmo de um estado para ele mesmo, para não ficar com a ideia errada por conta desses exemplos que eu citei por último que foram mais sequenciais.
Outros dois casos para máquina de estados são o ciclo de vida de uma "tarefa" em um sistema operacional moderno (por exemplo: criado, pronto p/ executar, em execução, bloqueado, terminado, etc.) e as implementações de loops em thread única que disponibilizam sem abstrair muito os recursos de I/O não-bloqueante feita pelo sistema operacional com o intuito de obter maior escalabilidade em aplicações que executam muita I/O, por exemplo através da biblioteca de NIO do Java que é base para frameworks de comunicação em rede escaláveis como o Netty e o Apache MINA, bem como versões de Tomcat mais recentes, ou como suponho que é feito no Node.js para atender um grande volume de requisições da Web a partir de uma thread só.
Comment: Obrigado pela resposta @Piovezan, deu para esclarecer o assunto.
Comment: " uma máquina de estados não é necessariamente sequencial, ela pode ser cíclica e também ter loops internos" @Piovezan  Imagino que tenha usado isso para ilustrar sua explicação apenas, mas uma máquina de estados não tem ciclos, sequência ou algum tipo de loop interno ou externo. Tem apenas ao menos 1 estado e talvez uma regra de transição para algum outro estado. Uma máquina finita de estados FSA pode ter dois estados por exemplo: ligada e desligada. e oscilar entre os dois. Isso não é um loop. Do ponto de vista de design cada estado é autosuficiente.
Comment: @arfneto Chamei de loops nesse caso às eventuais sequências de transições cíclicas, não os loops no código da implementação, se foi a isso que você se referiu. Ciclos entre estados ou de um estado para ele mesmo acredito que tem sim: https://blog.markshead.com/869/state-machines-computer-science/ De qualquer forma não estou sendo muito rigoroso na conceituação e posso ter falado algo errado.
Comment: @piovesan Entendo a idéia. Acho que o autor que mencionou também quis simplificar. Li o artigo. Acho que a idéia de loop não se aplica a uma FSM --- e acho que, até como o tal Mark chega a acenar, o que importa mesmo é uma maquina de estados determinística. E nesse caso cada estado tem uma regra objetiva de sucessão, podendo ser o único estado, afinal 1 é finito. E se pode implementar completamente um estado apenas mantendo a máquina e o contexto. Esse é o lance desse design pattern. Em um protocolo de comunicação por exemplo um grupo pode implementar até um certo estado apenas
Comment: @Piovezan e outro grupo seguir adiante depois da sessão autenticada. Esse é o valor dessa coisa: poder programar algun estados e seguir adiante. Talvez a gente devesse ter um exemplo. Vou ver se tenho algo disponível aqui
Comment: @arfneto Se quiser acrescentar, sugiro fazê-lo na forma de uma outra resposta. A minha está bem informal e acho que ficou até meio grandinha. Acho que o que estou definindo é uma FSM mesmo.
Comment: @Piovesan tem razão. Eu escrevi um exemplo para alguém outro dia e deve servir. Vou ver onde está. Abraço
Answer: Resposta didática para entender o que é e por quê é importante.
Você observará isso no mundo para aplicar depois em software ou projeto de circuitos.
Porta > (tem dois estados Aberta ou Fechada) - Considerando que ela está devidamente "instalada" heheheh.
Pense agora num Interruptor (Ligado / Desligado) que ATUA no estado de outro objeto chamado Lâmpada. (fica transitando entre esses dois estados eternamente) - claro para simplificar não considerei o estado dela: Queimada.
Agora, trazendo para os dias atuais, pense num sensor de temperatura conectado a uma placa de Arduino: (desligado) (ligado) (incomunicável) - estados possíveis. Exemplo: quando o sensor está incomunicável, não teve alguma resposta ao tentar se obter a temperatura, tem que tomar alguma decisão: enviar o email para alguém trocar aquele sensor, avisar, registrar num log de sistema, enfim (por si só O ESTADO DELE, pode interferir no comportamento TODO do sistema). Por isso que definir o estado de objetos é tão importante.
Diagramas de máquinas de estado da UML servem para vc poder mapear todos os estados possíveis de um determinado objeto (seja objeto do mundo real ou de software).
A parte interessante é que você pode observar a aplicação de diagramas de máquinas de estado em projetos de circuitos / IoT. Exemplo: Se o estado do sensor de temperatura é "defeito", toma uma decisão específica ou modifica o estado de outro objeto que faz parte do sistema como um todo.
A importância disso é não "esquecer" de todas as possibilidades e estados possíveis e acabar deixando alguma situação de projeto passar.
Imagine agora um sistema de e-commerce qualquer. Nele certamente existe o objeto PEDIDO.
O pedido pode transitar entre os diversos estados: FATURADO (o cliente acabou de fazer e pagar - um novo pedido com a nota fiscal já gerada) - EM SEPARAÇÃO (no setor de logística para poder ser despachado) - A CAMINHO (sendo transportado) - ENTREGUE / EXTRAVIADO ou Roubado pelos CORREIOS hahahahahahhahaha (pessoal é brincadeira - apenas para não esquecer do exemplo)...
Importante você compreender: Cada vez que um determinado objeto muda de estado, pode interferir no comportamento sistêmico como um todo ou de outros objetos e partes de um sistema e, respondendo sua pergunta: É importante projetar ou pelo menos levantar todos os estados possíveis dos objetos importantes do sistema para que você não esqueça de detalhes importantes ou de situações não previstas de projeto O QUANTO ANTES.
Quer ver um exercício legal? Estado de uma câmera de vigilância WiFi. Pense na máquina de estados dela (não é ligada/desligada não ... é interessante).
Comment: Bacana, gostei das analogias :)


How to redirect ip to address iptables

Question: I would like to know if is there a way to redirect the internal ip to another ip, but the second ip is Dynamically so i need to put an address, is there a way?
Example:
<code>iptables -t nat -I OUTPUT --dest 192.168.1.26 -p tcp -j DNAT --to-dest dynamicip.noip.org
</code>
Any help is appreciated !
Answer: No, I'm afraid that that is not possible.

How to redirect ip to address iptables


how to display linear layout only when keyboard is up?

Question: I want to display a linear layout with some textviews in it only when my soft keyboard is up. When soft keyboard is hidden the layout also should gone.how can i do that?
EDIT
PFB the picture. Like this i want to display those options only when keyboard is up.And these options will be hidden if there is not keyboard present.
Comment: You've 1,117Rep and you're asking a question like this, it looks weird. Review what you just ask and edit the question and add some code
Answer: check visibility of software keyboard in Android
How to check visibility of software keyboard in Android?
Comment: I know to check the visibility of the keyboard.I want to pin my linear layout with softkeyboard when it is up.
Answer: <code><activity android:name=".Book_Group"
 android:configChanges="orientation|keyboard|keyboardHidden|screenSize|screenLayout|uiMode"
 android:label="@string/title_activity_book__group"
 android:windowSoftInputMode="adjustPan|adjustResize" />
</code>

ADD IT INTO MANIFIST FILE LIKE THIS


Comparar variáveis com valores de uma tabela SQL Server C#

Question: Eu tenho 1 tabela com 2 colunas (Code e Value). Estou a escrever um código em C# e tenho uma variável taxpercentage. O que eu preciso é de retornar o valor da coluna Code, quando a variável taxpercentage for igual ao valor da coluna Value. Tenho ler a coluna Value e quando encontrar correspondência, retorno o Code.

Por exemplo:
Se a minha variável for 23.00, quero retornar o 6 !
Eu experimentei este código, mas não funciona:
conn.Open();
SqlCommand command = new SqlCommand("SELECT Value, Code FROM IVA", conn);
SqlDataReader rd = command.ExecuteReader();

if(rd.HasRows)
{
   while (rd.Read())
   {
      if (taxpercentage == rd["Value"].ToString())
      {
         codeiva = rd["Code"].ToString();
         Console.WriteLine(codeiva);
      }
   }
}
conn.Close();

Alguém sabe como posso fazer ?
Comment: Como você está atribuindo valor na variável `taxpercentage`?
Comment: Coloca a declaração da variável `taxpercentage`
Comment: A variável taxpercentage é um valor que eu estou a ler de um ficheiro xml, assim:   XmlNodeList taxlist = doc.GetElementsByTagName("Tax");
                foreach (XmlNode xn1 in taxlist)
                {
                    string taxpercentage = xn1["TaxPercentage"].InnerText;
                }
Answer: Sugiro que execute a pesquisa diretamente no servidor de banco de dados, para evitar que todo o conteúdo da tabela IVA trafegue pela rede.
SELECT Code from IVA where Value = @taxpercentage

Desta forma somente trafegará pela rede as linhas que atendam ao requisito. E o processamento da pesquisa ocorrerá no servidor de banco de dados.
Comment: e depois como é que eu posso atribuir o valor que receber a uma variável ? Eu recebo o valor do Code respetivo e quero pôr esse valor numa variável chamada "codeiva"


The gradient in the boundary of a surface

Question: Let the surface in question be the annulus $S := S^1 \times [0,1]$, just as an example.
Let $f:S \longrightarrow \mathbb R$ be a $C^\infty$ function.
Question:
Is there a way to adapt and define the derivative at a point of the boundary of S?
(My objective with this is to adapt, somehow, the implicity function theorem at a neighborhood of a boundary point).
Comment: Isn't differentiation defined on an open set?
Comment: @Divide1918 Yes, that is why I want to adapt the definition.
Answer: It's helpful to consider the half-space $X=[0,\infty)\times \mathbb{R}^{d-1}$, which is the local model for a $d$-dimensional manifold with boundary. In that case one typically defines a function $f:X\rightarrow \mathbb{R}$ to be smooth, if there is a smooth function $\tilde f:\mathbb{R}^d\rightarrow \mathbb{R}$ with $\tilde f\vert_X=f.$ One can then define $\nabla f(x) = \nabla \tilde f(x)$ and you can check that for $x\in \partial X$ this is independent of the choice of extension $\tilde f$.
Requiring $f$ to have  a smooth extension across $\partial X$ may feel a bit like cheating. But this is in fact equivalent to $f$ being smooth in $(0,\infty)\times \mathbb{R}^{d-1}$ (which is an open set) and all derivatives of $f$ having continuous limits as one approaches $\partial X$. See e.g. here.


How do you change the MIME on Amazon S3?

Question: I am trying to change a mp4 video file's MIME that is stored on Amazon S3.
I tried searching under the properties listing when you right click and then the meta-data drop down. But there is no option for that there.
How do you I do that?
Comment: I am using presigned post to upload the docs . how can i set the metadata from there
Answer: The issue does seem be to change the MIME in S3.
To be more specific, you need to log into Amazon S3 Console

Right on the file you need to change (or select them)
click on properties
click on metadata
HIGHLIGHT the text in "value" category (because "video/mp4" will not be in the drop down)
manually type in "video/mp4"
hit save
Comment: this will change all existing files, but is there a way to set the default type for all mp4 uploads to automatically use video/mp4 ?
Answer: To change the content type, find the object in the AWS Console, go to the Properties tab and under Metadata add a Content-Type key with the required MIME type as the value for that key (e.g. video/mp4).