Jmonkey Collision Detection

Question: I am new to Jmonkey programming and I would like to ask a question about collision interaction as my code seems to finds collisions possibly from the terrain and i do not know how to solve this out. My goal is player as a first person to be detected if he collides with an enemie's ghost control to display a message as an output. My code displays a continues collision and then it crashes...
package test;

//imports...

public class test extends SimpleApplication
implements ActionListener,PhysicsTickListener{
    private MotionPath path;
    private MotionPath path2;
    private MotionTrack motionTrack;
    private MotionTrack motionTrack2;
    private AnimChannel channel2;
    private AnimControl control2;
    private AnimControl control3;
    private AnimChannel channel3;
    private BulletAppState bulletAppState;
    private RigidBodyControl landscape;
    private CharacterControl player;
    private Vector3f walkDirection = new Vector3f();
    private boolean left = false, right = false, up = false, down = false;
    private TerrainQuad terrain;
    private Material mat_terrain;
    private GhostControl ghost;
    static test app;
    Material matMarker;
    public static void main(String[] args) {
        app = new test();
        app.start();

    }
    float displacement=60;
    int score = 0;
    int robotHealth=0;
    Geometry mark;
    Node shootables;
    Node pickUpObject1;
    BitmapText hudText;
    @Override
    public void simpleInitApp() {

        createScene();
        enemies();
        pickUptype1();
        initCrossHairs(); // a "+" in the middle of the screen to help aiming
        initKeys();       // load custom key mappings
        initMark();       // a red sphere to mark the hit

        hudText = new BitmapText(guiFont, false);
        hudText.setSize(guiFont.getCharSet().getRenderedSize());      // font size
        hudText.setColor(ColorRGBA.Red);                             // font color

        hudText.setLocalTranslation(600, 700, 0); // position
        guiNode.attachChild(hudText);

        DirectionalLight sun2 = new DirectionalLight();
        sun2.setDirection(new Vector3f(-0.1f, -0.7f, -1.0f));
        int width = settings.getWidth();           //width is the width of the gui
        int height = settings.getHeight();         //height is the height of the gui
    }

    protected Geometry makeCube(String name, float x, float y, float z) {
        Box box = new Box(new Vector3f(x, y, z), 3f, 3f, 3f);
        Geometry cube = new Geometry(name, box);

        Material mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        Texture tex_ml = assetManager.loadTexture("Interface/Logo/Monkey.jpg");
        mat1.setTexture("ColorMap", tex_ml);
        cube.setMaterial(mat1);

        return cube;
    }

    private PhysicsSpace getPhysicsSpace() {
        return bulletAppState.getPhysicsSpace();
    }

    /**
    * This is the main event loop--walking happens here.
    * We check in which direction the player is walking by interpreting
    * the camera direction forward (camDir) and to the side (camLeft).
    * The setWalkDirection() command is what lets a physics-controlled player walk.
    * We also make sure here that the camera moves with player.
    */
    @Override
    public void simpleUpdate(float tpf) {
        hudText.setText("SCORE \n" + "    " + score);// the text
        Vector3f camDir = cam.getDirection().clone().multLocal(0.6f);
        Vector3f camLeft = cam.getLeft().clone().multLocal(0.4f);
        walkDirection.set(0, 0, 0);
        if (left)  { walkDirection.addLocal(camLeft); }
        if (right) { walkDirection.addLocal(camLeft.negate()); }
        if (up)    { walkDirection.addLocal(camDir); }
        if (down)  { walkDirection.addLocal(camDir.negate()); }

        player.setWalkDirection(walkDirection);
        cam.setLocation(player.getPhysicsLocation());
        path.setCycle(true); // Make path a complete circuit
        path2.setCycle(true);
        motionTrack.setLoopMode(LoopMode.Loop);
        motionTrack2.setLoopMode(LoopMode.Loop);

    }

    public Node robot(){

        Node monster = (Node) assetManager.loadModel("Models/Oto/Oto.mesh.xml");
        monster.scale(1.5f, 1.5f, 1.5f);
        monster.rotate(0.0f, -3.0f, 0.0f);
        // Create a appropriate physical shape for it

        return monster;
    }

    public void createScene(){

        /** Set up Physics */
        bulletAppState = new BulletAppState();
        stateManager.attach(bulletAppState);
        //bulletAppState.getPhysicsSpace().enableDebug(assetManager);

        flyCam.setMoveSpeed(100);
        setUpKeys();

        terrain = new TerrainQuad("my terrain", 65, 513, heightmap.getHeightMap());

        /** 6. Add physics: */
        // We set up collision detection for the scene by creating a
        // compound collision shape and a static RigidBodyControl with mass zero.*/
        CollisionShape terrainShape =
        CollisionShapeFactory.createMeshShape((Node) terrain);
        landscape = new RigidBodyControl(terrainShape, 0);
        terrain.addControl(landscape);

        CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(1.5f, 6f, 1);
        player = new CharacterControl(capsuleShape, 0.05f);
        player.setJumpSpeed(20);
        player.setFallSpeed(30);
        player.setGravity(30);
        player.setPhysicsLocation(new Vector3f(145f, -28f, 10f));
        player.setCollisionGroup(PhysicsCollisionObject.COLLISION_GROUP_01);
        player.addCollideWithGroup(PhysicsCollisionObject.COLLISION_GROUP_01);

        setUpLight();
        rootNode.attachChild(SkyFactory.createSky( assetManager,
        "Textures/Sky/Bright/BrightSky.dds", false));
    }

    public void enemies(){

        shootables = new Node("Shootables");
        rootNode.attachChild(shootables);

        Node Robot1 = robot();
        Node Robot2 = robot();

        CapsuleCollisionShape capsule = new CapsuleCollisionShape(4f, 10f);
        RigidBodyControl robot1Cap = new RigidBodyControl(capsule, 0.01f);

        Robot1.addControl(robot1Cap);

        getPhysicsSpace().add(robot1Cap);

        bulletAppState.getPhysicsSpace().add(robot1Cap);
        bulletAppState.getPhysicsSpace().enableDebug(assetManager);

        robot1Cap.setMass(100f);
        robot1Cap.setKinematic(true);

        CapsuleCollisionShape capsule2 = new CapsuleCollisionShape(4f, 10f);
        RigidBodyControl robot2Cap = new RigidBodyControl(capsule, 0.01f);

        Robot2.addControl(robot2Cap);

        getPhysicsSpace().add(robot2Cap);

        bulletAppState.getPhysicsSpace().add(robot2Cap);
        bulletAppState.getPhysicsSpace().enableDebug(assetManager);

        robot2Cap.setMass(100f);
        robot2Cap.setKinematic(true);

        ghost = new GhostControl(
        new BoxCollisionShape(new Vector3f(8f,8f,8f)));  // a box-shaped ghost
        Robot1.addControl(ghost);

        ghost.setCollisionGroup(PhysicsCollisionObject.COLLISION_GROUP_01);
        ghost.setCollideWithGroups(PhysicsCollisionObject.COLLISION_GROUP_01);

        getPhysicsSpace().add(ghost);

        getPhysicsSpace().addTickListener(this);

        control2 = Robot1.getControl(AnimControl.class);
        channel2 = control2.createChannel();
        channel2.setAnim("Walk");

        control3 = Robot2.getControl(AnimControl.class);
        channel3 = control3.createChannel();
        channel3.setAnim("Walk");
        path = new MotionPath();

        path.addWayPoint(new Vector3f(500f,-83f,3f));
        path.addWayPoint(new Vector3f(350f,-79f, 3f));
        path.enableDebugShape(assetManager,rootNode);

        // Initialize our motionTrack object
        motionTrack = new MotionTrack(Robot1, path);

        motionTrack.setDirectionType(MotionTrack.Direction.Path);
        // Enable the motionTrack
        motionTrack.setEnabled(true);

        path2 = new MotionPath();

        path2.addWayPoint(new Vector3f(180f,-50f,-100f));
        path2.addWayPoint(new Vector3f(200f, -55f, -30f));
        path2.enableDebugShape(assetManager,rootNode);

        // Initialize our motionTrack object
        motionTrack2 = new MotionTrack(Robot2, path2);
        motionTrack2.setDirectionType(MotionTrack.Direction.Path);
        // Enable the motionTrack
        motionTrack2.setEnabled(true);

        shootables.attachChild(Robot1);
        shootables.attachChild(Robot2);

    }

    public void physicsTick(PhysicsSpace space, float f) {
        if (ghost.getOverlappingObjects().size() > 0) {
            final Vector3f bPoint = ghost.getPhysicsLocation();
            try {
                app.enqueue(new Callable<Boolean>() {
                    public Boolean call() throws Exception {
                        app.addMarker(bPoint);
                        return true;
                    }
                });
            } catch (Exception ex) {
            }
        }
    }

    public void pickUptype1(){
        pickUpObject1 = new Node("pickUpObject1");
        rootNode.attachChild(pickUpObject1);

        Node cube1 = new Node();
        cube1.attachChild(makeCube("the Deputy", 220f, -63f, -150f));
        Node cube2 = new Node();
        cube2.attachChild(makeCube("the Deputy2", 410f, -89f, -270f));

        RigidBodyControl floor_phy = new RigidBodyControl(0.0f);
        cube1.addControl(floor_phy);

        RigidBodyControl floor_phy2 = new RigidBodyControl(0.0f);
        cube2.addControl(floor_phy2);
        bulletAppState.getPhysicsSpace().add(floor_phy);
        bulletAppState.getPhysicsSpace().add(floor_phy2);
        pickUpObject1.attachChild(cube1);
        pickUpObject1.attachChild(cube2);
    }

}

