Como publicar um projeto Angular CLI (Angular 4) no meu servidor?

Question: Estou aprendendo Angular CLI e consegui fazer uma aplicação rodar a aplicação em localhost do ng serve. Porém, queria colocar essa aplicação no meu site hospedado na Hostinger, apenas para testar e etc.
Então, fui jogar todos os arquivos (26 mil arquivos) da aplicação no site por casa do "node_modules".
Pesquisando vi que tinha que fazer o "ng build". Fiz o "ng build", ele criou a pasta "dist", fui jogar essa pasta no site, ele subiu tudo certinho, mas não executa os componentes, fica apenas no index.html.
Gostaria de saber como eu faço para colocar essa aplicação no meu site?
index.html (Dentro da pasta "dist"):
    "

      Angular Reddit

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.4/semantic.min.css">

  <app-root>Carregando...</app-root>

"

package.json:
{
  "name": "angularreddit",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/common": "^4.0.0",
    "@angular/compiler": "^4.0.0",
    "@angular/core": "^4.0.0",
    "@angular/forms": "^4.0.0",
    "@angular/http": "^4.0.0",
    "@angular/platform-browser": "^4.0.0",
    "@angular/platform-browser-dynamic": "^4.0.0",
    "@angular/router": "^4.0.0",
    "core-js": "^2.4.1",
    "rxjs": "^5.1.0",
    "semantic-ui": "^2.2.10",
    "zone.js": "^0.8.4"
  },
  "devDependencies": {
    "@angular/cli": "1.0.4",
    "@angular/compiler-cli": "^4.0.0",
    "@types/jasmine": "2.5.38",
    "@types/node": "~6.0.60",
    "codelyzer": "~2.0.0",
    "jasmine-core": "~2.5.2",
    "jasmine-spec-reporter": "~3.2.0",
    "karma": "~1.4.1",
    "karma-chrome-launcher": "~2.1.1",
    "karma-cli": "~1.0.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "karma-coverage-istanbul-reporter": "^0.2.0",
    "protractor": "~5.1.0",
    "ts-node": "~2.0.0",
    "tslint": "~4.5.0",
    "typescript": "~2.2.0"
  }
}

inline.bundle.js:
/******/ (function(modules) { // webpackBootstrap
/******/    // install a JSONP callback for chunk loading
/******/    var parentJsonpFunction = window["webpackJsonp"];
/******/    window["webpackJsonp"] = function webpackJsonpCallback(chunkIds, moreModules, executeModules) {
/******/        // add "moreModules" to the modules object,
/******/        // then flag all "chunkIds" as loaded and fire callback
/******/        var moduleId, chunkId, i = 0, resolves = [], result;
/******/        for(;i < chunkIds.length; i++) {
/******/            chunkId = chunkIds[i];
/******/            if(installedChunks[chunkId])
/******/                resolves.push(installedChunks[chunkId][0]);
/******/            installedChunks[chunkId] = 0;
/******/        }
/******/        for(moduleId in moreModules) {
/******/            if(Object.prototype.hasOwnProperty.call(moreModules, moduleId)) {
/******/                modules[moduleId] = moreModules[moduleId];
/******/            }
/******/        }
/******/        if(parentJsonpFunction) parentJsonpFunction(chunkIds, moreModules, executeModules);
/******/        while(resolves.length)
/******/            resolves.shift()();
/******/        if(executeModules) {
/******/            for(i=0; i < executeModules.length; i++) {
/******/                result = __webpack_require__(__webpack_require__.s = executeModules[i]);
/******/            }
/******/        }
/******/        return result;
/******/    };
/******/
/******/    // The module cache
/******/    var installedModules = {};
/******/
/******/    // objects to store loaded and loading chunks
/******/    var installedChunks = {
/******/        4: 0
/******/    };
/******/
/******/    // The require function
/******/    function __webpack_require__(moduleId) {
/******/
/******/        // Check if module is in cache
/******/        if(installedModules[moduleId])
/******/            return installedModules[moduleId].exports;
/******/
/******/        // Create a new module (and put it into the cache)
/******/        var module = installedModules[moduleId] = {
/******/            i: moduleId,
/******/            l: false,
/******/            exports: {}
/******/        };
/******/
/******/        // Execute the module function
/******/        modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/        // Flag the module as loaded
/******/        module.l = true;
/******/
/******/        // Return the exports of the module
/******/        return module.exports;
/******/    }
/******/
/******/    // This file contains only the entry chunk.
/******/    // The chunk loading function for additional chunks
/******/    __webpack_require__.e = function requireEnsure(chunkId) {
/******/        if(installedChunks[chunkId] === 0)
/******/            return Promise.resolve();
/******/
/******/        // an Promise means "currently loading".
/******/        if(installedChunks[chunkId]) {
/******/            return installedChunks[chunkId][2];
/******/        }
/******/        // start chunk loading
/******/        var head = document.getElementsByTagName('head')[0];
/******/        var script = document.createElement('script');
/******/        script.type = 'text/javascript';
/******/        script.charset = 'utf-8';
/******/        script.async = true;
/******/        script.timeout = 120000;
/******/
/******/        if (__webpack_require__.nc) {
/******/            script.setAttribute("nonce", __webpack_require__.nc);
/******/        }
/******/        script.src = __webpack_require__.p + "" + chunkId + ".chunk.js";
/******/        var timeout = setTimeout(onScriptComplete, 120000);
/******/        script.onerror = script.onload = onScriptComplete;
/******/        function onScriptComplete() {
/******/            // avoid mem leaks in IE.
/******/            script.onerror = script.onload = null;
/******/            clearTimeout(timeout);
/******/            var chunk = installedChunks[chunkId];
/******/            if(chunk !== 0) {
/******/                if(chunk) chunk[1](new Error('Loading chunk ' + chunkId + ' failed.'));
/******/                installedChunks[chunkId] = undefined;
/******/            }
/******/        };
/******/
/******/        var promise = new Promise(function(resolve, reject) {
/******/            installedChunks[chunkId] = [resolve, reject];
/******/        });
/******/        installedChunks[chunkId][2] = promise;
/******/
/******/        head.appendChild(script);
/******/        return promise;
/******/    };
/******/
/******/    // expose the modules object (__webpack_modules__)
/******/    __webpack_require__.m = modules;
/******/
/******/    // expose the module cache
/******/    __webpack_require__.c = installedModules;
/******/
/******/    // identity function for calling harmony imports with the correct context
/******/    __webpack_require__.i = function(value) { return value; };
/******/
/******/    // define getter function for harmony exports
/******/    __webpack_require__.d = function(exports, name, getter) {
/******/        if(!__webpack_require__.o(exports, name)) {
/******/            Object.defineProperty(exports, name, {
/******/                configurable: false,
/******/                enumerable: true,
/******/                get: getter
/******/            });
/******/        }
/******/    };
/******/
/******/    // getDefaultExport function for compatibility with non-harmony modules
/******/    __webpack_require__.n = function(module) {
/******/        var getter = module && module.__esModule ?
/******/            function getDefault() { return module['default']; } :
/******/            function getModuleExports() { return module; };
/******/        __webpack_require__.d(getter, 'a', getter);
/******/        return getter;
/******/    };
/******/
/******/    // Object.prototype.hasOwnProperty.call
/******/    __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/    // __webpack_public_path__
/******/    __webpack_require__.p = "";
/******/
/******/    // on error function for async loading
/******/    __webpack_require__.oe = function(err) { console.error(err); throw err; };
/******/ })
/************************************************************************/
/******/ ([]);
//# sourceMappingURL=inline.bundle.js.map