Comment: That's a pretty decent amount of code.
Comment: Well, perhaps you should trim that down to the fewest neccesary lines. And i'd ask it at jmonkeyengine.org in the forum.
Answer: You include a lot of unnecessary code and judging from the style you are new to programming in general, I edited your question to make it readable so people could try and help (didn't fix the indenting though that would have taken far too much patience. The code should be stripped down to include only the physics code, member variables and setup for context. If your ghost control didn't rest on top of the ground it would fall through and leave your robot so its going to be constantly colliding with the ground or falling through the void after an initial pass through the ground.
You should be using getOverlappingObjects() to get the list of objects it's colliding with and then checking that list for the characters representation in the physics space.
Also this below is bad form. If your method is going to throw an exception you should know what it can throw and throw those exact exceptions and handle each one. Otherwise you're basically strapping on body armour and running around with your eyes closed because believe you won't get hurt.
public Boolean call() throws Exception

Also your enemies function is poorly designed. Functions should preferably be small, and should definitely only fulfill one purpose, whereas your function creates a node with shootables, creates two enemies and sets up controls.
Creating the node and attaching it to the root node should be in the constructor or an initialise function that is called near the start. And then you should have a function called something like addEnemy that adds a single enemy. For example:
/*
* adds an enemy and returns a reference to that enemy
*/
Spatial addEnemy()
{
    //Characters are spatials typically in JMonkey
    Spatial enemy=new Spatial();
    CapsuleCollisionShape collisionShape=new CapsuleCollisionShape(4.0f, 10.0f);
    CharacterControl characterControl = new CharacterControl(collisionShape, stepHeight);

    enemy.addControl(characterControl);
    getPhysicsSpaceState(characterControl);
    shootables.attachChild(enemy);
}

Now you notice this is really different right?
Well in JMonkey Characters use Spatials (a class derived from node) and also they don't use rigid body physics, things like terrain are too bumpy, instead they use character controls and these keep them facing upright so they don't topple and allow you to set walk direction and view direction. The character control also gives access to a collision shape that you can run collides with on to get when character controls collide with each other.
For controlling your Spatial you'll want to subclass AbstractControl, using that you can get access to other controls and update your Spatial every so often. In my game all character are Spatials with different skins set the difference is in the controls.
The player has:
CharacterControl
//to send keyboard input to the character controller to move it
KeyboardControl
GunControl

Whereas the AI has
CharacterControl
//does path planning to get a route and steers character control along it
RouteController
GunControl
//AI to determine where/if I want to walk, what to shoot at, where to aim
BehaviourControl

You should be splitting a lot of this functionality into a path to make it easier to maintain, learning more about how the JMonkey API works by looking at examples and also learning how Object Oriented design works and how to structure things so they are easy to read and easy to maintain. If you're interested the book clean code by Robert C Martin is a great guide on a clean maintainable coding style :)
Any questions feel free to ask
Comment: Didn't realise how old this question was... Guess JMonkey questions aren't that common if this was near top on the list =/
Comment: Re the indentation. If you use an IDE it will probably do it for you. In netbeans Tools->option->keymap look for reindent current line or selection and give it a key shortcut. This has saved my so much time, both with SO code and my own
Comment: hmm never thought of using an IDE for SO code, I just write it in the textbox if its from scratch. And I'm an eclipse (though moving to intellij person :P ) I just click ctrl+i to fix indentation ;)

Jmonkey Collision Detection


Como trabalhar com OCR para reconhecer fotografias

Question: Atualmente estou trabalhando em meu Trabalho de Conclusão de Curso e o tema envolve OCR.
Fiz algumas pesquisas e encontrei o Tesseract, também encontrei bibliotecas no Nuget pra ele (estou usando C#). O problema é que meu trabalho é sobre leitura de Placa Veicular, quando passo uma imagem de um carro (daquelas que os radares tiram), o tesseract não consegue fazer a leitura da imagem, diz que a imagem é inválida. Tentei com várias imagens diferentes, com imagens que só tinha a placa e sempre a mesma coisa (entenda coisa como erro). Porém se eu criar uma imagem no Paint com um texto e tentar ler, funciona, mas não é isso que quero.
Gostaria de saber se alguém já teve experiência com isso e sabe como resolver, ou se conhece outra forma de trabalhar com OCR e que atenda esta necessidade.
Se conhecer outra forma de fazer em outra linguagem, por favor me diga, posso desenvolver em outra linguagem que não seja C#.
Comment: Em que formato você está passando a imagem para o Tesseract?
Comment: Tentei com Tif, Jpg e Bmp.
Answer: Tem um cara chamado OpenCV que resolve seus problemas, ele consegue capturar não só de Imagem como de vídeo também, inclusive estou tendo que estudar ele que possui implementação em C, C++, Java e Python (optei por python).
http://opencv.org/new-opencv-books.html
Estou pensando seriamente em comprar este livro inclusive, pois, tenho muito interesse e necessidades no tema.
Link OpenCV
Comment: será que ele lê a placa do jeito que preciso? Sabe se tem algum demo que eu consiga fazer este teste?
Answer: A solução completa pode ser vista neste video. Para o que eu preciso é necessário utilizar EmguCV, OpenCV e tesseract.
Assim consigo fazer a leitura de placas veiculares.
Answer: Tive o mesmo problema alguns dias atras. Depois de testar algumas a que melhor funcionou para o meu caso foi o ocrad, que é em javascript. Eu precisava reconhecer o título da imagem de uma foto tirada de um documento.
O bom que ele te da a pontuação de reconhecimento. No meu caso quando ele dava uma pontuação maior que 60%, considerava como reconhecido. Mas pra mim só funcionou bem depois de editar a imagem, recortando somente a parte que ele precisava reconhecer.
No seu caso seria interessante ele avaliar somente a parte dos caracteres da placa, acho que teria resultados melhores.
Comment: Vi o demo dele, achei muito fraco para o que eu preciso. Mas obrigado por responder!
Comment: Pois é, no seu caso acho que tem que ser algo mais robusto. Boa sorte, se achar algo posta aqui pra gente conhecer.


Cobb-Douglas model - Lagrange multipliers

Question: I came across this problem and I almost solved it... but not quite, I think.
It's about this model.
Cobb-Douglas Production Function
OK, so let's say we have this function
$f(x,y) = K . x^\alpha . y^{1-\alpha}$
Here $K > 0, \alpha \in (0,1)$ are some constants.
We want to find the maximum of $f$ under the additional condition that
$g(x,y) = mx + ny = p$
Here $m,n,p > 0$ are also constants.
Also, we have that $x \ge 0, y \ge 0$
because $x$ is labor input and $y$ is capital input in the Cobb-Douglas model.
My book tells me to use the Lagrange multipliers method to find the maximum of $f$.
OK, so I applied the Lagrange multipliers method i.e. I created this system of three equations (for $x,y,\lambda$), and I solved it.
$f'_x(x,y) = \lambda g'_x (x,y)$
$f'_y(x,y) = \lambda g'_y (x,y)$
$g(x,y) = p$
So I was able to find that the function $f$ has an extremum at the point $(x_0, y_0) = (\alpha . p / m, (1-\alpha).p / n)$
But then I have some doubts.

How do I know that this extremum is a maximum and not a minimum? How do I justify that it's a maximum? Is it enough to compare $f(x_0, y_0)$ with say $f(0, p /n) = f(p/m, 0) = 0$ in order to conclude that we have a maximum at $(x_0, y_0)$ Somehow this doesn't sound very convincing to me.

It also bothers me that $f$ is defined only when $x \ge 0, y \ge 0$. So because of these additional restrictions on $x$ and $y$, is the Lagrange multipliers method applicable here at all? I guess it is because that's the method my book tells me to use but still... wanted to ask that here. These additional restrictions bother me because of what I learned yesterday in another question that I asked.

Lagrange Multipliers Question - some extremum points are missed (not detected) by the method
Answer: Method for solving a two-variable maximization problem with one equality constraint
Let $f$ and $g$ be continuously differentiable functions and let $c$ be a number. If the problem
$$\text{$\max_{x,y}f(x,y)$ subject to $g(x,y)=c$ and $(x,y)\in S$}$$
has a solution, then it may be found as follows:

Find all the values of $(x,y,\lambda)$ for which

$$\begin{align*}&\text{(a) $(x,y)$ is an interior point of $S$}\\ &\text{(b) $(x,y,\lambda)$ satisfies the first-order conditions and the constraint:}\end{align*}$$
$$\begin{align*}L_x(x,y)&=0\\ L_y(x,y)&=0 \\ g(x,y)&=c. \end{align*}$$

Find all the points $(x,y)$ in the interior of $S$ that satisfy

$$\begin{align*}g_x(x,y)&=0\\ g_y(x,y)&=0 \\ g(x,y)&=c. \end{align*}$$

If the set $S$ has any boundary points, find all the boundary points that solve the problem $\max_{x,y}f(x,y)$ subject to the two conditions $g(x,y) = c$ and $(x,y)$ is a boundary point of $S$.

The points $(x,y)$ you have found at which the value $f(x,y)$ is largest are the maximizers of the function $f$.

You have already carried out step 1. In many cases that will give your solution. However, you have to check points that do not satisfy the constraint qualification. A solution $(x^*,y^*)$ satisfies the constraint qualification if $g_x(x^*,y^*)\neq 0$ or $g_y(x^*,y^*)\neq 0$. Step 2 finds all the points in the constraint set that do not satisfy the constraint qualification, as they could potentially be solutions that are not found in step 1. Step 3 addresses your concern related to your previous question by also checking the boundary points of $S$.

In your case, steps 2 and 3 are as follows:

$g_x(x,y)=m\neq 0$ and $g_y(x,y)=n\neq 0$, so there are no points satisfying the conditions in step 2. (The constraint qualification is satisfied at all points.)

$S=\{(x,y)\ |\ x\geq 0, y\geq 0\}$, and the boundary points are where $x=0$ or $y=0$. When $x=0$ or $y=0$ we have $f(x,y)=0$. Since the point you found in step 1 gives $f(x,y)>0$, there is no need to go any further.

To address you first concern, once we have proved a maximum exist, then we know it must be found in step 1, 2, or 3. If a minimum exists, then we know it must satisfy the conditions in step 1, 2, or 3 with  $\max$ replaced by $\min$. It is step 4 that then allows you to distinguish between which candidates are minimizers or maximizers.
To prove existence of a maximum and minimum usually involves application of the extreme value theorem. In your case the constraint set is compact (it is the line $g(x,y)=p$ for $x\geq 0$ and $y\geq 0$) and the objective function is continuous, so you know both a maximum and minimum exist.
In your case, the minimum is $0$, and this occurs at boundary points of $S$ satisfying the constraint, i.e. at the points $(0,p/n)$ and $(p/m,0)$.
Comment: Thank you. This procedure seems very detailed and clear.
Comment: The set $S$ captures the constraints other than the equality constraint. The constraint set for the maximization problem is the intersection of $S$ with the set defined by the equality constraint. In your case the constraint set is the intersection of $S=\{(x,y) \ | \ x\geq 0, y\geq 0\}$ and the line defined by $g(x,y)=p$. So the set $S$ does have interior points; any points where $x>0$ and $y>0$ are interior.
Comment: Yeah, got it, that's why I deleted my comment. But you can keep yours. Thank you. I will reread all this and think it over.
Comment: Is there any book you can recommend, where this and similar optimization problems are described so well as in your answer?
Comment: These online notes by the economist Martin Osborne are very useful (it is where I got the method): https://mjo.osborne.economics.utoronto.ca/index.php/tutorial/index/1/int/i

The Sydsaeter and Hammond book Mathematics for Economic Analysis (referred to in Osborne's notes) is quite similar in level to his notes.
Comment: OK, thanks a log once again.
Comment: https://mjo.osborne.economics.utoronto.ca/index.php/tutorial/index/1/men/t  See "Procedure for solving a two-variable maximization problem with an equality constraint"


Overfit in aggregated models: boosting versus simple bagging

Question: Let's fix a bagging setup, where several models are build independently and than somehow aggregated. It is intuitive that increasing the number of weak learners ( N ) does not lead to overfit ( in the sense that the overfitting properties do not worsen adding an arbitrary number of trees ) . This is also discussed here for random forest:
https://datascience.stackexchange.com/questions/1028/do-random-forest-overfit
I was wondering if the situation is completely the opposite when we aggregate through boosting. In the AdaBoost algorithm, for example https://en.wikipedia.org/wiki/AdaBoost , the parameters of the next weak learner are chosen so that it improves the prediction of the previous step. Does it mean that, given enough weak learners, one would (over)fit perfectly the training data-set and, a fortiori, cause bad generaliazion ?
The question refers to the (theoretical) asymtptotic behavior for large N (the number of weak learners).
Comment: There's one misunderstanding (which is not central to your question): in bagging, we tend to employ highly flexible (strong) learners, and try to wash away their variance by averaging them all
Comment: Thank you for this remark. I guess you want to say that bagging is in general used to average low bias, high variance models, whereas boosting is thought to combine models with high bias and low variance. Is this correct ? As you noticed, this is not central to the question but thanks for the remark !
Comment: Yes, that would be correct for most applications. Of course, deviations must exist, but I'm unaware of them.
Answer: Yes, if you allow it to perfectly learn from the previous model.  But, for example with gradient boosting we utilize HEAVY regularization such as a learning rate and subsampling procedures.  For something like trees the depth of each tree is usually fairly shallow (at least it used to, now we are able to build larger trees due to other regularization advances) so it is still a pretty high bias model.  So we try to add bias to each weak learner so that the next iteration doesn't add too-too much value to our ensemble.  Also it is typical to stop iterating when your train/test set begins displaying signs of overfitting.
Even with this, we can use parameters which overfit badly, just like with a random forest or bagged tree.


Deserializing TimeSpan using JSON.net and .net WebApi

Question: I have an ASP.NET WebApi project that utilizes Entity Framework 4.1.  I am attempting to return an entity and leveraging JSON.net for deserialization.  All is working well however when attempting to deserialize entities with TimeSpan properties I run accross the following exception:

Could not cast or convert from System.String to System.TimeSpan.

Looking at the stack trace reveals:

JsonSerializationException: Error converting value
  "PT17H9M43.1671969S" to type 'System.TimeSpan'. Path 'TimeIn', line 1,
  position 890.

The output from response.Content.ReadAsStringAsync().Result is:

{"CreatedDate":"/Date(1329851383157-0500)/","ServicerUserId":6,"TimeIn":"PT17H9M43.1671969S","TimeOut":"PT17H28M43.1671969S"}

Before using JSON.net I have successfully deserialized using System.Runtime.Serialization.JsonDataContractJsonSerializer however would prefer to not use this approach.
Code used to get entity from WebApi
public T Get<T>(object id)
{
    using (var httpClient = NewHttpClient())
    {
        var response = httpClient.GetAsync(_endpoint + id.ToString()).Result;
        return JsonConvert.DeserializeObject<T>(response.Content.ReadAsStringAsync().Result);
    }
}

Code used to in WebApi controller to return entity:
// GET /api/services/5
public Service Get(Guid id)
{
    var entry = db.Services.Find(id);
    if (entry == null)
        throw new HttpResponseException(HttpStatusCode.NotFound);
    return entry;
}

Any ideas on how to deserialize TimeSpan's would be greatly appreciated.
Comment: Can you update your question with the value of response.Content.ReadAsStringAsync().Result?
Comment: Also, since you don't explicitly show your server HttpConfiguration, I'm going to assume that you are using the default formatters.  Yet you want to deserialize the response using Json.NET.  Ideally, you would have a Json.NET media type formatter and would both configure the server to use it and ensure that your client uses it when reading content like: response.Content.ReadAsAsync(new[] { new JsonNetFormatter() }).
Comment: Added example content from response.Content.ReadAsStringAsync().Result
Answer: The solution was in configuring the WebApi project to leverage JSON.net for serialization by default in the global.asax Application_Start event as referenced at http://blogs.msdn.com/b/henrikn/archive/2012/02/18/using-json-net-with-asp-net-web-api.aspx
 // Create Json.Net formatter serializing DateTime using the ISO 8601 format
            JsonSerializerSettings serializerSettings = new JsonSerializerSettings();
            serializerSettings.Converters.Add(new IsoDateTimeConverter());
            GlobalConfiguration.Configuration.Formatters[0] = new JsonNetFormatter(serializerSettings);

Comment: You accepted your own answer which is only partially right.  The real answer to your question lies in my comment above.  Deserialization is handled by the client, not the server.  You need to use the Json.NET formatter in both cases.  Clients are unaffected by server configuration.


NPM delivered package.json throws error

Question: I have created an NPM package and installed it to test it. NPM/Node throws an ENOENT error when installing dependencies for GruntJS in a nested node_modules folder with npm install.
I take these steps to create the error:

npm install pavilion
cd node_modules/pavilion
npm install

When installing the dependencies for Grunt, listed in the package.json...it throws the error as seen below.
PS. please continue reading below.
npm ERR! Darwin 14.5.0
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install"
npm ERR! node v5.0.0
npm ERR! npm  v3.8.6
npm ERR! path /Users/Baasdesign/Desktop/test/node_modules/pavilion/node_modules/.staging/pavilion-8a19d0be
npm ERR! code ENOENT
npm ERR! errno -2
npm ERR! syscall rename

npm ERR! enoent ENOENT: no such file or directory, rename '/Users/Baasdesign/Desktop/test/node_modules/pavilion/node_modules/.staging/pavilion-8a19d0be' -> '/Users/Baasdesign/Desktop/test/node_modules/pavilion'
npm ERR! enoent ENOENT: no such file or directory, rename '/Users/Baasdesign/Desktop/test/node_modules/pavilion/node_modules/.staging/pavilion-8a19d0be' -> '/Users/Baasdesign/Desktop/test/node_modules/pavilion'
npm ERR! enoent This is most likely not a problem with npm itself
npm ERR! enoent and is related to npm not being able to find a file.
npm ERR! enoent
npm ERR! Darwin 14.5.0
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install"
npm ERR! node v5.0.0
npm ERR! npm  v3.8.6
npm ERR! path npm-debug.log.575049388
npm ERR! code ENOENT
npm ERR! errno -2
npm ERR! syscall open

npm ERR! enoent ENOENT: no such file or directory, open 'npm-debug.log.575049388'
npm ERR! enoent ENOENT: no such file or directory, open 'npm-debug.log.575049388'
npm ERR! enoent This is most likely not a problem with npm itself
npm ERR! enoent and is related to npm not being able to find a file.
npm ERR! enoent

npm ERR! Please include the following file with any support request:
npm ERR!     /Users/Baasdesign/Desktop/test/node_modules/pavilion/npm-debug.log

Now the weird part. When I use Bower to install the package...I don't get the error. Here are my steps:

bower install pavilion
cd bower_components/pavilion
npm install

Everything installs fine. The difference? ---> the package.json.
The bower install package.json works find. NPM's doesn't.
Bower gives me a clean package.json, as created by myself before publishing to npm.
NPM install gives me an package.json that had a lot more info in there. When I use the bower package.json...everything install fine...even when I paste and replace the npm package.json in the node_modules folder.
What's the deal here? It seems that the package.json delivered with the npm install messes up the dependency installation.
Here is the bower package.json delivery:
http://pastebin.com/g8FgSDNG
And here is the npm package.json delivery:
http://pastebin.com/xTiQ15ih
I have tried npm cache clean but it didn't work.
sysinfo:

OSX Yosemite
node - 5.10.1
npm - 3.8.6
Comment: NPM is sometimes a pain as the error messages dont normally give you much.. have you tried a simple package.json and tried to install it and worked from there? I always work down from simple to complex to diagnose the issue
Comment: Yeah. the package.json that comes with the bower installation is much simpler, and that works fine! But I need it to work in NPM too :(
Answer: Node is brilliant but can be a pain sometimes!
I have had this error myself lately and for me rolling back to an earlier version of node fixed my problem.
I would recommend you try out an earlier install you could use "node version manager" to have a few node versions on your system and it provides methods to switch between versions.
I know its not a very technical answer and i hope someone has a specific answer to your question!
[https://github.com/creationix/nvm]
Here's the version manager link hope it helps
Comment: Thanks will try it. But it's not really efficient like this right?
Answer: The problem was that I didn't save the dependency in the package.json.
I guess Node gets the paths wrong if you do that.
So the correct workflow is:

npm init (create the package.json)
npm install <package> --save-dev
cd node_modules/pavilion
npm install

Then it works fine. The <package> needs to be added to the parent directory's package.json.

NPM delivered package.json throws error


How to create fast video loading feature like instagram Reel with React Native?

Question: How can I create a fast video consumption feature like Instagram Reels, I have created a simple video player with the help of react-native-video but after every video, it takes time to load that video.
How can I do something like prefetch the video before displaying that video? Also, it should take care of the user's mobile data consumption.
Is there any way to implement this feature in react native?
Comment: I am also finding library for same feature, did you find any library or solution?
Answer: You can use the mobile storage to store the next videos into the queue.
Comment: This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/32406972)


Expanding as Fourier-Legendre series

Question: I'll start with an example before my question so the approach becomes clearer:
To expand the function $$f(x) = \begin{cases}1,&0<x<1\\0,&\text{otherwise}\end{cases}$$ in a series of Legendre polynomials $P_n(x)$, first we find the coefficients $c_n$:
$$c_n=\frac{2n+1}{2}\int_{-1}^1 f(x)P_n(x)~dx = \frac{2n+1}{2}\int_0^1P_n(x)~dx$$
after applying Rodrigues' formula: $$c_n=\frac{2n+1}{2}\int_0^1\frac{1}{2^n n!}\frac{d^n}{dx^n}(x^2-1)^n~dx=\frac{2n+1}{2^{n+1}n!}\frac{d^{n-1}}{dx^{n-1}}(x^2-1)^n~\Biggr|_{0}^{1}$$
This terminates at $x=\pm1$, and performing a binomial expansion about $x=0$, we end up with:
$$c_n=-\frac{2n+1}{2^{n+1}n!}\sum_{k=0}^{\infty}(-1)^{n-k}\binom{n}{k}\frac{d^{n-1}}{dx^{n-1}}x^{2k}$$
This is nonzero only for $2k=n-1$. Which means $n=2k+1$, thus only odd terms survive, and hence: $$c_n=-\frac{4k+3}{4^{k+1}(2k+1)!}(-1)^{k+1}\binom{2k+1}{k}(2k)!$$
Noting that $c_0=\frac{1}{2}$, the final result is: $$f(x)\sim\frac{1}{2}+\sum_{k=0}^{\infty}\frac{(-1)^k(4k+3)(2k)!}{4^{k+1}k!(k+1)!}P_{2k+1}(x)$$

Now here is my question: I understand (or at least grasp) the methodology here, But don't know how to apply it to a similar function with just one (not so) minor adjustment. If we consider the function:
$$g(x) = \begin{cases}1,&0<x<a\\0,&\text{otherwise}\end{cases}\qquad(a<1)$$
Then there is no $x=1$ to terminate $(x^2-1)^n$, so the term at $x=a$ does not go away. I end up with a similar expression as in the example above but with two binomial expansions: one about $x=a$ minus the other at $x=0$. But I just get $n=2k+1$ for both series and they end up being equal since the $n-1$ derivatives of $(x-a)^{2k}$ differentiates it to $(2k)!$ so the result is just zero, which I don't believe it to be.
It was suggested to use the recursion formula: $$P'_{n+1}(x)-P'_{n-1}(x) = (2n+1)P_n(x).$$
But I can't get that to lead anywhere either, I expand both derivatives of $P$ in binomial series about $0$ and $a$, but just end up with the coefficients $c_n$ being zero. What is missing here?
Answer: Using the hint, isn't it just as simple as follows?
\begin{align}c_n&=\frac{2n+1}{2}\int_{-1}^1 g(x)P_n(x)\,dx\\&=\frac12\int_0^a\big(P_{n+1}'(x)-P_{n-1}'(x)\big)\,dx\\&=\frac12\big(P_{n+1}(a)-P_{n-1}(a)-P_{n+1}(0)+P_{n-1}(0)\big)\end{align}
Comment: Thanks for this answer. I've moved on wih so many other things and this feels like it was ten years ago :) But I'll definitely look into it when I have time, your answer is not too late

Expanding as Fourier-Legendre series


change selection animation in wpf listbox

Question: I have to change animation in WPF listbox, because I use my own.

The light blue is my selection (I made it with event select_changed and then I changed TextBlock.Background into lightblue)
The blue selection is the default, which I want to be transparent so that the selection will be more elegant.
Does anyone know how to do that?
Here is what I am currently doing:
        foreach (var item in myListBox.Items)
            (item as TextBlock).Background = Brushes.White;
        if (myListBox.SelectedItem != null)
            (myListBox.SelectedItem as TextBlock).Background= Brushes.LightBlue;
        myListBox.ScrollIntoView(myListBox.SelectedItem);

Comment: Implement your own ItemContainerStyle for a Listbox, hope this link will help you: http://stackoverflow.com/questions/18466521/keeping-coustum-style-of-listboxitem-when-selected/18467790#18467790
Comment: Yes, thanks o lot.
its the thing want i want to :)
now its beautiful listbox
Answer: Just add this Style to your ListBox:

</Style>

You can replace the colour Transparent with whichever colour you prefer. I believe that the names of these properties are self explanatory.
Comment: Sorry, I meant for you to add that `Style` to your `ListBox`.


Reids Patterns

Question: Can any one list out the patterns for redis like as TDG (table data gateway)? Actually I tried with TDG, but I am not satisfied.
Answer: Table data gateway describes an object being used as an interface or gateway to a table in a database. Redis alone would be the equivalent to the database store and so you would need an object, maybe created with ruby or php for example, to act as an interface to a key or group of keys within the redis dataset.
The closest model would be an object acting as an interface to a key holding a collection of data, such as a set or list.
Hope that helps.

Reids Patterns


Differentiating the binomial coefficient

Question: I took a lecture in combinatorics this semester and the professor did the following step in a proof:
He showed that function $f: x \mapsto \binom{x}{r}$ is convex for $x > r - 1$ (in order to use Jensen's inequality on $f$) and did this in the following way:
"By the product-rule we have $$f''(x) = \frac{2}{r!} \sum_{0 \leq i < j \leq r - 1} \prod_{l = 0}^{r - 1} ( x - l) \frac{1}{(x - i ) (x - j)} \geq 0$$ for all $ x > r - 1$." I am a bit confused on his definition: How would one extend the binomial coefficient to $x \notin \mathbf{N}$? I first thought about piecewise linear interpolation, but then I can't differentiate it. I also thought of plugging in the Gamma-function for the factorials, but I doubt that this is the definition he used here.
Can anyone explain to me what's happening here?
Thanks!
Answer: A way to write the (usual) binomial coefficient is
$$\binom{n}{r}= \frac{\prod_{i=0}^{r-1}(n-i)}{r!}.$$
In this expression $n$ does not have to be an integer for it to make sense.
This is the (or at least one) way to extend the definition.
So
$$\binom{x}{r}= \frac{\prod_{i=0}^{r-1}(x-i)}{r!}.$$
Answer: As small supplement:

A generalisation of the binomial coefficient is used in the binomial series representation
  \begin{align*}
(1+x)^\alpha=\sum_{r=0}^\infty\binom{\alpha}{r}x^r\qquad\qquad |x|<1, \,\alpha\in\mathbb{C}
\end{align*}
  where the binomial coefficient
  \begin{align*}
\binom{\alpha}{r}=\frac{1}{r!}\alpha(\alpha-1)\cdots(\alpha-r+1)
\end{align*}
  can be defined even for complex $\alpha$. This implies that we can consider the binomial coefficient as real-valued (or complex-valued) polynomial of degree $r$
  \begin{align*}
&f:\mathbb{R}\rightarrow\mathbb{R}\\
&f(x)=\binom{x}{r}\\
&\qquad=\frac{1}{r!}x(x-1)\cdots(x-r+1)
\end{align*}
  which is so accessible to analytical operations (differentiation, etc.).

And you're right, it's not necessary to involve the Gamma-function here.


How can I use the dynamic output of a Kubernetes resource as the environment variable in a pod?

Question: I have a custom resource in Kubernetes that outputs some dynamic properties in the status:
status:
  outputs:
    BasicParameter: command-k8s-test

I would like to be able to use that output value as an environment variable in a pod.  Is there any way to do this?  Via direct reference, or via an init container, or some other method?
Answer: There is no native feature for that, but you can use a workaround that will works.
For example, if your application requires the value of output field, you can add a initContainer to get this value and generate a kubernetes configMap. Then, in you container you can use the value of the config map as a environment variable.
Here you can find how to make your init container create a configMap, and then use this value as a environment variable.


timing code in matlab

Question: I have written down a function in 4 different ways and I want to time it .
Up-to now I have been doing this thing :
tic
%//function 1
toc

tic
%//function 2
toc

tic
%//function 3
toc

tic
%//function 4
toc

But now I want to compute the timing data for each function for (say 100 times) each and then compute the average time spent on each function. How can I do so?
Also I read somewhere that the time printed is the elapsed "wall clock" time – so it will be affected by whatever else my computer is doing whilst the MATLAB program was running.
So is there a better way of doing it ?? I have heard there is a MATLAB built in code-profiler with the command "profile on". Please can anyone suggest me the way in which I can use it?
I have also consulted the sites : Timing code in MATLAB and Profiler to find code bottlenecks.
Please suggest how to do this many times in a loop. Thanks in advance.
Comment: If you are worried about (short) processes interfering with your measurement results. It is in general a good idea to look at the median of timings rather than the mean.
Comment: I've undone your last edit, I think it muddles the focus of this Q&A, which is otherwise very good. Regarding the question in that edit (in case it's still useful): `imfilter` separates the kernel if possible (you can `edit` it to see the code), and possibly `conv2` does too, not sure. In any case, the execution time here seems dominated by the image load time, which you should exclude from the timing.
Answer: The best way to time MATLAB code is to use timeit, available from the MATLAB Central File Exchange.
It was implemented by Steve Eddins, one of the senior developers at MathWorks, and it takes care of a lot of subtleties in timing your code. For example, code runs very differently when it's executed within a function rather than within a script, and it needs to have a couple of "warm-up" runs in order to take proper advantage of the JIT compiler. It will also run the code many times in a loop, and take the median.
These things are difficult to get right without knowing a fair amount about how MATLAB works under the hood, and timeit takes care of these things for you - simple applications of tic and toc do not.
Using the profiler, as other answers have suggested, is problematic as it switches off many aspects of the JIT compiler, and will not run at the same speed as it does normally. Profiler does a great job of telling you which portions of your code take a relatively large proportion of time, i.e. discovering bottlenecks, but it's not intended for giving you actually realistic timings.
Note that in the most recent version (R2013b), timeit is available as part of core MATLAB, and does not need to be obtained from the File Exchange.
For example, to time your function one with the input argument x equal to 64, you would type:
myfun = @()one(64);
timeit(myfun);

What this does is to make a function handle to your function one (which makes sure that the code is executed inside a function, important as mentioned above), then passes this function handle into timeit. The output is timeit's estimate of the time taken to execute the code.
Comment: PS I've just taken a look through your edit containing the functions `one`, `two` etc. If later, when you're actually going to use these functions, you will require some of their outputs, you should really time them returning output arguments.
Comment: If they're all returning the same output arguments then it will probably not significantly affect the relative timings - it should have the same effect on each. But just in general, try to time things in the way they will actually be used, as it's not obvious which things will be the bottlenecks.
Comment: @roni, please ask that in a separate question and we'll try.
Comment: All my functions are returning the same outputs. So is it necessary to time my functions with returning outputs ? Will they then show differeces ?
Comment: +1: :)) Thanks I accepted your answer. By the way can you tell me how to calculate the gradient of a 2D image in matlab in a faster method? As I understand matlab's gradient command is quite inefficient. I actually found out it here : http://regularize.wordpress.com/2013/06/19/how-fast-can-you-calculate-the-gradient-of-an-image-in-matlab/
Comment: Please look into this : http://stackoverflow.com/questions/18958231/fast-computation-of-a-gradient-of-an-image-in-matlab
Answer: The profiler is one possibility, but it will slow down your code significantly.
Alternatively you could store the toc value within your loop or after every function call.
t(i) = toc

and then compare these values, compute the mean or whatever, like you would deal with other vectors.
Comment: It analyses much more than just a simple lapse of time, and already evalutes the determined time spans. But maybe you don't need any further calculations, because the profiler offers all the information you need. try it out ;)
Comment: The profiler will slow down your code because (in order to time your code line by line, and to provide other diagnostic information it gives you), it needs to turn off various aspects of the JIT compiler.
Comment: @roni Just consider the profiler to give you upper bounds. If a line is slow you can always spot it with the profiler, but sometimes things appear to be slow that can actually be sped up if it is ran without profiler. --- For what its worth, though there may be differences between running normally and with profiler on, the profiler has always led me to the right bottleneck.
Comment: @roni Sure. Think of it like this. In very old versions of MATLAB, MATLAB was purely an interpreted language, in other words it executed your code one line at a time, then moved onto the next one. In more recent versions, it looks through your code as it runs, and precompiles bits of it so they will run faster. This is called just-in-time, or JIT compilation. When you use the profiler, in order to produce its report it needs to give information on a line-by-line basis, so it needs to switch off some of this JIT functionality. (cont...)
Comment: @roni (...) Your code therefore runs slower. Not only does it run slower, but different things will slow down more than other others, so it's not a great way to compare two different things.
Comment: @roni `timeit`, like my answer indicates. `timeit` uses `tic` and `toc` - they're the correct things to be using - it just uses them appropriately to make sure that you're measuring the right things in the right way.
Comment: @roni done, added example. Once you've downloaded it, type `help timeit` for more information and examples.
Comment: I will try it out and post the results soon!
Comment: @SamRoberts I am a novice and have no idea regarding JIT compiler. Could you please elaborate ?
Comment: @SamRoberts Thank Sam for the simple yet effective explanation. Got it now ! So what type of timing mechanism would you prefer ? tic-toc ot timeit? And do give your comments regarding etime .
Comment: @SamRoberts Thanks again. Could you edit your answer please to show the implementation of timeit on my functions one, two, etc ? It would greatly help me. Was going through the documentation of timeit in FEX and found a term "function handle". Don't know how to call such a function.
Comment: @SamRoberts Please look into this : http://stackoverflow.com/questions/18958231/fast-computation-of-a-gradient-of-an-image-in-matlab
Answer: Using the profiler is almost as simple as tic/toc:
profile on;
for i=1:N
    your_function()
end
profile viewer;

If your 4 functions are independent and don't influence each other, you can also profile all of them in one block:
profile on;
for i=1:N
    your_function1()
    your_function2()
    your_function3()
    your_function4()
end
profile viewer;

The profiler will let you have a look at the processing times for each single line of code.
You can either benchmark wall-clock or cpu-time, the default is cpu-time. See the profile documentation for how to change that.
EDIT:
What I like about the profiler is, that it gives you a breakdown of each subfunction's processing time - hence it's a great way to spot bottlenecks in larger processes.
That's probably not so much the use case here.
Comment: One MAJOR problem with the profiler is that it turns the JIT accelerator off.
Comment: @roni You won't need `etime` for timing. At best it can give you the same results as `tic` and `toc`. In general `tic` and `toc` are used for quick and dirty checks, whilst `timeit` can provide better results. However, if you have many lines and quickly want to see which ones take long you can use the profiler.
Comment: @DanielE.Shub Yeah thanks !! Sam also explained it to me above in his post! So timeit is better or tic-toc ? And do give your commands regarding etime ?

timing code in matlab


getting data from edittext placed in listview

Question: i created a list with textview & edittext using holder... it look like
     Textview    Edittext
     Textview    Edittext
     Textview    Edittext
                       [button]

but i cant get the data from each every Edittext & whenever i add a data to any edittext data is swapped to other edittext automatically i can't control over it.My need is to transfer all the edittext data (which are given dynamically) to hash table or something while click a single button... can any one assist me.. hw to achieve it... assist with sample code...
this is my src file....
package org.me.androidapplication40;

import android.app.Activity;
import android.os.Bundle;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class customlistview extends Activity {

 private static class EfficientAdapter extends BaseAdapter {
 private LayoutInflater mInflater;

 public EfficientAdapter(Context context) {
 mInflater = LayoutInflater.from(context);

 }

 public int getCount() {
 return country.length;
 }

 public Object getItem(int position) {
 return position;
 }

 public long getItemId(int position) {
 return position;
 }

 public View getView(int position, View convertView, ViewGroup parent) {
 ViewHolder holder;
 if (convertView == null) {
 convertView = mInflater.inflate(R.layout.listview,parent,false);
 holder = new ViewHolder();
 holder.text = (TextView) convertView.findViewById(R.id.TextView01);
 holder.text2 = (TextView) convertView.findViewById(R.id.TextView02);
 holder.txt=(EditText) convertView.findViewById(R.id.txtbox);

 convertView.setTag(holder);
 } else {
 holder = (ViewHolder) convertView.getTag();
 }

 holder.text.setText(curr[position]);
 holder.text2.setText(country[position]);
holder.txt.setText("");
 return convertView;
 }

 public class ViewHolder {
 TextView text;
 TextView text2;
 EditText txt;
 }
 }

 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 ListView l1 = (ListView) findViewById(R.id.ListView01);
 l1.setAdapter(new EfficientAdapter(this));

 }

 private static final String[] country = { "item1","item2","item3","item4","item5","item6"};
 private static final String[] curr = { "1","2","3","4","5","6"};

}

listview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_height="wrap_content"
 android:gravity="left|center"
 android:layout_width="wrap_content"
 android:paddingBottom="5px"
 android:paddingTop="5px"
 android:paddingLeft="5px">
 <TextView android:id="@+id/TextView01"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:gravity="center"
 android:background="@drawable/bg"
 android:textColor="#FFFF00"
 android:text="hi"></TextView>
 <TextView android:text="@+id/TextView02"
 android:id="@+id/TextView02"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginLeft="10px"
 android:textColor="#0099CC"></TextView>
 <EditText
android:id="@+id/txtbox"
android:layout_width="63px"
android:layout_height="30px"
android:textSize="12sp"
android:layout_x="211px"
android:layout_y="13px"
>
</EditText>
</LinearLayout>

main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">
<TextView android:id="@+id/TextView01"
android:layout_height="wrap_content"
android:text="List of items"
android:textStyle="normal|bold"
android:gravity="center_vertical|center_horizontal"
android:layout_width="fill_parent"></TextView>
 <ListView android:id="@+id/ListView01"
 android:layout_height="wrap_content"
 android:layout_width="fill_parent">
 </ListView>
</LinearLayout>

Comment: btw. you should accept the questions which had a solution, in order to make people want to help you! It's kinda apreciation.
Comment: kindly assist as soon as possible
Answer: One way you can do is take whole holder of view in hasmap or any collection and get postion and retrive whenever its required.
Answer: I think your problem is similar to the one I had here:
Change ListView background - strange behaviour
I think you are missing to update your reference like:
// we must update the object's reference
holder.data = getItem(position);