Comment: @DanielGomes eu não recomendaria, em *nenhuma* circunstância, o desenvolvimento no host de produção. O processo inicial do Nicolas está correto - desenvolvimento local e publicação da versão de distribuição.
Comment: Está usando o plano VPS?
Comment: Daniel, não! Estou usando o plano gratuito da Hostinger!
Comment: Então é isso - você precisa de um servidor linux que você possa controlar, configurar, instalar módulos, etc. Dá uma olhada [aqui](https://www.hostinger.com.br/hospedagem-web) e você vai ver que o suporte do plano gratuito é pra PHP e não o que você está usando... :p
Comment: O que eu faria: primeiro arrumar um servidor que dê acesso a um prompt de comando do linux, depois eu usaria o [git](https://git-scm.com/) e um serviço como [esse](https://bitbucket.org/product) para salvar meu projeto inteiro. Depois no servidor linux, eu simplesmente daria o comando 'pull' do meu projeto inteiro, e depois 'npm install' - e tudo deveria estar rodando! Sugiro que você pesquise esses comandos e principalmente o 'git' (já tem bastante pergunta de como utilizar ele aqui, e o próprio serviço que eu indiquei te ajuda a fazer o básico, e é grátis).
Comment: Muito obrigado pelas dicas e pela resposta, Daniel! Eu vou procurar saber melhor sobre os comandos e provável que vou comprar um VPS. Tenho alguns projetos em PHP para clientes, creio que vai ser melhor hospedar tudo em um lugar só e eu mesmo gerenciar. Novamente, muito obrigado!
Comment: Eu escrevi errado, o comando completo que 'baixa' seu projeto pro servidor é 'git pull' - boa sorte! :)
Comment: @NícolasAigner você poderia postar aqui o conteúdo de `dist\index.html`? E, se possível, o seu `package.json` também?
Comment: @OnoSendai nem eu, +1
Comment: @OnoSendai, posto sim! Coloquei na pergunta os arquivos!
Comment: Perfeito - e como estão os conteúdos de `inline.bundle.js` e `main.bundle.js`? Estes são os arquivos responsáveis por carregar tanto as dependências quanto a sua aplicação. Eles estão corretamente populados?
Comment: @OnoSendai, consegui colocar o inline.bundle.js, o outro arquivo está gigante, mas está correto também. Creio que seja o que o Daniel Gomes falou, por não ser um VPS e etc.
Answer: Como você descobriu é necessário fazer um build da aplicação, ao fazer isso os arquivos gerados podem ser utilizados em qualquer servidor web.
Ex: no wampserve você pode copiar a pasta /dist do seu projeto angular para a pasta /www do wampserve e acessar localhost/dist e lá estará sua pagina em angular 4.
No seu caso você deve "subir" sua aplicação para a hospedagem da hostinger e configurar seu htaccess para redirecionar todas as requisições para o arquivo index.html, só dessa forma o angular poderá ter controle sobre as rotas.
adicione um arquivo .htaccess na pasta da sua hospedagem com essa regra abaixo para fazer essa configuração
    <IfModule mod_rewrite.c>
      RewriteEngine on
      RewriteCond %{REQUEST_FILENAME} -s [OR]
      RewriteCond %{REQUEST_FILENAME} -l [OR]
      RewriteCond %{REQUEST_FILENAME} -d
      RewriteRule ^.*$ - [NC,L]

      RewriteRule ^(.*) /dist/index.html [NC,L]
    </IfModule>

Obs: coloque no seu servidor somente os arquivos da pasta dist, os arquivos gerados devem ser usados em conjunto e não separadamente pois um depende do outro para funcionar.
ps: eu também uso a hospedagem da hostinger para testar minhas aplicações em angular e para trabalhar com o angular só é preciso de um servidor de conteúdo estático e nada mais.
Comment: Rodrigo, boa noite! Obrigado por responder tão bem minha dúvida.
Consegui fazer o Angular funcionar no Wamp graças as tuas dicas.
Porém, joguei a pasta dist para o servidor e não funcionou. Creio que não vai funcionar com o plano gratuito e também não existe mais o .htaccess, tentei criar um, mas não funcionou, quando vai para lá, em 5 minutos ele é apagado. Eu vou comprar amanhã um mês do VPS da Hostinger para fazer alguns testes, se tudo der certo, beleza! Se não der, paciência! Muito obrigado pela resposta!
Answer: <!doctype html>

  <meta charset="utf-8">
  <title>AngularHelloWord</title>
  <base href="/">

Retire a tag base do index, para que seus arquivos js sejam direcionados corretamente.
Answer: Cara, pra mim funcionou fazendo o build assim:
    ng build --base-href /nomeDoSeuProjeto/

Depois só fazer o upload do projeto que está dentro da pasta dist/ para o servidor.
Fonte: https://angular.io/cli/build


Count occurrences of each tag using functional programming

Question: I've been trying to make a function that returns a Map<String, Int> with the key being a certain tag and the value being the number of occurrences.
The object (simplified) from which I need to extract the info:
class Note {
   List<String> tags
}

The function so far:
private fun extractTags(notes: List<Note>): Map<String, Int> {
    return notes.map { note -> note.tags }
                .groupBy { it }
                .mapValues { it.value.count() }
}

Right now the compiler gives me a return type mismatch of Map<(Mutable)Set<String!>!, Int> and I'm not certain I'm getting the desired result (as I still can't test this properly).
I'm expecting a result something in the lines of:
(tag1, 1)
(tag2, 4)
(tag3, 14)
...

Answer: You can using Iterable#asSequence just like as Java-8 stream-api in Kotlin. then using Sequence#flatMap to merge all tags into a Sequence , and then using Sequence#groupingBy to counting each tag, for example:
private fun extractTags(notes: List<Note>): Map<String, Int> {
    return notes.asSequence()
                .flatMap { it.tags.asSequence() }
                .groupingBy { it }.eachCount()
}

Note: both Sequence#flatMap and Sequence#groupingBy are intermediate operations, which means if the terminal operation Grouping#eachCount is not called. all of the operations on the Sequence is not ran.
Comment: Note: when use the stream-api you only need to transform the data source only once rather than many times, and it uses lower-memory than `Iterable#flatMap` since it will collect the original data source to another data source in each terminal operation. if you have many data & operations on the stream, you maybe get the `OutOfMemoryError`. e.g: a `Sequence` read a huge file lines by `BufferedReader.lines`. for more details you can see [@hotkey's answer](https://stackoverflow.com/a/35630670/4465208).
Answer: While the already accepted answer unarguably solves your problem, I feel like there's a bit of an "everything looks like a nail when you have a hammer" thing going on here.
The essence of that answer is that flatMap, groupingBy, and eachCount are the methods you need to solve your problem, however, using sequences here seems completely unnecessary.
Here's the code that just operates on/with regular collections:
private fun extractTags(notes: List<Note>): Map<String, Int> {
    return notes.flatMap { it.tags }
            .groupingBy { it }
            .eachCount()
}

I'd like to argue that this is a better solution than the one using sequences, because:

It produces the same results, since it uses the same operators.
The code is just simpler and easier to read without them.
The transformations here are simple and few, sequences get useful when you have long chains.
We are probably operating on relatively small data sets here. In my own quick measurements, the solution using sequences was about 10% faster when there are a million notes, but 17% slower when there are only ten thousand. I'll wager to guess you're closer to the latter in size of your lists. Sequences have overhead.
We aren't making use of the laziness provided by sequences at all, since we want to evaluate and return the results immediately.

You can see an excellent comparison of the two ways with pros and cons here as well for more details.
Answer: Here is your code modified to work.  I changed map to flatMap.  I also provided a version implemented as an extension function.  Yours was failing because map> was producing a List<List<String>> where you were expecting List<String> (hence, flagMap).
fun extractTags(notes: List<Note>): Map<String, Int> {
    return notes.flatMap { it.tags } // results in List<String>
            .groupBy { it } // results in Pair<String, List<String>>
            .mapValues { it.value.count() }
}

fun Iterable<Note>.extractTags(): Map<String, Int> {
    return this.flatMap { it.tags } // results in List<String>
            .groupBy { it } // results in Pair<String, List<String>>
            .mapValues { it.value.count() }
}

And here is some code to test it with
import kotlin.collections.*

fun main(vararg args: String) : Unit {
    var notes = ArrayList<Note>()
    notes.add(Note(List<String>(1) { "tag1" }))
    notes.add(Note(List<String>(4) { "tag4" }))
    notes.add(Note(List<String>(14) { "tag14" }))

   for((first,second) in extractTags(notes))
       println("$first: $second")
   for((first,second) in notes.extractTags())
       println("$first: $second")
}

class Note {
    constructor(strings: List<String>) {
        tags = strings
    }
    var tags: List<String>
}

Comment: `groupingBy { it }.eachCount()` is more preferable in terms of memory consumption.
Comment: @Ilya - yes, you're right.  I chose to use `groupBy` and `mapValues` because the OP used these and my main intent was to point to the specific problem with the original example.  The accepted answer uses `groupingBy { it }.eachCount()`, I felt that would suffice.
Answer: Excuse me for this late solution, but it is be the best one: as I think when you are using Kotlin you have the standard library that give you a better syntax, shorter and cleaner than the Java 8 streams.
private fun extractTags(notes: List<Note>): Map<String, Int> = notes.flatMap { it.tags }//list of String
        .groupBy { it }//list of Map.Entry<String,List<String>> //List<Map.Entry<String,List<String>>>
        .map {
            Pair(it.key, it.value.size)
        }//list of pairs(tag, count) // List<Pair(String,Int)
       .toMap()//creat a map from the list of pairs


Function build() only render first array with for loop

Question: Currently working on a custom module and I'm trying to render a set of arrays that are created through a blockForm and a blockSubmit this ones are working correctly. blockForm is creating the fields of the form correctly and blockSubmit is saving the data of the fields correctly. I tested this several times with positive results. And this two are working with a FOR loop.
Now i'm trying to render the data of this blockForm with public function build() and a FOR loop. But is only rendering the first array and as it is configurated at the moment is should be four arrays.
This is the code:
FssContentBlock.php
<?php

namespace Drupal\fss_content\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Form\FormStateInterface;

/**
 * Provides a 'Fussion Content Block' Block.
 *
 * @Block(
 *   id = "fss_content",
 *   admin_label = @Translation("Fussion Content Block"),
 *   category = @Translation("Fussion Content Block"),
 * )
 */

class FssContentBlock extends BlockBase implements BlockPluginInterface {

  /**
   * {@inheritdoc}
   */
  public function build() {//ONLY RENDER FIRST ARRAY...
    $config = $this->getConfiguration();
  $config2 = \Drupal::config('fss_content.settings');
  $config2->get('fss_content.paragraphs');
  $qtty = $config2->get('fss_content.paragraphs');

   for ($i = 0; $i < $qtty; $i ++) {
     $i = $config['fss_content_paragraph'][$i];
     $block1 = array
       (
        $i => array
         (
          '#prefix' => '
',
          '#suffix' => '
',
          '#markup' => t('@i', array('@i' => $i,)),
         ),
       );
   }

    $block2 = array
      (
        'test' => array
        (
          '#markup' => $qtty . '
' . $i . '
', //TESTING $qqty VALUE. IT IS CORRECT... BUT WEIRD $i VALUE...
        )
      );
    $allblocks = array_merge($block1, $block2);
  return $allblocks;

/* //THIS CODE WORK... BUT NEED IT TO WORK WITH (FOR - LOOPS)
    $p0 = $config['fss_content_paragraph']['0'];
    $p1 = $config['fss_content_paragraph']['1'];
    $p2 = $config['fss_content_paragraph']['2'];
    $p3 = $config['fss_content_paragraph']['3'];

    $block = array
        (

      'p0' => array
            (
       '#prefix' => '
',
       '#suffix' => '
',
             '#markup' => t('@p0', array('@p0' => $p0,)),
            ),
      'p1' => array
            (
       '#prefix' => '
',
       '#suffix' => '
',
             '#markup' => t('@p1', array('@p1' => $p1,)),
            ),
      'p2' => array
            (
       '#prefix' => '
',
       '#suffix' => '
',
             '#markup' => t('@p2', array('@p2' => $p2,)),
            ),
      'p3' => array
            (
       '#prefix' => '
',
       '#suffix' => '
',
             '#markup' => t('@p3', array('@p3' => $p3,)),
            )

    );
    return $block;  */
  }//End build

  /**
  * {@inheritdoc}
  */
  public function blockForm($form, FormStateInterface $form_state) { //THIS WORK...
    $form = parent::blockForm($form, $form_state);
    $config = $this->getConfiguration();

    $config2 = \Drupal::config('fss_content.settings');
    $config2->get('fss_content.paragraphs');
    $qtty = $config2->get('fss_content.paragraphs');

    $form['fss_content_paragraph'] = array(
      '#type' => 'fieldset',
      '#tree' => TRUE,
    );

    for ($i = 0; $i < $qtty; $i ++) {
      $num = $i + 1;
      $form['fss_content_paragraph'][$i] = array(
        '#type' => 'textarea',
        '#title' => 'Parrafo N°' . $num,
        '#default_value' => isset($config['fss_content_paragraph'][$i]) ? $config['fss_content_paragraph'][$i] : '',
      );
    }

  return $form;

  }
  /**
   * {@inheritdoc}
   */
  public function blockSubmit($form, FormStateInterface $form_state) { //THIS WORK...
    parent::blockSubmit($form, $form_state);
    $values = $form_state->getValues();

    $config2 = \Drupal::config('fss_content.settings');
    $config2->get('fss_content.paragraphs');
    $qtty = $config2->get('fss_content.paragraphs');

    for ($i = 0; $i < $qtty; $i ++) {
      $this->configuration['fss_content_paragraph'] = $form_state->getValue('fss_content_paragraph',$i);
    }
  }//End blockSubmit

}//End Class

Explanation of how the module work:

The module has a configuration option that sets the amount of fields the Administrator user needs.

Then the form with the amount of fields is created:

And this is what my code is rendering at this moment:

As you can see is rendering the first array but nothing else.
Now, if you take a look at the test i'm making the $qtty value is correct witch is the value of the amount of fields i need.
But the last value of $i is weird (last_value_$i = "ABCE"). Because is taking the first three characters of the first array (First_array = "ABCD") and the first character of my second array (Second_array= "EFGH")
Any ideas how can i solve this problem? Thank you!
Comment: Maybe you need to define your $block1 variable before your loop block starts?
like $block1 = [];
Comment: Just tested... but no.. didn't work...
Comment: At the `public function build()` Is it possible to replace the FOR loop with a foreach? I've being trying but haven't made it right i think how would be the right way to do it that way?
Comment: Or a foreach nested into a FOR loop.. ?
Answer: After hours and hours... i finally found the correct way to implement this for loop at function build()
This is the final code:
<?php

namespace Drupal\fss_content\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Form\FormStateInterface;

/**
 * Provides a 'Fussion Content Block' Block.
 *
 * @Block(
 *   id = "fss_content",
 *   admin_label = @Translation("Fussion Content Block"),
 *   category = @Translation("Fussion Content Block"),
 * )
 */

class FssContentBlock extends BlockBase implements BlockPluginInterface {
  /**
   * {@inheritdoc}
   */
  public function build() {
    $config = $this->getConfiguration();
  $config2 = \Drupal::config('fss_content.settings');
  $config2->get('fss_content.paragraphs');
  $qtty = $config2->get('fss_content.paragraphs');

  for ($i = 0; $i < $qtty; $i++) {
    ${'p' . $i} = $config['fss_content_paragraph'][$i];
    $block1['p' . $i]['#prefix'] = '
';
    $block1['p' . $i]['#suffix'] = '
';
    $block1['p' . $i]['#markup'] = ${'p' . $i};
  }

  return $block1;

  }//End build
  /**
  * {@inheritdoc}
  */
  public function blockForm($form, FormStateInterface $form_state) {
    $form = parent::blockForm($form, $form_state);
    $config = $this->getConfiguration();

    $config2 = \Drupal::config('fss_content.settings');
    $config2->get('fss_content.paragraphs');
    $qtty = $config2->get('fss_content.paragraphs');

    $form['fss_content_paragraph'] = array(
      '#type' => 'fieldset',
      '#tree' => TRUE,
    );

    for ($i = 0; $i < $qtty; $i ++) {
      $num = $i + 1;
      $form['fss_content_paragraph'][$i] = array(
        '#type' => 'textarea',
        '#title' => 'Parrafo N°' . $num,
        '#default_value' => isset($config['fss_content_paragraph'][$i]) ? $config['fss_content_paragraph'][$i] : '',
      );
    }

  return $form;

  }//End blockForm
  /**
   * {@inheritdoc}
   */
  public function blockSubmit($form, FormStateInterface $form_state) {
    parent::blockSubmit($form, $form_state);
    $values = $form_state->getValues();

    $config2 = \Drupal::config('fss_content.settings');
    $config2->get('fss_content.paragraphs');
    $qtty = $config2->get('fss_content.paragraphs');

    for ($i = 0; $i < $qtty; $i ++) {
      $this->configuration['fss_content_paragraph'] = $form_state->getValue('fss_content_paragraph',$i);
    }
  }//End blockSubmit

}//End Class

Hopefully some day this will help someone else...


Let $G$ be solvable. Let $g\in G$. What can we say about $g=[g^{x_1}, g^{y_1}]\dots [g^{x_n}, g^{y_n}]$ with $x_i,y_i\in G,i=1,...,n$?

Question: We have $G$ a solvable group. Let g be an element of G. What can we say about
$$g = [g^{x_{1}}, g^{y_{1}}]\cdot\cdot\cdot[g^{x_{n}}, g^{y_{n}}]$$
with $x_{i}, y_{i} \in G, i = 1,..., n$?
Comment: @diracdeltafunk Considering $x_i \in G$, I suspect that this is notation for "the conjugate of $g$ by $x_i$".
Comment: By $g^{x_i}$ do you mean a power of $g$? If so, this equation is false unless $g = e$ -- this is just because powers of $g$ always commute.
Comment: @DuncanRamage Thank you, that was silly of me! I should have read the question carefully.
Comment: By the way, in this forum,  you are expected to offer your own thoughts on the question when asking for help. You are getting downvotes and votes to close because you failed to do this.
Answer: If $G$ is finite, then by an absurd-sounding result of John Wilson, $n\leq 55$. This appears in J. London Math. Soc., entitled Finite Axiomatization of Finite Soluble Groups. The theorem is as follows:
A finite group $G$ is soluble if and only if no non-trivial element $g$ can be expressed as a product of 56 commutators of conjugates of $g$.
It is likely that the number 56 can be reduced.
Comment: I call this 'absurd-sounding' because when I was a Ph.D. student and John told me he had proved this, I didn't believe him. While some people think the best theorems are those people respond "Wow, X proved that?" to, I personally think the best theorems are those to which you respond "What? No, you must have got the statement wrong. There's no way that's true."
Comment: Well I challenge you to come up with any such expression in a soluble group! (I suspect that it is the "if" past of Wilson's theorem that is tricky to prove.)


Error SIGABRT na UITableView

Question: O código abaixo compila mas depois apresenta o erro 12main SIGABRT no appdelegate.swift, o que fazer? E a tableview ja esta ligada com data source e delegate a viewcontroller.

2016-01-09 16:34:54.433 Tabelas[17730:805874] *** Terminating app due
  to uncaught exception 'NSInternalInconsistencyException', reason:
  'Could not load NIB in bundle: 'NSBundle

  (loaded)' with name 'Main''

Arquivo viewcontroller.swift
import UIKit

class ViewController: UIViewController, UITableViewDelegate {

    var cellContent = ["Ana, Lucia, Maria, Ja"]

    override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
        }

        func tableView(tableView: UITableView, numberOfRowInSection section: Int) -> Int {
        return cellContent.count
        }

        func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")

        cell.textLabel?.text = cellContent[indexPath.row]

        return cell
        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }

}

Answer: Esse erro geralmente pode ocorrer por 2 motivos:

Verifique se seu storyboard está adicionado no seu Copy Bundle Resources:

Verifique se nas propriedades do seu .storyboard ou .xib está linkado com o target do seu projeto:

Error SIGABRT na UITableView


SOLVED $\mathbb{Z}_{(p)}/{(p)} \cong \mathbb{Z}_p$

Question: in my algebra exam I had to prove that $\mathbb{Z}_{(p)}/{(p)}
\cong \mathbb{Z}_p$, where $\mathbb{Z}_{(p)}
$ is the subset of $\mathbb{Q}$ containing only fraction with the denominator which is not divided by $p$, $p$ prime.
Notation: $\mathbb{Z}_p$ is the field with $p$ elements.
I have not done this exercise, can you give me an help please?
Comment: Is $\mathbb Z_p$ meant to be the $p$-adic integers? If not, a better notation would be in order.
Comment: Ok thank you! I have understood: $\frac{m}{n} \longmapsto mn^{-1}$. It works!
Comment: @user3343783 Rather than adding "SOLVED" to the title, you should supply an answer.
Comment: Are sure you've written this right?
Comment: Now it is correct, sorry.
Comment: Do you mean $\frac{pq + r}{m} \longmapsto r$?
Comment: I think the simplest direct approach is simply to write down the map $\mathbf{Z}_{(p)} \to \mathbf{F}_p$ then prove it has the right kernel. This fact is almost "obvious" if you write out what has to happen to elements in more familiar terms.
Comment: I am afraid that my map does not work, what does you mean with "familiar terms"?
Comment: I have written the map in my last comment. Nevertheless thank you everyone!
Answer: (I assume all rings are commutative)
In general, if $\varphi : R \to S$ is any ring homomorphism, and $a \in R$ has the property that $\varphi(a)$ is invertible in $S$, then we also get a homomorphism $\varphi : R[a^{-1}] \to S$ given by $\varphi(x / a^n) = \varphi(x) / \varphi(a^n)$.
You also have the property that $\varphi(x/a^n) = 0$ if and only if $\varphi(x) = 0$.
As a special case, if $\mathfrak{p}$ is a prime ideal of an integral domain $R$, then every element of $R \setminus \mathfrak{p}$ is invertible after mapping to the residue field $R / \mathfrak{p}$.
Correspondingly, the quotient map extends to a map $R_{\mathfrak{p}} \mapsto R/\mathfrak{p}$, and the kernel is precisely $\mathfrak{p}_{\mathfrak{p}} = \mathfrak{p} R_{\mathfrak{p}}$: the ideal of all elements whose numerator is in $\mathfrak{p}$.
(I think this is true for all rings, not just domains, but my memory is fuzzy on this point)
In your particular example, we are mapping the fraction $x/y$ to the quotient $x/y \pmod{p}$. We can divide by $y$ modulo $p$, because $y$ and $p$ are relatively prime.
Comment: I don't agree with this: "In general, if φ:R→S is any ring homomorphism, and a∈R has the property that φ(a) is invertible in S" but the rest seems correct. Thank you!


Am I hyperventilating during racing? Extreme feeling of being tired/out of breath during mountain bike races

Question: I race mountain bikes and I have had a number of races where I have been completely out of breath and unable to go faster because I feel like I am at my limit. I am experienced and I am a good bike handler with a very high level of fitness in both running and cycling. I have been training all winter, but this weekend I did a race and I came 38th out of 50. At the same race last year I came 6th and ahead of the guy who won it this year. I know my ability level and I can ride very fast at times. But this was staggeringly bad. I felt like I was going as hard as i could, even though I must have been going very slow looking at the times. It has happened in other races too but this weekend was by far the worst.
I always get nervous when I ride with other people too. Sometimes it feels like its not even me on the bike and I am a passenger to this feeling of not being able to keep up. I know its not a fitness thing as I am a fast runner and a strong road cyclist and I know I have very good bike handling skills. But sometimes it just doesn't feel like me when I try to go fast under pressure.
I can't swim and I get a similar feeling of being out of breath, despite having done little to no physical exertion, when I try. With all the work I have put in, this complete and seemingly inexplicable capitulation was pretty heartbreaking.
Answer: "Hitting the wall", or "pinging" is colloquial for having your heart rate bouncing off your Max Heart Rate (MHR).  Being overly out of breath and head-dizzy are symptoms.
"Bonking" is colloquial for when your muscles run out of glucose-etc.  Your entire body aching – especially your shoulders – is a symptom.
These are two very distinct feelings; but are somewhat interrelated.  Sounds like you're suffering from the first.  In my experience the first can start to happen more dramatically (noticeably) as you get older – "older" as in 36yo mind you : /
I had the first example above happen so bad one time in Santa Cruz that I had to get off my bike and walk.  I had had plenty of food, and absolutely knew myself – I knew I wasn't bonking and couldn't understand what was happening to me – but I pushed too hard for too long to try and keep up with an amazing athlete on a hill climb.  This is colloquially  called "exploding".  You see it all the time in professional bike races during hard climbing events.  Once you "explode" you can't recover while still providing output to the bike.  This happens easier as you get older – even though you think you are at exactly the same (or better) physical condition.
A third thing is "heat exhaustion".  This is a whole 'nother animal, and the most dangerous of the lot.  I've explored the depths of these three failure points quite a few times.  It's good to know where they are, but to be sure, one day you may "take it to the next level" and be completely shocked by what your body is doing,even though you think you understand what these things are...
You need a heart rate monitor, and to review the breadcrumb heart rate data for a race on your computer and see exactly what your heat is doing when you feel that way.  The numbers will be clear as day.  Or you can just look at your wrist/stem when riding and SEE you're "pinging" off your MHR – and if you stay there for long, you'll feel that "explosion" coming - irretrievably out of breath and often dizziness.
"That's rodeo" as far as bicycle racing (sans downhill, in most cases) – scrunching up against a predetermined heart/work rate and staying there.  The way this feels, as far as work output, can vary widely – hugely in fact – depending on altitude, food, sickness and countless other things – and especially nerves – but the numbers don't lie as far as optimizing your output.
Hope that helps.
Be careful.  Genetic predisposition for heart problems may onset.  A full checkup may be required – even if you're only 40-some years old.  People die from heat attacks in their 40s – even athletes.
EDIT: One final thought – as a runner you probably understand clearly the notion of stride length.  How extending your stride just slightly can drastically increase your speed for basically the same amount of work.  And like rowing, or swimming, where getting that extra "reach" - even an inch – can really make all the difference in the world for max top-speed.  I see breathing as exactly the same – especially road racing.  Take a big breath right now.  Then take another breath and exaggerate the total input – EXPAND the lungs.  THAT right there – training yourself to do that all the time when riding at your predetermined heart rate, will effectively be like having the extra few inches of "reach".
If you ride hard enough long enough – training – you just naturally acquire it.  But understanding what the desired end-state condition "looks like", can really help you get there ..faster.
When you "explode", as I've call it, that whole process of breathing is out of sync with your body.  To try best to work through an explosion in a race is exactly – precisely – directly – the art of breathing big and consistent, even though your body is saying you really don't need to – because it's overloaded and wants to make you slow the F' down.... it's making you hyperventilate with shorter 25% quick breaths.
Comment: Very informative, thank you. As you say its not bonking as the stages are short and this happens almost instantly. I think you may be on to something with the breathing. Its not something i ever focus on. As someone who has anxiety in other areas of life it affects me a lot and breathing better might help. I have been "in the zone" before and I havent had to think about anything while going very fast. Everything happens without thinking then and its a wonderful feeling. This was the antithesis of that. I am only 28 and there is no history of heart failure in my family.


How to locate select with Selenium, what controlled by javascript?

Question: I have the following select.
I want to select specified elements with Java Selenium. I tried these but none of them working, I always got
In normal usage If I click on the element its

no such element: Unable to locate element:...

Heres the my code(s):
driver.findElement(By.xpath("//option[@value=\"2498847\"]")).click();
        driver.findElement(By.xpath("/html/body/form/div/center/table/tbody/tr[2]/td[1]/font/select/option[2]")).click();
        driver.findElement(By.xpath("//html/body/form/div/center/table/tbody/tr[2]/td[1]/font/select/option[2]")).click();

Tired with Select class:
Select selector = new Select(this.driver.findElement(By.xpath("//select")));
selector.selectByVisibleText("Harris_SF_1");

What am I doing wrong?
Or Is it possible to simulate a javascript click and call this script with Selenium?
Html:
<select size="10" name="DropDown" onchange="fSaveSearch(document.Form1.DropDown);">
    <option value="1715424">1960 cypress creek N</option>
    <option value="2164877">Broker Closing Search</option>
    <option value="2498847">Harris_SF_1</option>
    <option value="1715426">Spring East</option>
    <option value="1715428">Spring Klein</option>
    <option value="1715429">Spring Klein Tomball</option>
    <option value="1715427">Spring NE</option>
    <option value="1715432">The Woodlands</option>
</select>

The final goal, to simulate a click on a option with specific visible text or value.
Edit: The site default iFrame is not that where these elements/js writter. If I change the iframe at the chrome console it works, but
driver.switchTo().frame("SearchFrame");

Doenst work am I change wrong?
Comment: So the element is located under frame right..So is the switchToFrame method throw any exception? And why dont you go by `name` selector.. `findElementByName('DropDown')`
Comment: Wht does the javascript onchange function do? Have you tried locating the select by name attribute?
Comment: When you bring the page up under google chrome, right click the target element, and under dev tools, right-click the high-lighted html, select Copy -> Copy XPath, what does the XPath string look like?
Comment: /html/body/form/div/center/table/tbody/tr[2]/td[1]/font/select/option[3]  this is the chrome suggested xpath.
Answer: Thanks Madhan  your comment solved the my problem.
But it only worked after I swtich to Defaultcontent, then I switch to the frame I need!
driver.switchTo().defaultContent();

then switch to the frame I needed:
driver.switchTo.frame("Frame_ID");


Model Theory and Topology Connections

Question: I have studied a bit of model theory, when I say "a bit" I have studied much more than is available to a typical undergraduate in the UK (i think, certainly from what I have seen) but I am sure this is all introductory material when looked at on a bigger scale (as things always are) but it is the area that I am going to pursue in my masters degree next year and hopefully beyond.
I have also done some very introductory material in topology, sort of covered a typical "intro to topology" book.
What I was wanting to ask about was the connections between the two. Now I know that there are many strong connections, but I am yet to actually come across any of these connections (apart from reading things which says they exists and hearing people talk about these connections very generally).
So I was wondering if anyone could give me some idea as to where the connections lie and some references for reading about this. I am aware that there is something called "o-minimality" but I have no idea what this is, so I was looking for some good references. Also as I stated above I have an introductory grasp on topology so is it the case that I need to learn much more before I can grasp these connections at all?
(I would also like to add that I am not really sure what I am asking about, as I'm sure is obvious from the question, so please feel free to tell me that I'm looking for the wrong things or my questions is too naive.)
As always any help is greatly appreciated.
Comment: What about first-order compactness being an instance of topological compactness?
Answer: You can start by reading Alan Dow's paper:

Dow, A. An introduction to applications of elementary submodels to topology. Topology Proc. 13 (1988), no. 1, 17–72. MR1031969
Comment: Very good suggestion. The paper is freely available from [Alan's home page](http://math.uncc.edu/~adow/) at UNCC.
Comment: @Brian: Thanks for catching that mistake!
Comment: You're welcome. I used to know Alan, so it kind of hit me in the eye.
Answer: Since you mention o-minimality I thought I should mention that this area of model theory which is closely related to (generalizations of?) semi-algebraic geometry and real-algebraic geometry. In particular, techniques from o-minimality have been used to make some progress towards solving the Andre-Oort Conjecture, I am referring to the paper by Jonathon Pila called "O-minimality and the Andre-Oort Conjecture for $\mathbb{C}^n$". For introductory papers on this subject, I suggest a paper by Thomas Scanlon called "O-minimality as an approach to the Andre-Oort conjecture" which can be found on Scanlon's academic webpage.
I would also like to mention another area of model theory with connection to analysis. It is called the Model Theory of Metric Structures. In this model theory, one changes the particular logic one is using and the definition of structure. Structures in this theory are now complete metric spaces, connectives are uniformly continuous real-valued functions, etc. You can find an introduction online (legally) for free. I forget the authors, but just search "Model Theory for Metric Structures."
Finally, there is also a thing called topological model theory, which was first proposed by Anand Pillay (I believe). There are lecture notes by Ziegler called "Topological Model Theory" published by Springer. I am sure you can find this in your university library.
Have fun
Answer: Most obvious examples come from considering the Stone space of complete types over a complete theory. For instance:

Compactness of the Stone space is closely related to compactness of first-order logic.
Any subset of the Stone space can be omitted simultaneously if it is meager (and the proof I know of even the simplest version of omitting types theorem uses Baire category theorem).
Formulas, and definable sets correspond exactly to clopen subsets of the Stone space.
Morley rank of a formula equals the Cantor-Bendixson rank of the clopen set defined by it (within a Stone space over an $\aleph_0$-saturated structure), similarly the multiplicity.
Type-definable sets are closed subsets of the Stone space (though right now I'm not certain if the converse is true).

For a different, more geometrical flavour of examples:

The automorphism group of any model has natural structure of topological group (with open sets defined by fixing the images and preimages of finite tuples).
For countable models, the group is, in fact, Polish.
For saturated countable models it is isomorphic to the Baire group $\omega^\omega$.

For different yet kind of examples, as far as I know, the theory of algebraic groups generalizes to definable groups (of finite Morley rank, I think) in suitable theories (but I know next to nothing about it, so I will not elaborate).
A different yet kind of use of topology comes from some particular theories related to particular topological spaces. For example, if we want to show something about real closed fields, we often only need to work in the real numbers, and if we want to show something about algebraically closed fields of characteristic $0$, we only need to work with the complex numbers. Both of these come with their natural topology and it can sometimes be used to show some (first-order) facts which would otherwise be hard to prove.

Model Theory and Topology Connections


Manually doing what the ModelAttribute annotation does in Spring MVC

Question: I have an action method on a Spring MVC controller that has an argument annotated with @ModelAttribute. However, I don't know at compile time what the type of this parameter will be - I know the abstract base type but not the derived type.
At runtime, I will be able to decide what class I am expecting and I will be able to get a new'd up instance of this class. However, I have no idea what code I should be calling to parse the request data in the same fashion that @ModelAttribute does.
I've looked around and it seems that if i can get a hold of a WebRequestDataBinder I can use that to populate my object, but for that I need a BinderFactory and this is where I kind of get lost.
Can anyone give me some pointers here - or tell me that I am looking at it the wrong way and need to do something else?
Answer: you can inject the model itself in your controllers method and access the attribute yourself.
@RequestMapping(...)
public void doStuff(ModelMap model) {
    Object attr = model.get("nameOfAttribute");
    // ...
}

Comment: Will this also work for complex objects, or just primitive values? How will it know what class to use?
Comment: @efdee it will work for whatever type you put in the model... when i consider your comment, it sounds like you are talking about `@RequestParam` and not about `@ModelAttribute`. Working with `@ModelAttribute` you normally control what is inside the model. maybe we talk at cross-purposes. could you provide some code to clearify what your are doing?
Comment: I thought `@RequestParam` is for parameters coming from the request mapping, eg. for '/users/:id', id would be a `@RequestParam`.

I have a form that contains data and I want to post it back to my controller. I've used `@ModelAttribute` in the past to get the data into an actual object. However, this particular form is built dynamically, so I can't at compile time specify the concrete class to use for parsing the data. I can, however, at runtime inspect some things so I end up knowing what type it will be.
Comment: Seems like my assumption about `@RequestParam` is wrong. I'll have to go back to the books and learn what the difference is exactly. Same question though, how do I invoke this manually inside the method, instead of using an annotation on the method parameter?


How to redirect everything to 404 not found?

Question: I need to block an entire Drupal 7 site from being indexed for a while. So, I think it is best to redirect everything to 404 instead of actually deleting all my files. How can I do that?
PS: using the following code on .htaccess does not seem to work.
RewriteEngine On
RewriteRule ^ - [R=404,L]

-- Update 03/04/2015 --
The server is nginx
Answer: If you are just trying to keep it from being indexed, you can just add
User-agent: *
Disallow: /

To the top of your robots.txt
Comment: This is one of a good option to use for being indexed, but I really need the 404. :(
Comment: since the nginx does not support .htaccess, I'm going to mark this as my answer. Ty @Patrick Ryan
Answer: You'll need to remove the existing Drupal rewrites for the moment; with them left in the .th access this code won't work.
Comment: I dont have anything in ".htaccess" beside those 2 lines of code.
Comment: I updated the question. The site is running on nginx, not sure does it really matter.
Comment: If the server is nginx .htaccess has no effect.


limit $\lim \:_{x\to \:0}\left(\frac{\sin^2\frac{x}{2}}{\sin x}\right)$

Question: how can I solve it?
$\displaystyle\lim_{x\to \:0}\left(\frac{\sin^2\frac{x}{2}}{\sin x}\right)$
Comment: $\sin x=2\sin\frac{x}{2}\cos\frac{x}{2}$
Answer: Use
$$\sin(x)=2\sin(\frac{x}{2})\cos(\frac{x}{2})$$
So
$$
\lim_{x \to 0} \frac{\sin^2(\frac{x}{2})}{2\sin(\frac{x}{2})\cos(\frac{x}{2})}=\lim_{x \to 0} \frac{\sin(\frac{x}{2})}{2\cos(\frac{x}{2})}=0
$$
Answer: Just elementary limits:
$$
\lim_{x\to0}\frac{\sin^2(x/2)}{\sin x}=
\lim_{x\to0}\frac{1}{4}\left(\frac{\sin(x/2)}{(x/2)}\right)^{\!2}
\cdot\frac{x}{\sin x}\cdot x
$$
Comment: That's the best way to do it, although I would have left out the limit signs and called it a hint, or carried it on out to $(1/4)\cdot 1^2\cdot 1 \cdot 0 =0$ at the end.
Comment: @zhw. There were already answers showing the final result. I also like Angelo Mark's solution.
Answer: because the limit is $\frac{0}{0}$,we can use Lopital Rule
 $$\lim_{x\rightarrow 0}\frac{\sin^2 x/2}{\sin x}=\lim_{x\rightarrow 0}\frac{2(\sin x/2)(\cos x/2)(0.5)}{\cos x}=0$$
Comment: One should not use Lopital's rule before checking the sufficient conditions for it to be applied
Comment: I meant a more rigorous approach, but nevermind
Answer: This problem is a bit more straightforward if we make a simple substitution $t=\frac{1}{2}x$.
$$\lim_{x\to 0}\frac{\sin^2\frac{x}{2}}{\sin x} = \lim_{t\to 0}\frac{\sin^2 t}{\sin 2t} =\dots$$
Answer: $$\lim_{x\to 0}\left(\frac{\sin^2\left(\frac{x}{2}\right)}{\sin(x)}\right)=$$
$$\lim_{x\to 0}\left(\frac{\frac{\text{d}}{\text{d}x}\sin^2\left(\frac{x}{2}\right)}{\frac{\text{d}}{\text{d}x}\sin(x)}\right)=$$
$$\lim_{x\to 0}\left(\frac{\sin\left(\frac{x}{2}\right)\cos\left(\frac{x}{2}\right)}{\cos(x)}\right)=$$
$$\lim_{x\to 0}\left(\sin\left(\frac{x}{2}\right)\cos\left(\frac{x}{2}\right)\sec(x)\right)=$$
$$\sin\left(\frac{0}{2}\right)\cos\left(\frac{0}{2}\right)\sec(0)=0$$


Is a fielder allowed to change position during the bowler's run up?

Question: If a fielder standing in the gully position moves to cover point (as bowler starts his run up) before the batsman plays the ball what action will the umpire want to take?  Often a fielder will move forward or backward, but in this situation the fielder standing in gully moved to cover point before the batsman faced the ball.
He checked the ground fielding positions before facing and at that time the fielder was in the gully position, then suddenly he was at cover point and took a catch. What is the reply by the umpire?
Answer: This is a dead ball. Law 20.5 makes it clear when the ball is in play:

The ball ceases to be dead – that is, it comes into play – when the bowler starts his/her run-up

Law 28.6 defines the allowable movement by fielders once the ball is in play:

28.6.1 Any movement by any fielder, excluding the wicket-keeper, after the ball comes into play and before the ball reaches the striker, is unfair except for the following:
[ some exceptions which don't apply here ]
[ ... ]
28.6.3 In the event of such unfair movement, either umpire shall call and signal Dead ball.

If the movement was a deliberate attempt to distract the striker, it would be covered by Law 41.4 and result in penalty runs.


Ruby trainwrecks

Question: The following code:
class Avalon
  def h
    puts "petrezselyem"
  end
end
Avalon.new.h

prints out "petrezselyem". However, if I type:
puts Avalon.new.h.size

It gives
undefined method `size' for nil:NilClass (NoMethodError).

Could you please explain this behaviour?
Answer: The method h does not return "petrezselyem", it prints out "petrezselyem".  So Avalon.new.h == nil.
Java would give you a compile error, but a dynamic language like Ruby does not know the type returned.
Comment: @Mischa: Or `"petrezselyem".tap { |s| puts s }` ;-) And no, I'm not serious, but it works.
Comment: Every method returns something; in this case the result of `puts`, which is always `nil`.
Comment: The solution would be to change `puts "petrezselyem"` to `"petrezselyem"` or `return "petrezselyem"`. Then you *can* do `puts Avalon.new.h.size`.
Answer: Look at the signature of Kernel#puts:
puts(obj, ...) → nil

This means puts will always return nil and since it's the last statement in the h method, this will also be the return value of that method.
Comment: I see, just like a println wouldn't return what its printing. Beginners mistake... :)

Ruby trainwrecks


testing ports on a Nortel Networks switch

Question: I'm trying to test a number of Nortel Networks BayStack 470-48T switches. I can connect a machine per port, but it's a 48 port switch. I have two machines, and my plan was to plug one in and move the other 'down the line' testing each one as I went with a constant ping. Unfortunately, once a machine has been plugged into one port, the switch won't accept that machine on any other port. How can I make the switch clear its information on disconnect so I can quickly test each port?
Answer: When your target machine responds to the ping, it's MAC address gets "registered" in the MAC address table of the switch to the port that the ping reply came from. When you move the target machine the switch still believes it to be connected to that port until the MAC address table cache expires. You could do two things to try and get around this:

Establish a telnet session to the CLI of the switch and flush the MAC address table when you move the target machine.
Reverse the test. Run the ping from the target machine to the "stationary" machine.

As an FYI: Switches populate their MAC address tables by examining the source MAC address of frames, not the destination address.
Comment: I can't seem to connect via CLI. I've tried serial to serial, and serial to straightthrough RJ45, and neither would connect over hyperterminal. And reversing the test because once the "mobile" machine has connected on one port, it won't acquire an IP address when it's on another port.
Comment: give it a static ip address for the purpose of the test.
Comment: Tried that, no cigar. I suspect because it bound the mac address to a port already, it refuses to re bind.
Comment: OK, I thought that might force it to repopulate the MAC address table, I guess not though. What you could do, since the ping reply isn't coming back to the mobile machine, would be to continue running the reverse test from the mobile machine and at the same time run a packet capture on the stationary machine and look for the ICMP Echo Request from the mobile machine. The ICMP Echo Reply may not be getting back to the mobile machine but that shouldn't stop the ICMP Echo Request from getting to the stationary machine.
Comment: Any recommendation on a freeware packet capture application that runs on win-xp?
Comment: Microsoft Network Monitor or Wireshark.


Is it possible for a distribution to have a non-zero probability of generating a value with zero probability density?

Question: I am reading the book "An Elementary Introduction to Statistical Learning Theory" and there is a sketch of a proof (Section 8.4) for the universal consistency of kernel rules for binary classification.
In this sketch the authors write: "with very high probability, the feature vector x will fall where the probability density p(x) is positive"
Since probability density is non-negative, the authors suggest that there is a non-zero probability that the probability density p(x) is zero. I would like to know if this is possible and if so how?
Comment: "Very high probability" sounds like a tongue-in-cheek way of saying "100% probability."
Comment: Please add the full citation for the book.
Comment: Remember the example of a exponential distribution with density $f(x)=e^{-x}$ for $x>0$ and $f(x)=0$ for $x\le 0$.  Then its mode/most likely value is $0$ even though that has zero density.  The probability of seeing it is $0$.   There will be other more complicated examples, and it is always the case for a continuous distribution that the probability that you would see the observations you do in fact turn out to see is also $0$.  That being said, "very high" here presumably means "almost sure" i.e. 100%
Answer: Because it is possible for a probability density function $p$ to be zero at infinitely many points, and even to be negative at countably many points, there is something that needs proving here. To those intimately familiar with the theory of integration, the result is obvious.  My aim in this post is to point out that the obvious is not, however, trivial to show; yet it can be demonstrated using the most basic properties of probabilities and densities with which everyone at any level of learning is conversant.
To justify the first claims above, which might surprise some people, let us note in passing that the density of (say) any positive random variable is zero almost everywhere on the negative numbers, showing densities can have plenty of zeros.  We could modify any such density function by setting its values to arbitrary negative numbers at all the integers.  This would not change any integral involving the density (even in the sense of Riemann), showing the modified function is also a valid density, despite having many negative values.

To proceed, all we need to know about probability is that

A probability is a countably additive non-negative set function.  This is a compact way of stating two of the probability axioms.  At the very end we will also need the third axiom, that the total probability is $1,$ to find exactly what a "very high probability" value is.

A density function $p$ for a probability $\Pr$ satisfies $$\Pr(\mathcal E) = \int_\mathcal E p(x)\,\mathrm dx$$ for all sets $\mathcal E$ for which $\Pr$ is defined (the "measurable" sets).

Use your favorite construction for this integral: Riemann, Lebesgue, Kurzweil-Henstock, or whatever, because we will need only the most basic properties of integrals for this analysis.  The integral is meant in a general sense: the integral sign $\int$ denotes integrals over spaces of arbitrary dimension.  But for simplicity I will refer to the elements of these spaces as "numbers."
For any number $q,$ let $\mathcal E_q$ be the set of numbers $x$ where the density does not exceed $q:$ that is, for which $p(x)\le q.$  The complement $\mathbb R \setminus \mathcal E_0$ therefore is the set of numbers at which $p$ is positive: the set with "very high probability" in the quotation.  We need to figure out what its probability could be.
No matter what the number $q$ might be, observe that
$$0 \le \Pr(\mathcal E_{q}) = \int_{\mathcal E_{q}} p(x)\,\mathrm dx \le q\int_{\mathcal E_{q}}\,\mathrm dx .$$
Because the integral on the right cannot be negative, when $q\lt 0$ that integral must be zero if we aren't to arrive at a contradiction.  In particular, every set $\mathcal E_{-1/n}$ for whole numbers $n=1,2,3,\ldots$ has zero probability.  Let $\mathcal E_{-\omega}$ denote their union: it consists of all numbers where $p$ is negative.
Because the sets $E_{-1/n}$ overlap, it's hard to work with their probabilities.  But by writing these sets in order and considering just the numbers newly appearing at each step, they break up into disjoint sets
$$\mathcal E_{-\omega} = \mathcal E_{-1} \cup \mathcal E_{-1/2} \cup \mathcal E_{-1/3} \cup \cdots =  \mathcal E_{-1} \cup \left( \mathcal E_{-1/2} \setminus  \mathcal E_{-1}\right) \cup \left( \mathcal E_{-1/3} \setminus  \mathcal E_{-1/2}\right) \cup \cdots.$$
Writing $\mathcal F_1 = \mathcal E_{-1}$ and $\mathcal F_n =  \mathcal E_{-1/n}\setminus \mathcal E_{-1/(n+1)}$ for $n=2, 3, \ldots$ permits us to partition the real numbers as
$$\mathbb R = \left(\mathbb R \setminus \mathcal E_0\right) \cup \left(\mathcal E_0 \setminus \mathcal E_{-\omega}\right) \cup \mathcal F_1 \cup \mathcal F_2 \cap \mathcal F_3 \cup \cdots.$$
(In words: every real number $x$ is one where either $p(x)\gt 0,$ $p(x)=0,$ $p(x) \le -1,$ $-1\lt p(x)\le -1/2, \ldots,$ $-1/n\lt p(x)\le -1/(n+1), \ldots$ and exactly one of these conditions will hold.)
To find the probabilities of each these pieces, note that

Because $\mathcal F_n \subseteq \mathcal E_{-1/n},$ $\Pr(\mathcal F_n) \le \Pr(\mathcal E_{-1/n}) = 0$ for all $n,$ as we have already seen, demonstrating that $\Pr(\mathcal F_n) = 0.$

By construction, the values of $p$ are $0$ or less but greater than any number $-1/n$ on the set $\mathcal E_0 \setminus \mathcal E_{-\omega}.$  Thus, this is precisely the set of zeros of $p.$  Because $p$ is a density,  $$\Pr\left(\mathcal E_0 \setminus \mathcal E_{-\omega}\right) = \int_{\mathcal E_0 \setminus \mathcal E_{-\omega}} p(x)\,\mathrm dx = \int_{\mathcal E_0 \setminus \mathcal E_{-\omega}} 0\,\mathrm dx = 0.$$

Applying the countable additivity axiom to this partition gives
$$\begin{aligned}
1 = \Pr(\mathbb R) &= \Pr\left(\mathbb R \setminus \mathcal E_0\right) + \Pr\left(\mathcal E_0 \setminus \mathcal E_{-\omega}\right) + \Pr( \mathcal F_1 ) + \Pr( \mathcal F_2 ) + \Pr( \mathcal F_3 ) + \cdots\\
& = \Pr\left(\mathbb R \setminus \mathcal E_0\right) + 0 + 0 + 0 + 0 + \cdots\\
& = \Pr\left(\mathbb R \setminus \mathcal E_0\right).
\end{aligned}$$
This shows the "very high probability" is necessarily $1.$
Answer: Agree with you, this quote is confusing. Probability is bounded between zero and one, probability density is non-negative, so "positive probability" taken literally means non-zero. My guess would be that by "positive probability" they mean something like "high probability" and state the tautology "for $x$ such that probability density $p(x)$ is high, the probability of observing value in close proximity to $x$ is high". Otherwise they would be suggesting that probability of observing some $x$ value does not correspond to $p(x)$, what is nonsense.
Another interpretation might be that authors are trying to define probability density in here. For continuous variable $\Pr(X=x) = 0$ for any $x$, so we use probability densities instead. Probability densities $p(x)$ are "probabilities per foot", so $\int_a^b p(x) \, dx = \Pr(a < x < b)$ tells us about probability of observing $x$ within the $(a,b)$ range. You could say something similar like the authors: "if probability density $p(x)$ is large, there's high probability, the feature vector $x$ will fall near to $x$".
Comment: We are probably half-way there. The remaining half concerns *Otherwise they would be suggesting that probability of observing some $x$ value does not correspond to $p(x)$*. As mentioned before, the probability $P(X=x)$ is zero for a continuous random variable with density $p_X(\cdot)$, so there is no correspondence between $P(X=x)$ and $p_X(x)$.
Comment: While I generally agree with your idea, when talking about probability densities rather than probability masses, *probability of observing some $x$ value can differ from $p(x)$* is correct. The probability is zero, while $p(x)$ may be anything nonnegative.
Comment: @RichardHardy agree, this was badly written, corrected.
Comment: I think the edit is insufficient to prevent confusion. The "tautology" part is also fraught with the same problem.
Comment: @RichardHardy those are good points, made some clarifications, & our discussion gave me one more hypothesis what might have been meant by the quote.


Is there a way to force Chromium browsers to execute setInterval/setTimeout in the background?

Question: This is somewhat a code/browser specific question, but I noticed that my setTimeout/setInterval code will eventually NEVER execute in the background even if the tab is not asleep. The following logs are from code that is supposed to execute every 1s and I'd be fine bumping that to ~15s which would work with Firefox, but not to every 3 minutes like Chrome/Edge seem to demand:
<code>Took 1.6927670464932056 time to execute at Tue Mar 07 2023 15:03:26 GMT-0700 (Mountain Standard Time)
main-loop.service.ts:191 Took 1.6322318190016232 time to execute at Tue Mar 07 2023 15:03:29 GMT-0700 (Mountain Standard Time)
main-loop.service.ts:191 Took 1.6322318190016232 time to execute at Tue Mar 07 2023 15:03:29 GMT-0700 (Mountain Standard Time)
main-loop.service.ts:191 Took 1.5268028376189324 time to execute at Tue Mar 07 2023 15:03:31 GMT-0700 (Mountain Standard Time)
main-loop.service.ts:191 Took 1.5268028376189324 time to execute at Tue Mar 07 2023 15:03:31 GMT-0700 (Mountain Standard Time)
main-loop.service.ts:191 Took 1.4788079994765282 time to execute at Tue Mar 07 2023 15:03:34 GMT-0700 (Mountain Standard Time)
main-loop.service.ts:191 Took 1.4788079994765282 time to execute at Tue Mar 07 2023 15:03:34 GMT-0700 (Mountain Standard Time)
main-loop.service.ts:191 Took 1.4304450467780012 time to execute at Tue Mar 07 2023 15:03:36 GMT-0700 (Mountain Standard Time)
main-loop.service.ts:191 Took 1.4304450467780012 time to execute at Tue Mar 07 2023 15:03:36 GMT-0700 (Mountain Standard Time)
main-loop.service.ts:191 Took 1.4040571259142123 time to execute at Tue Mar 07 2023 15:03:46 GMT-0700 (Mountain Standard Time)
main-loop.service.ts:191 Took 1.4040571259142123 time to execute at Tue Mar 07 2023 15:03:46 GMT-0700 (Mountain Standard Time)
main-loop.service.ts:191 Took 1.428811537802659 time to execute at Tue Mar 07 2023 15:07:16 GMT-0700 (Mountain Standard Time)
main-loop.service.ts:191 Took 1.428811537802659 time to execute at Tue Mar 07 2023 15:07:16 GMT-0700 (Mountain Standard Time)
main-loop.service.ts:191 Took 1.4115845539280958 time to execute at Tue Mar 07 2023 15:10:16 GMT-0700 (Mountain Standard Time)
main-loop.service.ts:191 Took 1.4115845539280958 time to execute at Tue Mar 07 2023 15:10:16 GMT-0700 (Mountain Standard Time)
</code>
It gets progressively longer until eventually it is basically stopped. This is a game intended to run in the background so any settings/code that can work around this would be great.
Edit: What ended up working was to add in some background music. The volume has to be non-zero, but users can just mute the tab and background tasks will still work.
Comment: Please use [`chrome.offscreen`](https://developer.chrome.com/docs/extensions/reference/offscreen/).
Comment: @NorioYamamoto this is for a web app, not a Chrome extension.
Comment: May I know if you have got any chance to check my answer below? I am glad to help if you have any other questions.
Comment: @YuZhou it's been a while, but it seemed like the worker solution still got throttled. The only thing that worked was adding background music (even if the user muted the tab). It seems like browsers also don't like sites using workers to run CPU intensive workloads in the background.
Answer: The behavior is by design in chromium which is called tab throttling feature. The workaround is using Web Workers which runs in the background and not affected by throttling. For more information, you can also refer to this thread.


hadoop namenode not starting/formatting on Ubuntu

Question: am trying to set up a hadoop instance on Ubuntu. The namenode is not starting up. When i do jps command I can see all but namenode . Here is my hdfs-site.xml file.
<configuration>
    <property>
        <name>dfs.datanode.data.dir</name>
        <value>/home/ac/hadoop/dfs</value>
    </property>
    <property>
        <name>dfs.namenode.name.dir</name>
        <value>/home/ac/hadoop/dfs</value>
    </property>
    <property>
        <name>dfs.replication</name>
        <value>1</value>
    </property>
</configuration>

and heres my core-site.xml
<configuration>
       <property>
        <name>fs.default.name</name>
        <value>hdfs://localhost:9000</value>
    </property>
</configuration>

The error that i got is
ERROR org.apache.hadoop.hdfs.server.namenode.FSNamesystem: FSNamesystem initialization failed.
java.io.IOException: NameNode is not formatted.

When I formatted namenode I got this on prompt
STARTUP_MSG: Starting NameNode
STARTUP_MSG:   host = hanu/127.0.1.1
STARTUP_MSG:   args = [–format]
STARTUP_MSG:   version = 1.2.1
STARTUP_MSG:   build = https://svn.apache.org/repos/asf/hadoop/common/branches/branch-1.2 -r 1503152; compiled by 'mattf' on Mon Jul 22 15:23:09 PDT 2013
STARTUP_MSG:   java = 1.8.0_31
************************************************************/
Usage: java NameNode [-format [-force ] [-nonInteractive]] | [-upgrade] | [-rollback] | [-finalize] | [-importCheckpoint] | [-recover [ -force ] ]
15/02/03 15:03:41 INFO namenode.NameNode: SHUTDOWN_MSG:
/************************************************************
SHUTDOWN_MSG: Shutting down NameNode at hanu/127.0.1.1

I've tried to  to change files as per various suggestions out there but nothing is working. I think namenode is not formatting properly.
Whats wrong in my setup and how can I get it corrected.Any help is appreciated. Thanks
Comment: Can you put in the commands that you have used to format the NameNode and start it.
Comment: @Ashrith thank you for your reply. To start `./start-all.sh` and to format `bin/hadoop namenode –format`
Answer: The reason you are seeing the error message is because of command typo, that is why namenode class is showing the Usage error, may be you have issued the command option improperly.
Make sure you type the command properly:
bin/hadoop namenode -format

and then try to start the NameNode, you could start NameNode service on foreground just to see if everything is working out properly and if you don't see any errors you could kill the process and start all the services using start-all.sh script.
Here's how you could start NameNode process on foreground:
bin/hadoop namenode

once started these are the log messages to look for to validate a proper startup:
15/02/04 10:42:44 INFO http.HttpServer: Jetty bound to port 50070
15/02/04 10:42:44 INFO mortbay.log: jetty-6.1.26
15/02/04 10:42:45 INFO mortbay.log: Started email@example.com:50070
15/02/04 10:42:45 INFO namenode.NameNode: Web-server up at: 0.0.0.0:50070
15/02/04 10:42:45 INFO ipc.Server: IPC Server Responder: starting
15/02/04 10:42:45 INFO ipc.Server: IPC Server listener on 8020: starting
15/02/04 10:42:45 INFO ipc.Server: IPC Server handler 0 on 8020: starting
15/02/04 10:42:45 INFO ipc.Server: IPC Server handler 1 on 8020: starting
15/02/04 10:42:45 INFO ipc.Server: IPC Server handler 2 on 8020: starting
15/02/04 10:42:45 INFO ipc.Server: IPC Server handler 3 on 8020: starting
15/02/04 10:42:45 INFO ipc.Server: IPC Server handler 4 on 8020: starting
15/02/04 10:42:45 INFO ipc.Server: IPC Server handler 5 on 8020: starting
15/02/04 10:42:45 INFO ipc.Server: IPC Server handler 6 on 8020: starting
15/02/04 10:42:45 INFO ipc.Server: IPC Server handler 7 on 8020: starting
15/02/04 10:42:45 INFO ipc.Server: IPC Server handler 8 on 8020: starting
15/02/04 10:42:45 INFO ipc.Server: IPC Server handler 9 on 8020: starting

you could kill the service by sending <Ctrl+C> to the process.
Comment: Thanks a lot. When I typed the command by hand, it worked . I was copy-pasting it before from somewhere. Strange though, it looked exactly same on terminal. Thank you!


How to remove horizontal bar in Hindi word Matlab

Question: I wish to remove the horizontal bar (Shirorekha) from the word to get characters from the following image, for character recognition.
Any ideas as to how can I do that. I tried to use Hough Transforms for line and then looked for longest line. Blue line is the longest, but this too is a vertical line.  I do not get results better than this. Since the line is crooked and not straight I do not get good results.
Please help.
Comment: Why not look for the longest *near-horizontal* line in a Hough transform?
Answer: In order to take out the Hindi language characters, you don't need to remove the horizontal line. The horizontal line is a part of the character, also there are some characters in the hindi language that use small or no horizontal line. So, this means that to separate characters you will need to slice them vertically.
You can search the 'Matras' system for reference of what to consider before vertical slicing.
Answer: You need to define the acceptable <code>[theta][1]</code> range. See the below example:
<code>[H,T,R] = hough(BW,'Theta',85:0.01:89.99);
</code>


What is the solution set of the system $x^x + y^y = 31$ and $x + y = 5$?

Question: It may be easily seen (heuristically) that the only solution (in $\mathbb R$) of the system
$x^x + y^y = 31$ and $ x + y = 5$
is the set containing $ (2, 3)$ and $(3, 2)$.
My question is, how may one go about demonstrating that those are indeed the only intersection points of the two equations? As I said above, I can see why this is so, intuitively, but can't seem to put it down properly.
Thank you.
Comment: Using the second equation, may be we could use the simetry using $x=z+\frac 52$ and $y=z-\frac 52$
Comment: All real solutions, DonAntonio.
Comment: $x^x+(5-x)^{5-x}$ looks convex and differentiating twice seems to confirm it, probably there are easier ways to check.
Comment: Do you mean **integer** solutions?
Comment: Claude Leibovici, I don't see what you mean -- could you explain better? Although I think I've found a way -- see my reply to the answer below -- but I'd like to see other reasons why these are the only solutions. Thank you.
Comment: Countingstuff, I now see what you mean. It seems your answer and Michael's below are essentially the same.
Answer: Let $f(x)=x^x+(5-x)^{5-x}.$
Thus, since $$\left(x^x+(5-x)^{5-x}\right)''=\frac{x^x}{x}+x^x(1+\ln{x})^2+(5-x)^{4-x}+(5-x)^{5-x}(1+\ln(5-x))^2>0,$$
we obtain that $f$ is a convex function, which says yhat
the graph of $f$ and the line $y=31$ have two common points maximum,
which says that our system has two solutions maximum.
But $(3,2)$ and $(2,3)$ they are solutions and we are done!
Comment: Its cleaner to note that $x^x$ is a log-convex function, since $(x \log x)'' = 1/x$, and hence convex. Also the sum of convex functions is convex.
Comment: Your idea to see the system by using the function f(x) := x^x + (5 - x)^(5 - x) - 31 is useful. With this, one may show that all values of this function in the range 2 < x < 3 are negative and that all values of f for x < 2 and x > 3 are positive. This shows that 2 and 3 are the only points at which f vanishes.
Comment: $f$ is a convex function, which says that the graph of $f$ and the line $y=31$ have two common points maximum. Draw it!
Comment: It seems obvious but how we can prove it ?
Comment: By the definition of the convex function we have:  $f(x)<31$ for all $231$ for all $331`](http://www.wolframalpha.com/input/?i=plot+x%5Ex%2By%5Ey%3E31).
Comment: I´m not sure if this comment box is the proper way for asking this, but does somebody know if there is an analytical way for finding the solutions $x=2$ and $x=3$? I mean, not by guess or numerical methods...


Updating variables in the watch command in bash

Question: I have a series of directories with names in a sequence (i.e. 001 002 003 ... 999). I am running a job that works on the contents in each directory, ascending the sequence from 001 to 999. The job will echo into an output.txt file in each directory. When the job has finished working on the content in a directory, it echoes "running DIRECTORY_NAME" to the  job_status.txt file in the parent directory and moves on to work on the next directory.
I would like to run a simple command that looks at the output.txt of the directory that the job is currently running in.
I tried
watch "i=$(tail -n 1 job_status.txt | awk '{print $2}'); tail $i/output.txt"

This works initially; however, when the job moves on to a new directory, $i in the watch command is not updated and I'm stuck looking at the output file from the directory that the job has already completed.
How can I solve this issue?
Many thanks
Jacek
Answer: You're expanding the variable even before watch executes. Use a single quote instead:
watch 'i=$(awk "END { print \$2 }" job_status.txt); tail "$i/output.txt"'

Watch calls sh -c when executing the command, so define the command as if you're calling it with sh.
Comment: This works. Thanks! But what if "running DIRECTORY_NAME" is echoed into the job_status.txt? Using ````watch 'tail -n 1 job_status.txt | awk "{print $2}"); tail $i/output.txt'```` will lead to the same problem.
Comment: @Jacek `watch 'i=$(awk "END { print \"\$2\" }"); tail "$i/output.txt"'`
Comment: This gives an error ````cannot open `$2/output.txt' for reading: No such file or directory````
Comment: it seems the ````awk 'END {print $2}'```` only works if it is using single quote. Then it raises the question of how to put this single quote inside the single quote of the watch command. I've modified the question to include the "running DIRECOTRY_NAME" problem
Comment: @Jacek Right. The quotes weren't needed there: `watch 'i=$(awk "END { print \$2 }"); tail "$i/output.txt"'`
Comment: Yes, this solved the problem, but you missed the "job_status.txt" in your reply. Can you modify your answer so I can accept it for the modified question? Many thanks!


Kafka consumer on virtual threads - is this ok?

Question: So I'm working on a project that can dynamically create and add new listeners on Kafka brokers.
Basically each instance of the application watches for a prefix on etcd, such as /instance-1/tasks and then a manager application rebalance workload through multiple instances and put new tasks into a etcd key that takes the prefix. For example the manager puts the following to etcd, key /instance-1/tasks/f348b447-012e-425d-893b-bca3086ebf67 value some-basic-config-for-that-task.
When the instance detects a new task has been added under it's prefix, it goes to a config data store to find the kafka broker configuration and then creates new "listeners" to that kafka broker on each partition the configuration tells to.
The listener basically have an inifinite loop that can be interrupted by calling a close() method that sets a flag that it should stop on the next iteration.
For example, if the configuration is bootstrapServers 22.214.171.124:9092 ; numberOfPartitions 3 then 3 listeners (i.e. 3 threads) will be created (because there are 3 partitions). The listener basically polls kafka at each X seconds.
Since the release of virtual threads on the last LTS version (Java 21), the team is working on migrating from actual platform threads (new Thread()) to Virtual Threads.
The listener code looks like this (a very reduced version):
protected void doStart() {
    long hostConnectivityTestRate = 5000L;
    while (!closeCalled) {
        long currentTime = System.currentTimeMillis();
        if (lastHostConnectivityTest < currentTime - hostConnectivityTestRate) {
            ConnectivityTestResult res = testConnectivityWithHost();
            this.lastHostConnectivityTest = currentTime;
            this.couldConnectToHost = res.wasAbleToConnect();
            this.reporter.report(res);
        }

        if (this.couldConnectToHost) {
            ConsumerRecords <byte[], byte[]> records = kafkaConsumer.poll(Duration.ofSeconds(20));
            // DO MORE WORK...
        } else {
            Thread.sleep(hostConnectivityTestRate); // prevents busy-spin loop
        }
    }

    this.kafkaConsumer.close();
}

public void close() throws IOException {
    this.closeCalled = true;
}

The doStart() method is ran inside a virtual thread (Thread.ofVirtual().name("virtual-thread-" + id).start(connector::doStart))
I was reading about Virtual Threads and ok, I understood the concept. It does have a underlying carrier thread so it can execute. Seems useful for a lot of high throughput scenarios.
The documentation says:

Virtual threads are suitable for running tasks that spend most of the
time blocked, often waiting for I/O operations to complete. However,
they aren't intended for long-running CPU-intensive operations.

The part "they aren't intended for long-running CPU-intensive operation" got me. The listener is a long-running process that will only be stopped when the close method is called (the snippet I wrote down doesn't show it, but if an exception happens, it is reported, logged and then swallowed). I already had a discussion with some team members about it and some of them seem very sure about using virtual threads for this kind of task. I'm asking this question just because I want to be sure that there is nothing wrong with it.
Useful informations:

Each instance can have up to 50 listeners (and therefore 50 threads
/ virtual threads created dynamically)

At first, 2 instances are started and then horizontally scalled based
on CPU usage and number of listeners

Is there any problem on using virtual threads like this? Thank you guys!
Answer: After reading the code from and debugging the KafkaConsumer#poll(Duration) method, I came with the conclusion that there's no harm in executing this code backed by a Virtual Thread.
The KafkaConsumer#poll(Duration), in its essence, is just a loop that controls the interaction and polling intervals between the client and the broker.
When you call, for example, kafkaConsumer.poll(Duration.ofSeconds(20)), the underlying code may send multiple poll requests to the broker in that 20 seconds time window, for example, it may do that if the fetch conditions haven't been satisfied (conditions that can be controlled by fetch.min.bytes and fetch.max.wait.ms consumer properties), meaning that if the duration is 20 seconds and your fetch.min.bytes is, say, 300 bytes, and your fetch.max.wait.ms is, say, 500 milliseconds, when you issue a kafkaConsumer.poll(Duration.ofSeconds(20)) and there are less than 300 bytes available for consuming, the fetch request is not satisfied and thus an empty response will be returned after 500ms have been passed; during this 20 seconds frame, the kafka consumer API will issue more poll requests to the broker until 20 seconds have passed since the initial call to KafkaConsumer#poll or until the fetch request is satisfied (the broker returns content, which means 300 bytes were available at the time).
That being said, it is clear that the application will be hanging on a I/O operation (waiting for data in the socket, wether there are messages returned from the broker or not).
Maybe I misunderstood something, but, to me, it looks like a reasonable approach.

EDIT
No, it's not a reasonable approach.
From the very same Oracle's doc on Virtual Threads:

As a rule of thumb, if your application never has 10,000 virtual threads or more, it is unlikely to benefit from virtual threads. Either it experiences too light a load to need better throughput, or you have not represented sufficiently many tasks to virtual threads.

We don't have 10k virtual threads and therefore we would not benefit from this feature. One use case I can see and we may introduce in our application soon is to process the messages using Virtual Threads (this process is not CPU-intensive), but we will do this only when the order does not matter, because if you delegate messages from the same topic to be processed in parallel, you lose control over the order you may want to process them.
Resources:

org.apache.kafka.clients.consumer.KafkaConsumer on Github
org.apache.kafka.clients.NetworkClient on Github
Oracle's Documentation on Virtual Threads


O que são as transações Begin, Commit e Rollback?

Question: O que são as Transações Begin, Commit e Rollback? E como usá-los?
Exemplo na prática vai ajudar bastante no entendimento.
Answer: Na verdade transação é o processo todo de consulta ou manipulação do banco de dados. É uma forma de estabelecer que algo deva ser feito atomicamente, ou seja, ou faz tudo ou faz nada, não pode fazer pela metade. É tudo feito em uma viagem da aplicação para o banco de dados. Em condições normais enquanto a transação não termina outras transações não podem ver o que esta está fazendo. Entenda o que é o ACID.
Esses 3 comandos SQL são para controlar isso.
O BEGIN TRANSACTION indica onde ela deve começar, então os comando SQL a seguir estarão dentro desta transação.
O COMMIT TRANSACTION indica o fim normal da transação, o que tiver de comando depois já não fará parte desta transação. Neste momento tudo o que foi manipulado passa fazer parte do banco de dados normalmente e operações diversas passam enxergar o que foi feito.
O ROLLBACK TRANSACTION também fecha o bloco da transação e é a indicação que a transação deve ser terminada, mas tudo que tentou ser feito deve ser descartado porque alguma coisa errada aconteceu e ela não pode terminar normalmente. Nada realizado dentro dela será perdurado no banco de dados.
Ao contrário do que muita gente acredita rollback no contexto de banco de dados não significa reverter e sim voltar ao estado original. Um processo de reversão seria de complicadíssimo à impossível. Um processo de descarte é simples e pode ser atômico.
A maioria dos comandos SQL são transacionais implicitamente, ou seja, ele por si só já é uma transação. Você só precisa usar esses comandos citados quando precisa usar múltiplos comandos e todos esses devam rodar atomicamente. Ou seja, eles funcionam como as chaves de um código, eles criam um bloco. Na verdade está mais para o using do C# já que há uma consequência garantida no final da execução.
CREATE TABLE ValueTable (id int);
BEGIN TRANSACTION;              -- aqui começa a transação
       INSERT INTO ValueTable VALUES(1);
       INSERT INTO ValueTable VALUES(2);
COMMIT;                         -- aqui termina e "grava" tudo

Coloquei no GitHub para referência futura.
Enquanto não dá o COMMIT essas inserções não constam de fato no banco de dados. Um ROLLBACK descartaria tudo feito antes.
A documentação tem mais informações e o assunto é interessante, pena não ter mais perguntas sobre isto.
Apesar de ser MySQL dá para entender mais sobre transações.
Comment: Ótimo, vou aguardar, eu vou dar uma melhoradinha na minha tb, e acabei de postar outra que vai ajudar: https://pt.stackoverflow.com/q/203857/101
Comment: Aparentemente saíram negativando tudo aqui, gostaria de saber se tem algo errado na minha resposta e se posso melhorar algo.
Comment: Eu não sabia que existiam estás transações no MySQL, você acha interessante trocar a tag `Sql-Server`por `independe-de-linguagem`?
Comment: Ou minha pergunta pode ser duplicada @Bigown?
Comment: @Marconi aí é c/ vc, mas ñ acho duplicata, eu só vi depois que tinha algo, mas tem alguma especificidades que é melhor encaminhar para a documentação da Microsoft mesmo.
Comment: Ainda pretendo postar uma resposta minha, pois vou me aprofundar um pouco mais aqui @bigown. Assim que tiver tempo e postar marco sua resposta.
Comment: A sua resposta está muito clara. Se existir alguém que tenha algum conhecimento extra por favor partilhe.
Answer: No livro Sistemas de banco de dados, de Elmasri & Navathe, os capítulos 17 a 19 (páginas 395 a 452, 4ª edição) tratam da teoria do processamento de transações. Sobre a pergunta "O que são transações", o texto informa que "Uma transação inclui uma ou mais operações de acesso ao banco de dados - englobam operações de inserção, exclusão, alteração ou recuperação" e também cita que "Uma transação é uma unidade atômica de trabalho que ou estará completa ou não foi realizada".
Outra parte de sua pergunta menciona BEGIN TRANSACTION, COMMIT e ROLLBACK: "Um meio para especificar os limites de uma transação é estabelecer explicitamente declarações de início de transação e fim de transação". No caso das instruções SQL que citou, BEGIN TRANSACTION refere-se a início da transação e COMMIT ao final da transação.
Ao utilizar o par BEGIN TRANSACTION/COMMIT definem-se os limites da transação: "... todas as operações de acesso ao banco de dados, entre as duas, serão consideradas parte da transação".
Como exemplo, o caso clássico de transferência de valor entre duas contas bancárias. A programação deve ser de tal forma que não seja possível a retirada do valor da conta e, por uma falha qualquer, o processo seja interrompido antes que o valor seja depositado na conta do destinatário.
-- código #1
declare @contaDe char(6), @contaPara char(6), @Valor money;

set @contaDe= '029820';
set @contaPara= '407302';
set @Valor= 1200.00;

BEGIN TRANSACTION;

UPDATE CONTA_CORRENTE
  set saldoConta= saldoConta - @Valor
  where numConta = @contaDe;

UPDATE CONTA_CORRENTE
  set saldoConta= saldoConta + @Valor
  where numConta = @contaPara;

COMMIT;
go

No caso de transferência bancária há vários procedimentos adicionais, mas o objetivo aqui é exemplificar a utilização das instruções BEGIN TRANSACTION e COMMIT.
Já a instrução ROLLBACK "reverte uma transação explícita ou implícita ao começo da transação ou a um ponto de salvamento dentro da transação", conforme consta na documentação da instrução. Geralmente é utilizada quando ocorre algo inesperado no processamento e é necessário então desfazer o processamento realizado na transação.
O conceito de transação está intimamente ligado a concorrência de processos e também às propriedades ACID (atomicidade, consistência, isolamento e durabilidade).
Documentação em português, para SQL Server:

BEGIN TRANSACTION
COMMIT TRANSACTION
ROLLBACK TRANSACTION
SAVE TRANSACTION
Comment: @Marconi: O livro "Sistemas de banco de dados", de Elmasri & Navathe, é adotado na PUC Minas; é provável que você o tenha utilizado no curso de SI.


D3JS stacked bar-chart over a timescale

Question: My JSON looks like this (with some extra records that I'm not including for brevity)
[{"name":"Jim","category":"TechSupport","month":"8",year:"2012","Date":"2012-08-01T04:00:00.000Z", "TechSupport":2,"Management":0,"Sales":0},
{"name":"Jim","category":"Management","month":"8",year:"2012","Date":"2012-08-01T04:00:00.000Z", "TechSupport":0,"Management":3,"Sales":0}]

My stack looks like this
var stack = d3.stack().keys(["TechSupport", "Management", "Sales"])
var series = stack(data)

The results of my stack look like [[0,2],[0,0]],[[2,2],[3,3]],[[1,1],[3,3]]
My code for displaying the barchart over the timescale looks like this
var groups = mainChart.selectAll("g")
.data(series)
.append("g")

groups.selectAll("rect")
.attr("id","bar")
.attr("fill", function(d){return colors(d.data.category)})
.attr("x", function(d){return xScale(d.data.Date);}}
.attr("y", function(d){return yScale(d[1]);})
.attr("width", 20)
.attr("height" return yScale(d[0]) - yScale(d[1])})

I can get everything to display along the timeline correctly except when I have items that have the same date, those are not stacking and I'm not sure why?
Comment: Can you make a fiddle and be more specific what you are trying to achieve? I think it will help me to understand the problem.
Comment: Yes, here you got. If you notice it isnt stacking it's just overlaying on the same date, where it should stack.

https://jsfiddle.net/jamiebrs/pzdbddah/
Comment: I believe something is wrong with your stacked data in "series".
Comment: Not disagreeing with that, but what specifically? Essentially I'm trying to show the number of times a person has worked in a department in a month, and if they have worked more than one department in the same month show that data stacked, looking at the JSON what can I add or change to it to make this work?
Comment: First you made a small error. In your forEach function `TechSupport` should be replaced with `Techsupport`. This will solve the fact that you have `Management` values in your series that shouldn´t be there. Secondly you have a JSON with 5 objects based upon the original dataset, that should be 4 based upon the unique date entries. The 2 objects with March 2013 should be combined to one. Then you have a good JSON for the stack function. I hope I´m giving you a good direction.
Comment: I fixed my fiddle in my foreach to fix that issue. As far as the 4 json objects vs 5, how would I go about doing that? I am fairly new to D3, so this is a learn as I go thing. I tried doing a nest and using the d.Date for my key then using .entries(data) but my bars are now disappeared, check out my fiddle to see what I am talking about, and feel free to fork it if you need to do so to explain better.
Answer: Preparing dataset - forEach()
In your forEach function I've changed two small errors.
Changed Techsupport to TechSupport like I mentioned earlier:
else if(d.category == 'Techsupport')

and by the cattotal of Management I've added a + to it:
d.Management = +d.cattotal;

Preparing dataset - reduce()
You have a JSON with several objects based upon the original dataset. This dataset must be reduced based upon the unique date entries. The object values should be combined. Then you have a good JSON for the stack function.
This can be achieved in several ways. I use a reduce() function like the one below:
var dataset = [];
data.reduce(function(res,obj){
   var key = obj.Date;
   if (res[key]){
       res[key].Sales +=obj.Sales;
       res[key].Techsupport +=obj.Techsupport;
       res[key].Management +=obj.Management;
   } else {
       dataset.push(o);
       res[key] = obj;
   }
   return res;
}, {});

The dataset contains the well prepared data for the stack function. However, I didn't want to replace data with dataset in your entire code, so I easily updated data:
data = dataset;

You are free to choose otherwise of course.
Responsive y-axis:
The y-axis must be automatically adapted to the stacked bars so I changed your yMax like:
var yMax = d3.max(series[series.length - 1], function(d) { return d[1]; });

Updating drawing the stacked bars and tooltips
Finally I updated your stacked bars by adjusting the fill attribute. I changed it for both your main and nav chart.
.attr("fill", function(d){ return colors(d3.select(this.parentNode).datum().key)})

For the tooltips I've updated the Key and Total values:
div.html("Date: " +d.data.Date + "
" + "Category: " + d3.select(this.parentNode).datum().key + "
" + "Total:" + (d[1]-d[0]))

In this fiddle you can find a working example.
Comment: The different persons are not really relevant, the page actually only allows for one person I just put in hand jammed data because I couldnt directly copy it from my other network, if fact I already made James Jim so as not to confuse the issue. Essentially there can only be one person for this graph and that person could work in any of 3 departments in a month and i need to stack his monthly work totals and display it over time using the stack to show the counts per department broken up as colors like we are doing but a total for the month of all departments along the y axis
Comment: Ok that all makes sense now. Lesson in this, see if you can get the data grouped before you send it down to D3. :)
Comment: So in implementing this I found a bug that I am unsure why it happens. I have updated my fiddle, take a look at the stack for Jan 2012. It has 3 data items (originally) and 3 categories, yet when displayed it doesnt appear stacked and it is displaying Sales twice?

Secondly is there a way using this .reduce approach to determine the highest "stacked" value for my y Axis?

here is the fiddle again

https://jsfiddle.net/jamiebrs/pzdbddah/
Comment: How do you want to implement the differtent persons (James, Jim,...) into the graph? Should we take the sales of Jim and James on a date together (add up)?
Comment: Still stuck on this one. Not sure why the stack isn't working?
Comment: I'm close. The problem is the dataset preparation. I hope to post the solution soon :)
Comment: Yeah that is what I am running into as well.
Comment: I posted a updated answer and a fiddle. I didn't change the tooltips, you have to follow the same procedure as the fill attribute.
Comment: Outstanding I got it working sans the tooltip which I am trying to follow what you did with the fill to get that working. Essentially you moved the fill up to the group level so that you can get the key, however I'm not entirely sure how to do that for the category? When I move the mouse over function to the group and do a console.log on d for the mouseover all I get is the numbers for the stack? How can I get the category and other data in the node?
Comment: The same procedure wasn't a good idea afterall. I've moved the fill attribute back to the nodes and linked to its parent. For the tooltips I did the same for the key. The tooltip value is also updated. I believe everything is working as it should be :)

D3JS stacked bar-chart over a timescale