In this case the items are in list and getItem(position); is returning the object I actually want to get updated in a specific row.
Out of the box the listView recycles the views, that means the view which is disapearing while scrolling gets reused for the one which becomes visible. In order to get the correct data displayed you have to update your references.
U got what I mean? Or should I explain in other way?
Hope this helps!


Do musicians count the meter all the time?

Question: When I started as a beginner on the piano I just timed the notes by feeling. That is, until my teacher played back a recording of me to show me just how messed up my timings were. Since then I've always been very diligent about figuring out the formal beat counting pattern and reciting it mentally as I play.
That went well for a while of course, but gradually the music got more complex, with irregular beats, ties, syncopation, different timings for left and right etc. A lot of the music I look at now have timings that are so complex I have a hard time reciting them, even mentally. Is there even a way to count thirty-seconds?
What do the pros do? Should I continue mentally singing the timings even if it seems to make things more difficult? Is counting the beats an absolutely necessary step in learning a new piece? Or is it just for beginners to use until they've internalized the timings?
Or do you use some kind of mixed approach where you count the slow beats and leave the faster and off-beat ones to "feel"?
Comment: Helpful trick (may or may not work for you): while learning a passage, sans metronome, I make the downbeat with my foot/toes, but bring the foot back *up* on the smallest current subdivision, i.e. sixteenth note for quads or eighth for triplets.
Comment: One very helpful technique I read somewhere is: when you learn a new piece, do it in 3 steps. Practice just the sequence of notes without regard to rythm; practice the exact timing of the notes without changing pitch; and finally play taking both into account. When you follow this "divide and conquer" method you'll find that in the end you'll have it much better.
Comment: This question was also similar in aspect to this one. check out the answer
http://music.stackexchange.com/questions/23143/counting-while-playing
Comment: Sometimes it's enough for me to _image_ ticks, especially after few rounds with metronome. You should try it, I am horrible at playing so if I can do this, anyone can :)
Answer: Counting is an absolutely necessary step when learning a new piece.It is the rhythmic framework of any piece. Without it, you may well be playing a different tune. 'All the right notes, but not in the right timing'.
You ask 'do they count all the time?' Well there's no need once a piece is well known to the player. We sound out words as kids, but eventually we learn to recognise the shapes of bigger words, and reading dots is similar. The rhythm pattern of a set of dots becomes known, not in every case, but patterns emerge and are remembered. We don't count the dots on a die - we recognise their pattern.
Playing with pros, looking at a new tune, often I'll hear 'Heads down and count like Hell !' muttered. Makes good sense. After all, how else is a tune going to be played successfully first time if the rhythm is ragged?
Yes, there are tricky rhythmic patterns that have to be played, and it depends how good at playing/reading someone is, but a basic, simple answer to your question is a resounding OF COURSE !! But - the header question is different - there's no need to do that once you're inside the piece. An internal clock takes over (or should !) Either foot tapping, head nodding, shoulder shrugging, whatever, it ought to be making the player into a human metronome. Some can stay still, and do it mentally, but most good players will have some form of keeping time bodily.
Comment: @Sumyrda - it's probably protocol in a 'classical' orchestra. In big bands and others playing 'less serious' music, it's almost expected.
Comment: Actually, my flute teacher, who was also a professional player, told me that foot tapping or shoulder shrugging or anything too obvious/hearable is considered unprofessional. You don't want your orchestra wiggling or tapping. But apparently wiggling your big toe ist ok ;)
Comment: One of the first times that Duke Ellington recorded a group playing his music, he made them re-play the song because they weren't tapping their toes - I guess you can't play jazz without it!
Comment: Ha ha - yes, interesting to see you both discussing this! Tapping or nodding are definitely more or less appropriate in different contexts. I always feel more comfortable keeping the beat somewhere, but ever since being "told-off" about 20 years ago for tapping my foot during a recording session, I try to tap my toe inside my shoe...
Answer: As you beginner, I strongly suggest you keep doing that. What I did when I first begun, was to count everything with my foot. After a while, I didn't really need to count every single thing with the foot, because I could hear/feel it in me.
So, no, I don't think pro musicians count every little thing, but they can if you ask them to.

Is counting the beats an absolutely necessary step in learning a new piece?

Do it; it won't harm you in any way -- that's for sure. It will help you understand rhythms much better and it will help you with the recording someday.

Or is it just for beginners to use until they've internalized the timings?

As you progress in your playing, you'll see that you won't need to be so thorought about this, and it will come naturally to you without much (or any) thinking.

Or do you use some kind of mixed approach where you count the slow beats and leave the faster and off-beat ones to "feel"?

That can work as well, but I (personally) never feel 100% sure for something that I cannot count and work just by feel. I have worked just with my feel, but better to be safe than sorry, I always try to count the things I play.

Is there even a way to count thirty-seconds?

Yes. Similar to the way you would count eighths or something. In the eighths you counr 1-2 on each beat, on sixteenths 1-2-3-4 on every beat and on thirty-seconds you count 1-2-3-4-5-6-7-8 on every beat.
In order to get used to that you have to be really good with sixteenths.
Answer: This is much like: when you drive a car, do you think about the steering all the time?
When you learn to drive, you will be consciously thinking about your steering.  Similarly, an experienced driver on an unfamiliar road will be consciously thinking about it.  An experienced driver on a familiar road will probably not be consciously considering their steering — but this is only because they know it well enough that some part of their subconscious is thinking about it without conscious control.
It's the same with the meter in music.  You don't need to be always consciously counting. But if you aren't, it should be because you are so familiar with the territory that a little part of your brain is counting in the background all the time.
Comment: I always thought driving and playing music correlate very well. Unfortunately in music you are rewarded for pushing the limits of ability; while in a car, you are punished quite severely ;p
Comment: Interesting analogy. It got me thinking. There are lots of moves I've internalized when driving, like flipping the indicator, changing gears, looking before turning etc. They all took a conscious effort to do when I started off, but now they come automatically to the point where you can't even remember that you done them ("Oh, I'm already _in_ fourth gear")
Comment: Interesting analogy. There are lots of moves I've internalized for driving, like changing gears, look before turning etc. They all took a conscious effort to do when I started but now they come automatically to the point where I can't even remember if I've done them ("Oh, I'm already _in_ 4th gear").

But if you have to start counting again whenever you try to play a new piece of music that would be like having to remember to change gears just because you're on a new road. An experienced driver wouldn't normally been thrown off by something like that. Is it the same for experienced musicians?
Comment: @PLL  +1 for the analogy. We're happier in 4/4 than 5/4 because  we've used it a million times more. We can relax (a bit) about it, but still need to be aware of what may be coming round the corner, though.Maybe that's why so many signal left/right even when there's no-one to tell?
Comment: Yes! Eventually you get to the point where you won't have to consciously think about the timing/rhythm/meter unless the song/piece/tune is particularly complex/challenging.
Answer: If you have trouble with counting meter, you might want to try a rhythm solfege method like Takadimi or the Kodaly Method.
I played with an Indian tabla player for a couple of years and found tabla rhthym solfege to be superior to the Western "1-e-and-a" counting for me in performance, possibly because I block on numbers (can't remember phone numbers) but am very verbal.
You might want try one of these three alternatives.
For complex meters, I break them up into groups of twos with the syllables "Taki" and groups of threes with the syllables "Takita".  For some reason, it's easier for me to solo over "Taki taki takita" than "12 12 123."
HTH.
Comment: This was a great tip for me, thanks. I'll say that it still helps to count with numbers when listening and getting a feel for a new song, but while playing this taki takita idea is really nice.
Answer: Agreed. Counting is necessary until you have internalised the time.
Improvising in 7 is fine as long as you've spent enough time getting used to this number of beats. It would be difficult in the extreme to improvise well and be counting at the same time.
Comment: With 7 time, it's usually split 2-2-3, 4-3 or 3-4, so counting, whilst paramount, isn't too difficult, and after a while, becomes second nature. Even something like a Greek 13/4 isn't mind blowing when split into 123,223,323,4234. But it sure as Hell needs counting !!
Comment: What's up with the voters today? +1 to everyone from me, even to you – though, frankly, your answer is a bit on the short side: generally on StackExchange, when a question already has multiple answers, adding a new one is discouraged unless you have something substantial to add. If you merely _agree_ with an existing answer, well, upvote! — At any rate, _welcome to Music.SE_.
Answer: Pros don't count everything off, and there would be no time for it, either.  One example you can't count is playing things like 4 notes against 3 notes (the "pass the goddamn butter" pattern).  If you wanted to count that off, you'd need to count it as 4 notes of 3 blips length each against 3 notes of 4 blips length each, for a total of 12 blips.  You can't count those.
The main problem with counting off everything is that you lose the accents of the music.  When first learning a piece in a multi-person setting, it is not unusual to count off, say, 2/2 in 4 beats.  When all parts and their relations have registered, then one can count in halves again.  Or depending on the music, even in whole measures.
When playing fast passages and runs, the exact timing of every note is usually not as important as having the accented notes right on cue.
Most of the time, anything but really complex rhythms can be read off the score right away by musicians.  Many professionals are excellent sight readers.  The main skill is to develop an eye for how the notes relate to the main stresses in the music.  Coming in half a beat early is something which one can manage pretty well without having to acually count half beats.
Comment: I disagree:  "pros," and us advanced amateurs, may not actively count out the beats, but you can be damned sure we are aware of the beats and subdivisions at some level at all times.  Percussionists (gosh :-) ) are best at this.  And BTW, the exact timing of notes in runs damn well **is** important.  Try ignoring it and your conductor (or accompanist) will run you out of the county.
Comment: As a percussionist.. Carl is right
Comment: As a percussionist [sometimes]... I just nod my head a bit in the difficult bits, to hold it all together. [my excuse is "I'm getting into it" ;-)
Comment: "accented notes right on cue" *is* the music, +1. I don't think we percussionists should be answering this question. After all it's our job to keep time. However, that doesn't mean we *ever* 'count' anything either.
Comment: @Mazura - I bet you like to be counted in at the start though !! What's wrong with Ready, steady - GO.
Answer: Practice and rehears over and over until it becomes second nature, I guess. I play guitar and when I started with music I was extremely bad in keeping rhythm, I couldn't even simply tap along with music. It was depressing. Somehow I picked it up over the years and now it is fairly acceptable. Rhythm/timing is an important part of music, wrong rhythm/timing basically changes a piece of music into something different that doesn't sound right, even if the notes/chords are OK. As you learn to play music you also learn to listen imho, very important.
I wish I could play classical music on the piano but I simply don't have the skills to 'copy-paste' a composition. I cannot play real time from notation either.
Answer: As a classically trained keyboardist composing songs for soul/funk bands, I am most often am using a 4-4 meter with even numbers of measures leading to changes in parts.  Defining the 1, has been a particularly difficult problem to solve for other band members who just feel the groove of the tune differently and are not proficient sight readers. They are feeling a different song completely and it frustrates the living hell out of me. When I want to go to the change, they are not ready or want to change early. All I want is for them to feel the 1 as it has been composed, NOT "redefine the 1" in a way that makes they way they initially felt the groove "feel right" . It shouldn't be that hard for any trained musician to redefine the groove by counting. When they do, then there is a moment of enlightenment where they say "WOW!, I was totally feeling it differently! But NOW I get it!" Working with an untrained musician who does not understand the importance of counting and refuses to define the 1 as composed, is like beating your head against a brick wall.
So, YES! Count until you unequivocally know where the 1 is! If somebody is saying "You aren't feeling the 1", YOU AREN'T. END OF STORY. So, learn to read music, charts or sheet music if you cannot, whatever helps you understand where that 1 lies. If this is a problem you face, you will not progress until it is solved. And you will drive a lot of other musicians completely crazy. Once you are feeling the 1, (and with a ton of practice) the counting part will just become second nature. But ALWAYS count learning a new piece. PLEASE. Having friends as band mates might put up with this kind of problem. Any large orchestrator, recording studio producer, or performing professional, WILL NOT.
Comment: This feels like a rant, and doesn't seem to add anything new that other answerers have already posted.
Comment: @DrMayhem - there's a certain resonance here for me. Playing with people who can't even feel the one? If they can't get that far, they're hardly going to count the smaller parts! I've played with them, and I guess you have too. Not easy, and underlines the importance of counting - as the question - so it's more than a rant. O.k it's a rantswer!!
Comment: This answer was very helpful for me. I've just started playing with a metronome and I am often missing the 1. This clears up that finding the 1 can be trained.
Comment: @quark - a metronome with a ping available on beat one is a very useful tool. Don't always need to use it, but it's there when needed.
Answer: Mixed.  If you 'just feel' the beat, you're lucky.  But it's also worth checking with a metronome occasionally - bad habits are easy to acquire!   As experience grows, our repertoire of patterns grows.  We read and play in the same way as a fluent reader reads a book - scanning phrases and sentences rather than spelling out 'c-a-t'.  But even then, sometimes it's good to go back to the detail.   Also, modern composers delight in challanging us with new, complex rhythms.  It's very common to see a part (even in professional circles) with pencilled slashes indicating where the beats come.
It can be amusing to be part of an ensemble or orchestra when, at a certain  tricky moment EVERYONE starts tapping their feet!


Does a pitcher receive an earned run if he makes an error?

Question: I know if a runner crosses a plate on an error made by a defending player, the run doesn't count as earned to the pitcher but if the runner scores because of an error made by the pitcher, shouldn't the run be counted as earned because it's the pitchers fault that the runner scored?
Answer: No he doesn't.
Pitcher errors are counted alongside other defensive errors and are treated the same for the purpose of determining ERA. If a run scores as the result of an error, it doesn't matter who committed it, that run is not counted for ERA.
This is explicitly covered by the rules, as well. MLB's Official Baseball Rules (OBR) 2015 edition Rule 9.16(e) is very specific about that:

An error by a pitcher is treated exactly the same as an error by any other fielder in computing earned runs.
Comment: Indeed.  ERA should be a measure of the pitcher's ability to pitch - not his ability to field.


Groove, loop, pattern, section

Question: Do the groove, loop, pattern, and section share a common meaning? I often see drum loops but guitar grooves and bass grooves and synth patterns or strings sections.
I guess a loop is mostly a short recording that you can loop and repeat as many times as you want whereas a groove can be a long recording and may or may not be loopable. A pattern is almost like a loop but not a wave form, instead a midi format. And section is just a short recording or midi data that isn't looped.
Answer: Most of these are very loose terms that depend on context, so I'll go through each one and explain their uses.
Groove
This can apply to the specific groove the drums are playing, and the style it is creating. It can also apply to the groove created by the whole rhythm section together. It implies a style, or "feel" rather than a specific musical feature. The word "groove" is commonly used in Jazz, Funk, and other modern and upbeat styles. It is not used as much when talking about slower-tempo music, and not at all in classical styles.
Loop
A loop is a short repeated section of music. It can be a chord progression in a solo section, or a loop used to create a drum beat. Loops in electronic music can be a sample which is played repeatedly while other things are added. A repeat is when a section is played again, whereas a loop tends to be on a smaller scale (within a section). The word "loop" is most often used in electronic or music, in reference to a sample or similar.
Pattern
This can refer to a note pattern, rhythmic pattern, or others. A drum pattern can also be referred to as the groove, or a drum loop. A note pattern may be used to create melodies in improvisation, with different rhythm each time. A chord pattern could also be called a chord progression and you can loop the progression or pattern to create a groove.
Section
A section is any part of music which is defined as different to the music before or after. It could also be a group of instruments, such as rhythm section, brass section, etc. The word section does not overlap much with the other three words.
Summary
Yes, the meanings of groove, loop, and section overlap sometimes but in essence they are different. The issue is that they are used to mean different things in different contexts, leading to confusion (as this answer has probably done).
Comment: Very nice answer. I'll add a few comments rather than another answer. To me, loop strongly implies electronic devices. Groove suggests certain styles like funk, jazz, etc. but not others. For example, you don't hear people talk about waltz 'groove.'
Comment: @MichaelCurtis you also don't have a rhythm section or drum kit in a Waltz. If you want i can edit the answer to incorporate your points?
Comment: You must be thinking about Strauss waltzes and that style, but sometimes there will be a drum kit in waltz time, like in older country music. Both those are two different style genres.
Comment: @Oh right I understand now.
Answer: All of these words have more than one meaning in a musical context, but I will attempt to focus on the meaning usually used by music software, since that seems to be what you're talking about. The best way to understand how terms like these are being used in your software is to read the manual, or at least look up the terms in the index.
You have loop correct, as far as I've seen. It's a bit of music that either can easily be made to repeated (as in Pro Tools or Logic) or is repeated by default unless you change the settings on it (as in Ableton Live or Fruity Loops). Most software can do MIDI or audio loops just as easily.
A groove in music software is a rhythm element that is extracted from a section of music. A groove is not music, it is musical metadata. The way you use a groove is for time alignment and quantization. Suppose you record a drummer and they lay down the perfect swing rhythm for the song. Then you try to record the bass player and they just can't get the timing right. So you tell your software to extract the groove from the drum part. It looks for drum transients and makes a note of when the drum hits occur. Then it can quantize the bass part to move the bass notes to the same time as the nearest drum hits. That is a way to quantize a part without quantizing it to a "boring" 16th note grid or something like that. You can do other things with grooves, and usually you can save grooves in a library and use them in other songs or for other things.
A pattern is usually an arbitrary part of a song that is built from a small number of measures that are all the same length and time signature. I agree that pattern implies MIDI data, not recorded audio. Patterns are mainly used by hardware sequencers and drum machines to make it possible to build up large songs while encouraging or enforcing reduced memory use (for a long time, memory for hardware sequencers was expensive). A pattern might be two measures of a drum beat that repeat four times for the verse of a song. Then there might be a different four bar pattern that repeats twice for the chorus. You can build up a song in the hardware sequencers by going into song programming mode and telling it to play the verse pattern four times, the chorus twice, then verse another four times, then chorus twice, then a bridge pattern, etc. This way you are encouraged to not program in every single hit of the verse, instead you repeat smaller sections and it saves memory. Some people also prefer to work this way.
I haven't seen section used in a device or software specific way. As far as I know, section just refers to "intro", "verse", "bridge", "chorus", etc.

Addendum: Quotes
Some other answers and comments seem to have muddied the waters, especially about the word groove. Yes the word groove can be used outside of music production hardware and software to mean something like the rhythmic feel of the music. But there is also a special definition used by several manufacturers to mean rhythmic metadata that can be used to adjust the rhythm of other elements. For example, the following is from the Beat Detective chapter of the Pro Tools Manual:

Beat Detective can extract groove templates, called DigiGrooves, from an audio or MIDI selection. DigiGrooves can be used to apply the groove, or feel of the captured passage to other audio selections (using Groove Conform) or MIDI data (using Groove Quantize).

The Ableton Live 9 Manual has an entire chapter on working with grooves, which starts (emphasis in the original):

The timing and "feel" of each clip in your Set can be modified through the use of grooves. Live comes with a large selection of grooves, which appear as .agr files in the browser.
The easiest way to work with library grooves is to drag and drop them from the browser directly onto clips in your Set. This immediately applies the timing characteristics of the groove file to the clip.

Pattern is also easily confused with its normal definition. Here is an example of an explanation of what a pattern is in terms of a hardware sequencer, from the Moog Music Mother-32 Manual:

The Mother-32 contains a monophonic step sequencer. The Sequencer stores a list of single notes and expression information called a pattern that can be played back in a loop. As a pattern plays back, the Sequencer advances through each step and outputs a new note every time a step is reached. Each pattern can contain up to 32 Steps, and is easily stored in one of 64 available pattern locations. Patterns are arranged in 8 Banks of 8 patterns each.

Note that loop also appears in the Moog quote, which means in this case that once the pattern completes playing, it immediately starts again from the first step in the pattern. The Mother-32 does not have a song function where the user can chain together multiple patterns to form a song, but many sequencers do. The DSI Tempest drum machine has a song function, but in their manual they use the word beat instead of pattern to mean the basic set of steps in the smallest functional sequence element.

Groove, loop, pattern, section


How to evaluate the integral $e^{-(c\ln(\frac{1}{x}))^s} dx$?

Question: Can anyone help me evaluate
$$\int_{\alpha}^1  \exp{\left\{-\left(c\ln\left(\frac{1}{x}\right)\right)^s\right\}} dx$$,
Where $0 \leq \alpha \leq 1$ and $s \in \mathbb{R}$.
I tried changing variables, integration by parts etc. and got nowhere. Any clues on how to handle this would be appreciated.

I tried integration by parts with $dv=1$ and $u = \exp{\left\{-\left(c\ln\left(\frac{1}{x}\right)\right)^s\right\}}$. This gives us:
$$x \exp{\left\{-\left(c\ln\left(\frac{1}{x}\right)\right)^s\right\}} {\Huge|}_{\alpha}^1 - s c\int_{\alpha}^1   \left(c\ln\left(\frac{1}{x}\right)\right)^{s-1}\exp{\left\{-\left(c\ln\left(\frac{1}{x}\right)\right)^s\right\}} dx .$$
Note that the second term reminds the incomplete Gamma function (with $ln(1/x)$ instead of $t$), however, i couldn't reach further than this.
Comment: Welcome! Please edit your post and include your attempts. It'll be easier to see where you went wrong.
Comment: The integrand can be heavily simplified.
Comment: @anomaly: it seems that "heavily" is a bit of an overstatement; the farthest I can re-work it is to $$x^{c^2\ln^{s-1}x}$$ which is not particularly simplified...
Comment: @mike : Thanks mike! I re-edit your edit. the minus sign should be before the parentheses.
Comment: @sefi, please check my editing.
Comment: first thing to do is set $x=1/y$, then $dx=-(dy)/y^2$
Comment: @abiessu: I made that comment before the post was edited. :)
Answer: Consider:
$$
\begin{align*}
e^{-(c\ln\frac{1}{x})^s} &= e^{-(\ln(\frac{1}{x})^c)^s} \\
&= e^{-(\ln(\frac{1}{x^c}))^s} \\
&= \frac{1}{e^{(\ln(\frac{1}{x^c}))^s}} \\
&= \frac{1}{e^{\ln(\frac{1}{x^c})} \cdot e^{\ln(\frac{1}{x^c})} \ldots \text{s times} } \\
&= \frac{1}{\frac{1}{x^c} \cdot \frac{1}{x^c} \ldots \text{s times}} \,\,\,\,\, \text{[exponent and log cancel out] } \\
&= \frac{1}{(\frac{1}{x^c})^s} \\
&= \frac{1}{(\frac{1}{x^{cs}})} \\
&= x^{cs}
\end{align*}
$$
Now, your integral gets reduced to:
$$
\begin{align*}
\int_{\alpha}^{1}{x^{cs}}dx &= \left[\int{x^{cs}}dx\right]_{\alpha}^{1} \\
&= \left[\frac{x^{cs + 1}}{cs + 1} + C\right]_{\alpha}^{1} \\
&= \left[\frac{1^{cs + 1}}{cs + 1} + C - \frac{\alpha^{cs + 1}}{cs + 1} - C \right] \\
&= \left[\frac{1^{cs + 1}}{cs + 1} - \frac{\alpha^{cs + 1}}{cs + 1}\right] \\
&= \left[\frac{1^{cs + 1} - \alpha^{cs + 1}}{cs + 1}\right] \\
\end{align*}
$$
Since, $ s \in \mathbb{R}$ but you haven't given any information about $c$, we can simplify it up to:
$$
\begin{align*}
\int_{\alpha}^{1}{x^{cs}}dx &= \frac{1^{c} - \alpha^{cs + 1}}{cs + 1} \\
\end{align*}
$$


Emails sent by Jenkins Email Extension plugin not received?

Question: pipeline
{
  agent any
  stages
  {
    stage ('Start')
    {
      steps
      {
        echo 'Hello'
      }
    }
  }
  post
  {
    success
    {
      emailext (
          to: 'firstname.lastname@example.com',
          subject: 'Test subject',
          body: 'Test body'
        )
    }
  }
}

This simple Jenkins pipeline seems to send email succesfully (output says: Sending email to: email@example.com), but the emails are never received in the inbox.
There is nothing in the Jenkins log.
I can successfully send a test mail from Manage Jenkins -> Configure System -> E-mail Notification.
Here I am using Email Extension plugin. Is there a way to send a test mail from Email Extension plugin? Is there any other way to find out why the emails are not sent/received?
Comment: In `Manage Jenkins -> Configure System -> E-mail Notification` in Advanced check `Enable Debug Mode` to have more logs in job logs
Comment: Thanks @Babasile, this saved me. I will describe the solution in an answer.
Answer: First: under Manage Jenkins -> Configure System -> E-mail Notification -> Advanced check Enable Debug Mode. This gives a lot of info after the job is run!
The problem was that TLS was not enabled for Email Extension plugin since there is only an SSL checkbox available. When using classic Manage Jenkins -> Configure System -> E-mail Notification - there is a TLS checkbox, but not here.
The solution to enable startTLS on Debian Linux: log into Jenkins and edit /etc/default/jenkins. Edit the JAVA_ARGS and add the following: "-Dmail.smtp.starttls.enable=true". Finally, this line for me looks like this:
JAVA_ARGS="-Djava.awt.headless=true -Dmail.smtp.starttls.enable=true". Restart Jenkins: sudo service jenkins restart.
After this is done, emailing works.
Answer: The E-mail Extension plugin (used here, as seen from emailext in the pipeline code attached to the question) has support for SMTP over TLS and the additional "TLS" configuration checkbox since v2.83 (see release notes).


How do I run npx commands without prefixing `npx`

Question: For example when I run gulp I don't have to do npx gulp.
I can omit the npx and just run gulp
How do I do this for my own package?
I've added mycommand to the package npm bin config, but I still always have to do npx mycommand for it to work.
Comment: It's most likely that you can just run gulp because you installed gulp-cli globally, which is a pattern you probably shouldn't mimic (it's inefficient, involves global packages which you can otherwise entirely avoid, and more complicated than the following alternative). Consider a shell alias `alias mycommand='npx mycommand'`.
Answer: It depends on your operating system.  On a UNIX-based OS (ie. Linux or Mac) you can use the alias command:
$ alias gulp="npx gulp"

For the rest of your terminal session, you can then run:
$ gulp

to run npx gulp.  However, whenever you restart your terminal program, you'll lose the alias.
To make the alias permanent, you need to add the alias command to the appropriate start-up file (eg. .bashrc, .profile, etc.) for your OS.  Simply copy/paste the exact command you used before, at the end of that file, save, and restart your terminal.  You'll have the alias permanently.
Aliases in Windows are also possible, but are a bit trickier; see Aliases in Windows command prompt.
Comment: "not the best practice" how? This is far better than installing things globally, the leading cause of "works on my machine" problems...
Comment: This is a good generic solution, but not best practice with npx
Answer: You don't have to use npx gulp for gulp because it is installed globally
So, there's gulp.cmd, gulp.ps1 and gulp starting npx gulp somewhere in PATH, so you can run them from there

this answer is low effort, feel free to edit it
Answer: if you install gulp globally:
npm i -g gulp
you will be able to just run gulp
Comment: You should never install things globally. It's the main cause of "works on my machine" problems. Everyone ends up with their own, unique, unversioned toolset instead of having it checked in to the project.
Answer: I would avoid answers suggesting global installs (npm install -g). Global installs guarantee that everyone working on a project has their own unique set of tools, and the reason companies end up with huge wiki pages on "how to get the project working locally".
You mention the "bin config", but perhaps you're misunderstanding that one. That's not for specifying commands that your app will use. If you were making a cli application, the bin section in package.json is where you would specify the commands exported by your project. For example, gulp exports the gulp command like:
  "bin": {
    "gulp": "./bin/gulp.js"
  },

Instead, you would add dependencies needed by your application to devDependencies. This ensures that everyone using the project will get the same version of all of the tools, such as gulp, tsc
Using npx is a far better solution because you can add everything the project needs in devDependencies, and npx will use that version. For the common command line tools, an alias such as @machineghost suggests is a better way to go, but to expand on it a bit:

Sometimes, the name of the command is different from the name of the package. In those cases, the alias can use:

alias tsc='npx --package=typescript tsc'

Normally, when running npx, it will prompt to install if there is not a version found in the current project. This is often a good safeguard, because  it reminds you to add it to the devDependencies of the project. However, if you really want it to "just work" like a global install, you can add the "yes" flag:
alias command=`npx -y gulp`

This will use the version specified in the current project if present, but install it and run it directly if not.


mongoose /node.js - update an object inside a dynamic array

Question: I have the following MongoDB collection:
user: "user123",
array1 = [
     {
     id: 1,
     active: true
     },
     {
     id: 2,
     active: true
     }
],
array2 = [
     {
     id: 3,
     active: true
     },
     {
     id: 4,
     active: true
     }
],
array3 = [
     {
     id: 5,
     active: true
     },
     {
     id: 6,
     active: true
     }
]

i need to update active to be false.
i am given the user, the array to update and also the id dynamically from the client.
so lets say for example i have following variables
let arrayToUpdate = 'array1'
let idToUpdate = 2
let user = 'user123'

according to mongoose docs i need to use the following method:
   const query = { user: user, "arrayToUpdate.id": idToUpdate}; //the problem is here
    const updateDocument = {
      $set: { "arrayToUpdate.$.active": "false" }
    };
    const result = await AutoUsersPositions.updateOne(query, updateDocument);

my problem is that the 2nd arguemnt of the query (arrayToUpdate.id) have to be inside "" which means i cant use it dynamically. i tried using ${} but it didnt work.
the same problem might be happening also on the $set part.
any solution?
Answer: Here is a simple way to generate dynamic key names, you just need to define the string first:
const arrayUpdateKey = `arrayToUpdate.${id}`
const query = { user: user, [arrayUpdateKey]: idToUpdate};


Hiera lookup based on pattern

Question: I am trying to wrap my head around how I can transition from something I am doing in my large site.pp file into a structure I can use in hiera.  It isn't clear to me from reading the puppet docs, exactly how the hiera data is evaluated and when it fits into the picture.  I have recently upgraded from puppet 2.7.x to 3.3.x.  This includes the hiera as part of standard package, so I wanted to finally look at using this since it should make my setup easier to read/understand.
I am using to support systems several external organizations.  This includes configuring systems unique to each organization.  At the top of my site.pp I have a structure like below.  Which I use to set facts for each organization based on a regular expression that matches against the clientcert fact which are configured and issued in a way that they will reliably identify each organization.
# match organization
case $::clientcert {

  /.*example1.org/ :
      { $snmp_ro_community='...'
        $snmp_location='Example Org 1'
        ... }
  /.*example2.org/ :
      { $snmp_ro_community='...'
        $snmp_location='Example Org 2'
        ... }
  /.*example3.org/ :
      { $snmp_ro_community='...'
        $snmp_location='Example Org 3'
        ... }
  /.*example4.org/ :
      { $snmp_ro_community='...'
        $snmp_location='Example Org 4'
        ... }
}

I browsing through examples I don't see any way to do any kind of pattern matching in my hiera.yaml file.  I suspect I must be missing something obvious though.
I do not want to rely on a custom fact for this.  I prefer to stick with the client certificate, since I am certain this will correctly identify the organization and system and has been confirmed using strong cryptography.  I don't want to give the one organizations values to another organization.
Comment: Since this is data from something the node already has (the FQDN and client cert CN), as opposed to something you're trying to set to the node, I'd say this is probably something that wouldn't really belong in Hiera - this bit of logic should coexist just fine in `site.pp` with a Hiera setup for your classes and class parameters, though?
Answer: I was looking for the same thing for a similar reason. and have found that hiera backedn in github helpful  jjulien/hiera-regex  . As an example from the project itself readme file:
 you have to configure a new backend that deals with the grouping as in:
/etc/puppet/hiera.yaml:
:backends:
  - regex
  - yaml
:yaml:
  :datadir: /var/lib/hiera
:regex:
  :datadir: /var/lib/hiera
:hierarchy:
  - "fqdn/%{::fqdn}"
  - common

/var/lib/hiera/fqdn/fqdn.regex:
---
 - '^mailin-trusted.example.org$':
     postfix::smtp_relay: 'mailout-dmz.example.org'
 - '^mailout.*':
     postfix::smtp_relay: 'smtp.mailgun.org'
 - '^mailin.*':
     postfix::smtp_relay: 'localhost'

however i see several puppet-users posts and puppetlabs video are warning against using hiera for classification, especially now with the release of PE 3.7 with the node manager.
Answer: You can setup hierarchy of YAML files in order to this. Let's start with hiera.yaml:
---
:hierarchy:
    - "host/%{fqdn}"
    - "domain/%{domain}"
    - "env/%{::environment}"
    - "ops/%{operatingsystem}"
    - "os/%{osfamily}"
    - common
:backends:
    - yaml
:yaml:
    :datadir: /etc/puppet/data

For the folder structure you can use any fact, that you can see in output of facter -y. e.g. you can have hiera config files for each CPU architecture. Then you would add line
- "arch/%{::architecture}"

and hiera would look let's say into arch/amd64.yaml
To debug hiera you can dump your current facts:
   $ facter -y > myhost.yaml

And look for some variable:
   $ hiera -y myhost.yml snmp_location --debug

Hiera will go through all rules and try to find the variable:
DEBUG: Mon Nov 11 11:00:23 +0100 2013: Hiera YAML backend starting
DEBUG: Mon Nov 11 11:00:23 +0100 2013: Looking up snmp_location in YAML backend
DEBUG: Mon Nov 11 11:00:23 +0100 2013: Looking for data source host/myhost.example.com
...
DEBUG: Mon Nov 11 11:00:23 +0100 2013: Looking for data source ops/Ubuntu
DEBUG: Mon Nov 11 11:00:23 +0100 2013: Cannot find datafile /etc/puppet/data/ops/Ubuntu.yaml, skipping

For matching $::clientcert it might be good idea to extract the top domain to a separate fact and then just have yaml files for cert/example1.org.yaml which would contain something like this:
---
snmp_location: 'Example Org 1'

It's good to know, that even if you have module that doesn't contain any hiera function call at all you can easily setup parameter values:
class snmp (
  $location = 'foo',
) {
# ...
}

some hiera config:
---
snmp::location: 'Example Org 1'

Comment: Hiera automatically receives all of Puppet's current variables. This includes facts and built-in variables, as well as local variables from the current scope. There are many types of variables: http://docs.puppetlabs.com/puppet/latest/reference/lang_variables.html
Comment: I guess I am not being clear enough in my question.  My problem is that there is no fact provided the client that I can use, and I don't to create a custom fact ant trust the what the client provides for that fact.  I need to do something based on a regular expression match against the client certificate which I know has been verify using strong cryptography.  I want to know if I can use the contents of a variable I set in my site.pp within my hiera config.

Hiera lookup based on pattern