What is the best way to minify drupal's aggregated javascript (automatically)?

Question: Have just run Google pagespeed over my newly created drupal 6 site, and it reports:
<code>Minifying the following JavaScript resources could reduce their size by 15.6KiB (12% reduction).

    Minifying http://[sites]/sites/default/files/js/js_9f19b87ffb72ca233ea7e45dda2f20f9.js could save 15.3KiB (18% reduction). See optimized version or Save as.
</code>
The above file was generated through drupal's default JavaScript aggregation method, but apparently it could be 18% smaller (which is a big deal for this site). What approach is considered the best when it comes to minifying JavaScript on a drupal site without breaking any existing code?
Comment: The drupal specific part would be that any solution would need to integrate with drupal and replace core JS aggregation files with minified versions. Changed title to hopefully make this clearer
Comment: I don't think this is really a Drupal specific question, it could relate to any minification of JavaScript and would therefore be better asked at StackOverflow.
Answer: I created a new module to tackle a lot of the aggregation issue & I hope to get a large chunk of it into D8; and yes it's a D6 module: http://drupal.org/project/advagg. Uses the jsmin+ lib for compression of JS and the CSSTidy lib for CSS compression. Main advantage of this is the css/js aggregate file doesn't change names unless it needs to; these files also have a 1 year cache lifetime & has imagecache generation so 404s to your CSS file should be a thing of the past.
Update: AdvAgg 7.x is being developed and comes with minification of JS as an optional sub module. Other D7 options:
http://drupal.org/project/speedy
http://drupal.org/project/uglifyjs
Comment: From the description on the page, I'm sensing a subtle hint that I should install this module :D Good job, I will try it out.
Comment: Yeah, I did try this module for the site, the only issue was it minified the CSS in such a way that the site broke. I was unsure of whether this is due to the CSS minification engine or some poorly coded CSS ;) In any case, I was only after JS minification, as the JS is substantially larger than the CSS. Will there be in option in this module to disable CSS minification? Nice work BTW
Comment: Just disable the CSS Compression module. It uses the CSSTidy library if you where wondering.
Comment: @mikeytown2 Thank you very much for such a great module. I have some problem with one of my websites css files sometime not load question is here [http://drupal.stackexchange.com/questions/128649/drupal-7-website-layout-styling-breaks-all-a-sudden-random-how-to-debug-ther] and website link [http://living.md/]i was wandering if I use advgg do i need to enable drupal Aggregate and compress CSS files and  Aggregate JavaScript files. under performance. I cant find any documentation for the module any link to some basic setting?
Answer: You could use the Javascript Aggregator module for that or install PageSpeed (the Apache module, not the Firebug extension) if you use Apache and can install extensions (e.g. not a shared webhost), combined with Drupal's own aggregation, with the added benefit that PageSpeed could also minify CSS.
Comment: Using nginx as the webserver, not too sure if Google pagespeed integrates with that just yet
Comment: Ah indeed, you're better off with the module than PageSpeed in that case.
Answer: You can use Minify module to do the same. Minify module minify the JavaScript using Google compiler. It also works with "Aggregate JavaScript files" so you can also get advantage of combining multiple JavaScript files which is default option in Drupal 7.
Minify module also minify HTML.


If $s \to t$ is satisfiable and $\neg s \to t$ is satisfiable then $t$ is satisfiable

Question: I am struggling with the following question:

Is the following statement true?
If $s \to t$ is satisfiable and $\neg s \to t$ is satisfiable then $t$ is satisfiable.

I thought this was true, but my answer sheet says "false" with no explanation.
Can anyone see why?
Answer: Satisfiable is the not the same as always true.

If $s\to t$ and $\lnot s\to t$ were always true, then $t$ would be always true.

But to say $s\to t$ and $\lnot s \to t$ are satisfiable only says that $s\to t$ and $\lnot s\to t$ are each sometimes true, which is not a strong enough condition to force $t$ to be sometimes true.

For example, let $s=p$, where $p$ is a boolean variable, and let $t=\bot$ (or any proposition that is always false).

If $p$ is set to false, then $s\to t$ is true, and if $p$ is set to true, then $\lnot s\to t$ is true, hence both $s\to t$ and $\lnot s\to t$ are satisfiable, but $t$ is not satisfiable.
Comment: Basically, having $\{s\to t\}$ be satisfiable and $\{\neg s\to t\}$ be satisfiable, doesn't *require* them to be satisfied by the same assignments.
Answer: Don't forget that an implication is always true if the hypothesis is wrong. In particular, $\bot\implies \bot$ is true, even though $\bot$ is never true by definition.
So, for example, both $s\implies \bot$ and $\lnot s\implies \bot$ have models, i.e. the left one where $s$ is false, and the right one where $s$ is true.
Yet, still, $\bot$ remains false in every model.
Answer: Satisfiable means that :

it is possible to find an interpretation (model) that makes the formula true.

In propositional logic, this amounts to find a suitable truth assignment $v$ such that the formula is evaluated to TRUE by $v$.
Consider a truth assignment $v$ such that $v(s)=v(t)=$ FALSE; we have that $v(s \to t)=$ TRUE, and thus $s \to t$ is satisfiable.
Consider now a truth assignment $v'$ such that $v(s)=$ TRUE and $v(t)=$ FALSE; we have that $v(\lnot s \to t)=$ TRUE, and thus also $\lnot s \to t$ is satisfiable.
But in both cases $t$ is evaluated to FALSE.


multiple indexer access to incomplete data

Question: If there are multiple indexer from redis access to data, resulting in each indexer data are not complete, I would like to ask how to solve this problem? Generally speaking, a logstash system can only be one indexer?
So if there are multiple indexer how to ensure that each indexer can get the complete data?
Comment: Your question is unclear, please rephrase. And make sure there is only one question, clearly asked.
Answer: What I understood was that you're trying to create multiple indexes using logstash where you're pulling up data from Redis. I haven't worked with redis but then, I could tell you that logstash also can handle in creating multiple indexes.
You can either do create the indexes within the same logstash configuration file, where you might have if conditions in order to maybe separate your incoming data to the appropriate indexes or else you could have different logstash configuration files as much as you need.
You might have a look at this for your uncertainty on getting all your data up to ES through Logstash.


Maximum value of $(3 \cos(x - 0.730) -3)^2$

Question: Maximum value of $(3 \cos(x - 0.730) -3)^2$
Maximum value of $(3\cos(x-0.730)$ is $3$ and minimum is $-3$
Conceptually , to find the maximum of $(3 \cos(x - 0.730) -3)^2$ is to use
$(3-3)^2$ however that is $0$.
But when I use the minimum value - $(-3-3)^2) =36$ and 36 is the maximum value . But isn't this conceptually wrong to use the minimum value to find the maximum value ?
I do not understand why must we use $-3$ obviously squaring a negative number will give u a positive number . But is there a reason why must I use $-3$ ?
Comment: Why do you say "Conceptually, to find the maximum of $(3 \cos (x−0.730)−3)^2$ is to use $(3−3)^2$"?
Answer: You can think of $(3 \cos(x - 0.730) -3)^2$ as the square of the distance between
$3 \cos(x - 0.730)$ and $3$  And that distance will be maximized when $3 \cos(x - 0.730)$ is negative.  And the square of the distance will always be minimized when the two coincide.
Answer: Remember that $y^2 = |y|^2$. So what you need to find is the maximum magnitude of $y$.
In this case, the cosine term is bounded by $[-1,1]$. You cannot change the $-3$ term, so your best option is to make the cosine term as negative as possible (a high magnitude negative number added to another negative number will give a higher magnitude negative number). The cosine term will be most negative when it is $-1$. Then the entire expression becomes $(3(-1) - 3)^2 = (-3-3)^2$. When squaring this, you know you'll end up with a positive value that's maximal.


differentiate between update and inset in SQLite

Question: In SQlite how can I say if a row exists update it else createEntry?
basically I want to have a table with single row and update it whenever single button pressed or so.
entry.open();
if(the row already exist)
entry.updateContact(1, Double.toString(db1), Double.toString(db2), Double.toString(db3));
else
entry.createEntry(Double.toString(db1), Double.toString(db2), Double.toString(db3));
entry.close();

Comment: You want to do an UpSert, check this out:

http://stackoverflow.com/questions/418898/sqlite-upsert-not-insert-or-replace
Comment: the best and simple way is to create a single now when db is created first time and then update rest of time , this way your pre checking computation will lessen
Comment: How can I limit the creation of db to single row?
Answer: Sounds like you want to INSERT OR REPLACE. There's a blog post on this topic at http://www.buzzingandroid.com/2013/01/sqlite-insert-or-replace-through-contentprovider/


NodeJS, request-promises are not waiting for each other

Question: So I have broken my issue down a simple code snippet.
I want the otherRequestes to wait for my firstRequests, but somehow this is not working. the firstRequested is never waited for
const rp = require('request-promise');

const firstRequest = () => {
  return rp('http://www.google.com')
    .then(function(htmlString) {
      console.log('in then of firstrequest');
    })
    .catch(function(err) {
      console.log('catch', err);
    });
}

laterRequest = (i) => {
  return rp('http://www.google.com')
    .then(function(htmlString) {
      console.log('in then' + i);
    })
    .catch(function(err) {
      console.log('catch', err);
    });

}

const requests = [];

for (let i = 0; i < 10; i += 1) {
  requests.push(laterRequest(i));
}

firstRequest().then(() => {
  Promise.all(requests).then(() => {
    console.log('all promises returned');
  });
});

So my desired output is "in then of firstrequest" after those there the output should be the laterRequests, I don't care for their order.
But when I run it currently my output is as follows the firstRequest ends up randomly somewhere in the output:
in then0
in then5
in then2
in then3
in then1
in then8
in then4
in then6
in then9
in then7
in then of firstrequest
all promises returned

Comment: The problem is, you start the 10 other requests in parallel with firstRequest - you need the for loop inside the firstRequest .then
Answer: When you do the for loop you're calling all the other promises before you've called the first one. Call your first promise, then in the .then() (when you know its finished) start calling the other promises.
firstRequest().then(() => {
  const requests = [];

  for (let i = 0; i < 10; i += 1) {
    requests.push(laterRequest(i));
  }

  return Promise.all(requests).then(() => {
    console.log('all promises returned');
  });
});

Answer: Your firstRequest doesn't return a promise. You are already attaching a resolution handler to it by attaching a .then to it in the function.
You have a few different options for how to structure this code, but it looks like you want to have some things logged to the console when each promise succeeds or fails so you could do something like this:
const firstRequest = () => {
    return new Promise((resolve, reject) => { // actually returns a promise now
        rp('http://www.google.com')
            .then(function(htmlString) {
                console.log('in then of firstrequest');
                resolve(htmlString); // promise resolves (i.e. calles its `.then` function)
            })
            .catch(function(err) {
                console.log('catch', err);
                reject(err); // promise rejects (calles the `.catch`)
            });
    })
}

const laterRequest = (i) => {
    return new Promise((resolve, reject) => {
        rp('http://www.google.com')
            .then(function(htmlString) {
                console.log('in then' + i);
                resolve(htmlString);
            })
            .catch(function(err) {
                console.log('catch', err);
                reject(err);
            });
    })
}

const requests = [];

for (let i = 0; i < 10; i += 1) {
    requests.push(laterRequest(i));
}

firstRequest().then(() => {
    Promise.all(requests).then(() => {
        console.log('all promises returned');
    });
});

Shorter answer
You could also skip repeating thens and catches and just return rp's promise directly:
const firstRequest = () => rp('http://www.google.com');

const laterRequest = (i) => rp('http://www.google.com');

firstRequest().then(() => {

    const requests = [];
    for (let i = 0; i < 10; i += 1) {
        requests.push(laterRequest(i));
    }

    Promise.all(requests).then(() => {
        console.log('all promises returned');
    });
});

Comment: but this still doesn't change the order of the execution, I ran  both of your examples and I get the same result as above. firstrequest is getting executed last.
Comment: @acid just edited my short example. `firstRequest` is executing last because you're actually running the other functions before running `firstRequest`. I've changed this in my edit. Try it now.


PHPUnit TDD questions

Question: I'm learning TDD and I have some questions. - The idea is to create a Simple Factory guided by tests. The thing is that my test coverage isn't 100% and that is where my questions lies.
Before I show you the code, let me explain what I want.

The factory is responsable to instantiate the classes and to test this I have to simulate the instantiation of the class, doing this I'm using a concrete class, it works, but when I see the test coverage, the concrete class appear and it isn't 100% tested. (You will understand). - I want to avoid that, I want to test the factory as abstract as possible.
And the other problem is that I want to insert the factory as dependency on the constructor, how to test this?

The coverage:

PizzaStore.php - 33,33% - The constructor with the factory dependency isn't tested.
PizzaFactory.php - 100%
Pizza.php - 100%
Pizza/Greek.php - 0.00% - Appeared after testing the factory using the concrete class.

PizzaTest.php:
<?php namespace Pattern\SimpleFactory;

use PHPUnit_Framework_TestCase;

class PizzaTest extends PHPUnit_Framework_TestCase {

  private $pizza;

  public function setUp()
  {
    $this->pizza = $this->getMockForAbstractClass('Pattern\SimpleFactory\Pizza');
  }

  /**
   * @covers Pattern\SimpleFactory\Pizza::setName
   * @covers Pattern\SimpleFactory\Pizza::getName
   */
  public function testPizzaSetsAndReturnsTheExpectedName()
  {
    $pizzaName = 'Greek Pizza';
    $this->pizza->setName($pizzaName);
    $this->assertEquals($pizzaName, $this->pizza->getName());
  }

  /**
   * @covers Pattern\SimpleFactory\Pizza::setDescription
   * @covers Pattern\SimpleFactory\Pizza::getDescription
   */
  public function testPizzaSetsAndReturnsTheExpectedDescription()
  {
    $pizzaDescription = 'A Pepperoni-style pizza with dough, tomato, and cheese';
    $this->pizza->setDescription($pizzaDescription);
    $this->assertEquals($pizzaDescription, $this->pizza->getDescription());
  }

}

PizzaStoreTest.php:
<?php namespace Pattern\SimpleFactory;

use PHPUnit_Framework_TestCase;

class PizzaStoreTest extends PHPUnit_Framework_TestCase {

  /**
   * @covers Pattern\SimpleFactory\PizzaStore::order
   */
  public function testStoreShouldReturnsTheRequestedPizzaWhenOrdered()
  {
    $factory = new PizzaFactory();
    $store = new PizzaStore($factory);
    $pizza = $store->order('greek');
    $this->assertInstanceOf('Pattern\SimpleFactory\Pizza', $pizza);
  }

}

PizzaFactoryTest.php:
<?php namespace Pattern\SimpleFactory;

use PHPUnit_Framework_TestCase;

class PizzaFactoryTest extends PHPUnit_Framework_TestCase {

  /**
   * @var PizzaFactory
   */
  private $pizzaFactory;

  public function setUp()
  {
    $this->pizzaFactory = new PizzaFactory();
  }

  /**
   * @covers Pattern\SimpleFactory\PizzaFactory::make
   */
  public function testPizzaFactoryMakesAPizza()
  {
    $pizza = $this->pizzaFactory->make('greek');
    $this->assertInstanceOf('Pattern\SimpleFactory\Pizza', $pizza);
  }

  /**
   * @covers Pattern\SimpleFactory\PizzaFactory::make
   */
  public function testPizzaFactoryReturnsNullWhenMakingANonexistentPizza()
  {
    $pizza = $this->pizzaFactory->make(null);
    $this->isNull($pizza);
  }

}

PizzaStore.php:
<?php namespace Pattern\SimpleFactory;

/**
 * @package Pattern\SimpleFactory
 */
class PizzaStore {

  /**
   * @var PizzaFactory
   */
  private $factory;

  /**
   * @param PizzaFactory $factory
   */
  function __construct(PizzaFactory $factory)
  {
    $this->factory = $factory;
  }

  /**
   * @param string $name
   * @return null|Pizza
   */
  public function order($name)
  {
    return $this->factory->make($name);
  }

}

PizzaFactory.php:
<?php namespace Pattern\SimpleFactory;

/**
 * @package Pattern\SimpleFactory
 */
class PizzaFactory {

  /**
   * @var array
   */
  private $pizzas = [
    'greek'     => 'Pattern\SimpleFactory\Pizza\Greek',
    'pepperoni' => 'Pattern\SimpleFactory\Pizza\Pepperoni',
  ];

  /**
   * @param string $name
   * @return null|Pizza
   */
  public function make($name)
  {
    if (isset($this->pizzas[$name]))
    {
      return new $this->pizzas[$name];
    }

    return null;
  }

}

Pizza.php:
<?php namespace Pattern\SimpleFactory;

/**
 * @package Pattern\SimpleFactory
 */
abstract class Pizza {

  /**
   * @var string
   */
  private $name;
  /**
   * @var string
   */
  private $description;

  /**
   * @return string
   */
  public function getName()
  {
    return $this->name;
  }

  /**
   * @param string $name
   */
  public function setName($name)
  {
    $this->name = $name;
  }

  /**
   * @return string
   */
  public function getDescription()
  {
    return $this->description;
  }

  /**
   * @param string $description
   */
  public function setDescription($description)
  {
    $this->description = $description;
  }

}

Pizza\Pepperoni.php:
<?php namespace Pattern\SimpleFactory\Pizza;

use Pattern\SimpleFactory\Pizza;

/**
 * @package Pattern\SimpleFactory\Pizza
 */
class Pepperoni extends Pizza {

  function __construct()
  {
    parent::setName('Pizza Pepperoni');
    parent::setDescription('A Pepperoni-style pizza with dough, tomato, and cheese');
  }

}

Pizza\Greek.php:
<?php namespace Pattern\SimpleFactory\Pizza;

use Pattern\SimpleFactory\Pizza;

/**
 * @package Pattern\SimpleFactory\Pizza
 */
class Greek extends Pizza {

  function __construct()
  {
    parent::setName('Pizza Greek');
    parent::setDescription('A Greek-style pizza with feta cheese, onion, olive and tomato');
  }

}

And that is it, anything wrong, please, say it. Thank you in advance.
Answer:
The factory is responsible to instantiate the classes and to test this
  I have to simulate the instantiation of the class, doing this I'm
  using a concrete class, it works, but when I see the test coverage,
  the concrete class appear and it isn't 100% tested. (You will
  understand). - I want to avoid that, I want to test the factory as
  abstract as possible.

This missing coverage at PizzaStore class is caused because you don't testing case when factory doesn't found class.
Missing test case:
/**
 * @covers Pattern\SimpleFactory\PizzaStore::order
 */
public function testStoreShouldReturnsTheRequestedPizzaWhenOrdered()
{
    $factory = new PizzaFactory();
    $store = new PizzaStore($factory);
    $pizza = $store->order('not exists pizza type');
    $this->assertNull($pizza);
}

And the other problem is that I want to insert the factory as
  dependency on the constructor, how to test this?

You could use mocked PizzaFactory class and verify is then called. I don't know how this work with PHPUnit mocks, but in ouzo-goodies mock utility this looks like:
public function testFactory()
{
    $factory = Mock::create('\Pattern\SimpleFactory\PizzaFactory');
    Mock::when($factory)->make()->thenReturn(new \Pattern\SimpleFactory\Pizza\Greek());

    $magic = new Magic($factory);

    $order = $magic->order();

    Mock::verify($factory)->make();
    $this->assertInstanceOf('\Pattern\SimpleFactory\Pizza\Greek', $order);
}

This Mock::verify method checks that method PizzaFactory::make was called.
Additionally in my opinion in your case better use interface over inheritance.
Comment: Actually the missing test isn't needed. `PizzaFactory` is the one that returns nulll and if you look the tests you will find `testPizzaFactoryReturnsNullWhenMakingANonexistentPizza` - `PizzaStore` isn't 100% because of the constructor that hasn't been tested. That is what I want to know, how to test the constructor that accepts an argument. - and the `Greek` pizza appears on the test coverage as 0% because I'm testing the factory using the concrete class. How to simulate the factory without the concrete class - What you mean with interface over inheritance in this case?

PHPUnit TDD questions


Reading fixed doubles with fixed point from file and convert into long

Question: I am reading US Dollar prices from a file. Example
asset_jsld 40.54
asset_sxd 40.80

I want to have a map that has these prices as a key. Since float or double are less than ideals keys, I am converting my values into Dollar Cents and I am storing them as a long. words is a list of string by column of the original file.
using boost::spirit::qi::parse;
// ...
if (!parse(words[1].begin(), words[4].end(), double_, price_d))
  // Error handeling
long price = boost::numeric_cast<long>(price_d * 100.0);

The problem is that the double is 40.80 and the long is 4079. Does this rounding error come from numeric_cast? Is there are numerical stable alternative?
Comment: conversion from `double` to `long` are towards 0. So to have a correct rounding in your case, you can use `long price = boost::numeric_cast(price_d * 100.0 + 0.5)` to round to almost the nearest, provided that `price_d >= 0`.
Answer: Don't do math operations on floating point numbers if you want consistency. Read the values as strings, remove the dot and parse that as long.
Is floating point math broken?


Approaches for comparing visual representation of two distributions with unequal sample sizes

Question: I have two distributions of continuous, unpaired measurements. I would like to visualize the two distributions with a pair of histograms, counting measurements that fall over a bin's interval.
Are there ways to rescale or process the smaller of the two sets, so that when I make two histograms (or other visualizations, violin, box, etc.) of their data, the visualization does not lead the viewer to favor a bin interval containing under- or over-represented measurements from one set, relative to the other.
Comment: I don't see why binning or rescaling is being considered at all. Just plot the actual values in order. That is compatible with showing means, medians, quartiles, etc. See http://stats.stackexchange.com/questions/181501/how-to-use-boxplots-to-find-the-point-where-values-are-more-likely-to-come-from
http://stats.stackexchange.com/questions/114744/how-to-present-box-plot-with-an-extreme-outlier
http://stats.stackexchange.com/questions/190223/how-to-visualize-independent-two-sample-t-test
for several examples.
Comment: This is not disagreeing with anything Nick wrote there ... but if you really need to compare histograms at different sample sizes, scale them both to area 1 (i.e. to be density estimates)
Answer: If you really need to compare histograms at different sample sizes, scale them both to area 1 (i.e. to be density estimates).

However, as Nick suggested in comments, there are other ways of comparing the distributions that don't require binning.
You could plot ecdfs, or a pair of theoretical QQ plots on the same axes (the theoretical distribution doesn't need to be perfect, though a reasonable approximation will help with detailed comparisons), or perhaps kernel density estimates, for example.
Answer: Because you asked about histograms, I'm assuming you are interested in comparing the shapes of two distributions to see how similar they are.  This is distinct from trying to visualize other aspects of the distributions, such as whether their means differ.
In general, histograms are a blunt tool for assessing the shape of a distribution (see this excellent answer: Assessing approximate distribution of data based on a histogram).  Although you would have the same problem with both distributions when trying to compare them, they won't necessarily cancel each other out.  Thus, histograms are not really a good choice for this task.
Your best bet is to use a qq-plot.  Nowadays, qq-plots are thought of as a means to compare an observed distribution to a theoretical one.  However, they were originally developed to compare distributions; the same situation you have here.  Interpolation methods are standardly used to match datasets of different sizes, so that is largely a non-issue.  If the shapes of the distributions are the same, the points will fall on a straight line.
To explore this suggestion, let's examine some data.  Here I simulate data from an exponential distribution and a chi-squared distribution.  Both are strongly right-skewed, but their shapes differ.  I will code the example using R:
set.seed(7264)  # this makes the example exactly reproducible
x1 = rexp(20, rate=1)
x2 = rchisq(100, df=2)

windows()
  qqplot(x1, x2)
  abline(0,1, col="gray")

The most basic finding is that the points do not fall on a straight line.  That means that the shapes differ.  A follow-up question is assessing how the distributions differ.  To some degree, that can be determined by examining aspects of the plot.  I added a 45$^\circ$ line through the origin.  To be clear, the points needn't fall on this line—they could fall on any straight line, but this particular reference line can help us understand how they differ:  Because the points start together at (0, 0) but are largely above the line, we can tell that the variance of X2 is larger than that of X1.  We can see that the last point is about (2.7, 10), so the maximum X2 value is much further out.  Because the middle value is above the line, we can tell the median of X2 is larger than the median of X1.  Furthermore, the concave-up curve implies that X2 is more skewed than X1.
Nonetheless, it is typically difficult for people to determine shapes, or relative shapes, from qq-plots.  I typically recommend people pair them with kernel density plots.  You can assess if the shapes are similar with the qq-plot, and then figure out what the shapes seem to be from the density plot.
windows()
  plot(density(x2),  col="navyblue", ylim=c(0, max(density(x1)$y)),
       xlim=c(0, max(density(x2)$x)), lwd=2, xlab="value", main="")
  lines(density(x1), col="green4", lwd=2)

  xs = seq(0,10,.1)
  lines(x=xs, y=dexp(xs, rate=1), lty=2, lwd=2, col="chartreuse")
  lines(x=xs, y=dchisq(xs, df=2), lty=2, lwd=2, col="cyan")
  legend("topright", legend=c("exp kernel density",
                              "chi-squared kernel density",
                              "exp theoretical density",
                              "chi-squared theoretical density"),
         lty=c(1,1,2,2), lwd=2, col=c("navyblue","green4","chartreuse","cyan"))

I think the shapes are easier to see here.  (I also included code for the true PDFs, if you want to see what they look like.)


Type 'Article | null' is not assignable to type 'void'

Question: I am trying to implement an interface into a class using typescript.
This is my class:
import connector from '../../../../common/mysql.persistence'
import { Article } from '../../domain/article'
import { ArticleRepository } from '../../article.repository'

export class ArticleMySQLRepository implements ArticleRepository {
  public async all(): Promise
 {
    const [rows] = await connector.execute(
      'SELECT * FROM articles ORDER BY id DESC'
    )

    return rows as Article[]
  }

  public async find(id: Number): Promise
 {
    const [rows]: any[] = await connector.execute(
      'SELECT * FROM articles WHERE id = ?',
      [id]
    )

    if (rows.length) {
      return rows[0] as Article
    }

    return null
  }

  public async store(entry: Article): Promise<void> {
    const date = new Date()
    const likes:number = 0
    const shares:number = 0

    await connector.execute(
      'INSERT INTO article(id, title, slug, description, content, likes, shares, updatedAt, createdAt) VALUES(?, ?, ?, ?, ?, ?, ?, ?)',
      [entry.id, entry.title, entry.slug, entry.description, entry.content, likes, shares, null, date ]
    )
  }

  public async update(entry: Article): Promise<void> {
    const date = new Date()

    await connector.execute(
      'UPDATE article SET title = ?, slug = ?, description = ?, content = ?, updatedAt = ? WHERE id = ?',
      [entry.title, entry.slug, entry.description, entry.content, date, entry.id]
    )
  }

  public async remove (id: Number): Promise<void> {
    await connector.execute(
        'DELETE FROM article WHERE id = ?',
        [id]
      )
  }
}

Then my interface:
import { Article } from './domain/article'

export interface ArticleRepository {
  all(): Promise

  find(id: Number):Promise

  store(entry: Article):Promise<void>
  update(entry: Article):Promise<void>
  find(id: Number): Promise<void>
}

I need to follow that interface for any other repository I want to implement. By the way, the editor is showing this error in the find method:

Property 'find' in type 'ArticleMySQLRepository' is not assignable to
the same property in base type 'ArticleRepository'.   Type '(id:
Number) => Promise
' is not assignable to type '{ (id:
Number): Promise
; (id: Number): Promise; }'.
Type 'Promise
' is not assignable to type 'Promise'.
Type 'Article | null' is not assignable to type 'void'.
Type 'null' is not assignable to type 'void'.t

Is there any setting I need to change on my tsconfig?
Comment: As dumb as it sounds, try restarting your IDE. Somehow it's seeing your `find` method as a weird callable object type with two call signatures, even though you've never defined that. Very strange.
Comment: @KelvinSchoofs following your recommendation I restart the IDE and I continue with the problem. It's a typescript error, because I get the same error running ts-node.
Answer: Ah, I didn't spot it at first, but your interface defines find() twice:
export interface ArticleRepository {
  all(): Promise

  find(id: Number):Promise
 // defined here
  store(entry: Article):Promise<void>
  update(entry: Article):Promise<void>
  find(id: Number): Promise<void> // defined here
}

You probably want to get rid of the last one.
Comment: You right. The second one supposed to be the remove method. Thanks.


Self referencing/constrained types without generics

Question: I have a generic type which ensures that object values are unique. I want to define a general type without generics which does the same thing based on the value being assigned to the variable of that type.
<code>type Unique<T extends Object> = {
  [x in keyof T]: Exclude<keyof T, x> extends infer K ? K extends keyof T ? T[K] extends T[x] ? unknown : never : never : never;
} extends infer G ? G[keyof G] extends never ? T : never : never

type AreUnique = Unique<{a: string, b: number}> // {a: string, b: number}
type NotUnique = Unique<{a: string, b: string}> // never

// My Goal

const foo: UniqueObject = {a: 'foo', b: 'bar'} // No Error
const bar: UniqueObject = {a: 'foo', b: 'foo'} // Error, not assignable to never

// My failed attempt

type UniqueObject = {
  [x: string]: Object
} extends infer G ? G extends Object ? Unique<G> : never : never;

const a: UniqueObject = {
  a: 'foo',
  g: 'bar'
}
</code>
Playground
Ultimately I don't want to use generics because I simply want to clarify/assert that a certain variable is an object of unique values. No functions involved.
Comment: So you want RecordWithUniqueValues? No, not possible without a function, you have to validate the type, it's undescribeable.
Comment: @Dimava Indeed, I arrived at the same conclusion. I've chosen to just use a one line function that returns the input to do the type checking. Thank you!
Comment: @Brother58697: Since this is impossible, could you write up your own answer that says so?  I don't know if anyone else would bother to do that.
Comment: @jcalz Only because it's you who asked. What do you think of `Unique`? Could it be made more concise or clearer?
Answer: Asserting this constraint using a concrete type isn't possible as of when this answer was written so I've resigned myself to using a short generic function that asserts on the parameter type and returns it.
<code>//Borrowing @Dimava's type name
type RecordWithUniqueValues<T extends object> = {
  [x in keyof T]: Exclude<keyof T, x> extends infer K ? K extends keyof T ? T[K] extends T[x] ? unknown : never : never : never;
} extends infer G ? G[keyof G] extends never ? T : never : never

function hasUniqueValues<T extends object>(obj: T extends RecordWithUniqueValues<T> ? T : never) { return obj }

const uniquePrimitives = hasUniqueValues({ a: 'foo', b: 'bar' } as const) // No error
const uniqueObjects = hasUniqueValues({ a: { a: 'foo' }, b: { b: 'bar' } }) // No error

const dupePrimitives = hasUniqueValues({ a: 'foo', b: 'foo' } as const) // Error - Not assignable to type never
const dupeObjects = hasUniqueValues({ a: { a: 'foo' }, b: { a: 'foo' } } as const) // Error - ...
</code>
Playground


RecyclerView inside CoordinatorLayout,AppBarLayout Scrolling issue

Question: I have this xml code in fragment:
<CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"                              xmlns:app="http://schemas.android.com/apk/res-auto"                         android:layout_width="match_parent"                    android:layout_height="match_parent"                       android:id="@+id/coordinatorLayout"                      android:fitsSystemWindows="true">
     <android.support.design.widget.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme"
            app:elevation="0dp">
     <android.support.design.widget.CollapsingToolbarLayout
                android:layout_width="match_parent"
                android:layout_height="300dp"
                app:layout_scrollFlags="scroll"
                android:id="@+id/collapsingToolbarLayout"
                app:statusBarScrim="@color/bestColor">
    <LinearLayout></LinearLayout>
            </android.support.design.widget.CollapsingToolbarLayout>
    <LinearLayout>
    <ImageButton>
     android:id="@+id/profile_header_trophies"
    </ImageButton>
    </LinearLayout>
        </android.support.design.widget.AppBarLayout>

<android.support.v7.widget.RecyclerView
    android:id="@+id/profile_recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
    </android.support.design.widget.CoordinatorLayout>

in Java Class on Item set ClickListener:
@OnClick(R.id.profile_header_trophies)
    public void profile_header_trophies_clicked() {
        if (myProfile != null) {
            appBarLayout.setExpanded(false, false);
            if (myProfile.getBests().size() == 0) {
                profile_recyclerView.smoothScrollToPosition(3);
            } else {
                profile_recyclerView.smoothScrollToPosition(2 + 20);
                }
            }

When I click to ImageButton, my RecyclerView scrolls to position, everything looks fine.
But if I put finger on AppBarLayout section (ImageButton) which visible(sticky) on top, and drag to bottom I have a bad scrolling.
My appbar start expanded, while my Recycler have some elements on top (they are hidden when scrolled).

I think this problem is setting behavoir. Because if I scrolling recycler first, AppBar doesnt start expanding, while Recycler not rich top of elements.
Thanks for your answers.
Comment: What is the name of BottomTab library? It looks very nice.
Comment: @NehaK find this answer help me. workaround: change root element to NestedScrollView, add recycler to it, and add to nestedscrollingEnabled = true to NestedScrollView (>21ApI) or use NestedScrollChildHelper for support libr
Comment: Did you tried applying layout_behavouir on collabsing toolbar ??
Comment: @Thracian this is simple layout with own logick, ty
Comment: Hi did you find solution?
Comment: should I add header also in NestedScrollView?
Answer: The bad scrolling once happened to me, it was happening because I was using RecyclerView inside another RecyclerView.
So when I try to scroll contents in the main RecyclerView it gave me this issue.
To resolve that issue I added this to RecyclerView:
recyclerView.setNestedScrollingEnabled(false);

For doing this in XML you can use:
android:nestedScrollingEnabled="false"

Comment: This is correct way to making toolbar `scrollFlags` working in case of nested RecyclerViews (to make toolbar scroll working, place `android:nestedScrollingEnabled="false"` in nested RecyclerViews)
Comment: What's worse, this is actually completely backwards. You should not have this, it will block the intended behaviour
Answer: with this, you tell it to "merge" the scrolling of the RecyclerView with the scrolling of its parent
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

if I well understood, you want to have the following scrolling behaviour:

if you are scrolling by touching outside the RecyclerView, it collapses the AppBar
if you are scrolling by touching inside it, it ignores the RecyclerView's scroll and collapses the AppBar, and once the AppBar is collapsed, it scrolls inside the RecyclerView

Could you confirm this is the desired behaviour please?
In such case, you may look at this answer, maybe it will help
Answer: It can be tricky and there's a few things you need to have in order for this to work.
You should use app:layout_scrollFlags="scroll|enterAlwaysCollapsed" in your CollapsingToolbarLayout instead of just scroll.
It's not clear where the tabs or buttons are in your XML layout, but if they are supposed to stay on screen then you need to pin them, so you would use app:layout_collapseMode="pin" for that element. Perhaps this is in the LinearLayout or ImageView?
If the LinearLayout holds something else then you should add some scroll flags to that too, probably best would be app:layout_scrollFlags="scroll|enterAlwaysCollapsed" if it is supposed to scroll off screen.
Lastly, make sure you are not disabling nested scrolling in your RecyclerView.
Answer: I think you need wrap content in NestedScrollView and set app:layout_behavior="@string/appbar_scrolling_view_behavior" in NestedScrollView
Answer: If you are using RecyclerView inside ViewPager then add this line to ViewPager: android:nestedScrollingEnabled="false"
It will solve your problem.


Making conditional variable with Pydantic's BaseSettings

Question: I am currently migrating my config setup to Pydantic's base settings. However I need to make a condition in the Settings class and I am not sure how to go about it:
e.g. I currently have:
class Settings(BaseSetting):
    name: str = "name"
    age: int = 25

and I want to add some logic like this:
class Settings(BaseSetting):
    name: str = "name"
    age: int = 25

    if age > 50:
        old_person: bool = True

What is the best way to go about this in pydantic?
Comment: Does this answer your question? [How to set a Pydantic field value depending on other fields](https://stackoverflow.com/questions/76301828/how-to-set-a-pydantic-field-value-depending-on-other-fields)
Comment: the answer for Pydantic v2 is in the link above!
Answer: Use a root validator. You probably also want to enable the validate_assignment option so that validations are run after changing an attribute via assignment:
from pydantic import BaseSettings, root_validator

class Settings(BaseSettings):
    name: str = "name"
    age: int = 25
    old_person: bool = False

    @root_validator
    def validate_old_person(cls, values):
      values['old_person'] = values['age'] > 50
      return values

    class Config:
        validate_assignment = True

If we run the following code:
# Creating an object with the defaults
settings = Settings()
print(settings)

# Create an object with an advanced age
settings = Settings(age=60)
print(settings)

# Create an object with the defaults, then modify it
settings = Settings()
settings.age = 60
print(settings)

The output is:
name='name' age=25 old_person=False
name='name' age=60 old_person=True
name='name' age=60 old_person=True

Comment: it seems root_validators got deprecated?
Comment: Yes, newer versions of Pydantic now use a type-annotation based system for defining validators. See [the documentation for details](https://docs.pydantic.dev/latest/concepts/validators/), and also the [notes about the changes around validators in 2.x](https://docs.pydantic.dev/2.0/migration/#changes-to-validators).


Facebook like box as popup

Question: I have the facebook like box in a block position and i want to make this block popup for first time visitors only. The popup will open only if the user visits the website for first time and will open once for every visitor.
Is it possible to implement this feature with existing modules? I am not a programmer.
Thank you
Comment: It will be hard without programming. There are modules for most *common* functionalities, but when it comes to some more elaborate business conditions, it starts to be hard. After all, programming languages was invented to allow humans easily define conditions for computers to follow ;) You may want to see [Rules](https://drupal.org/project/rules) module. And [Colorbox](https://drupal.org/project/colorbox). But I don't know how they play with each other.
Comment: Facebook Like is basically an iframe with code from Facebook in it. So what ever happens with the operation and control in that iframe is done by Facebook and not Drupal. It would be difficult to control unless there is setting in the Facebook app itself.
Comment: @QuentinCampbell It's pretty easy to move iframe to lightbox / color box. This part is controlled by Drupal. So it could be relatively easy to *code* it, for a programmer.
Comment: @Molot. True. I thought the popup he refers to are the popup generated by Facebook. As well as whether the user has 'Liked' the page or not before is also from Facebook.
Comment: @QuentinCampbell OP asked to move a whole block into a popup, so I interpreted it as Drupal-side stuff - block is div on a page and can be acted on like any other Drupal-generated div. I agree this question might be a bit clearer about that as there is a room for confusion.
Comment: If you want to do a popup for first time visitors, try the Popup announcements or Popup on load modules.  Somehow put the FB link to be popped up.
Comment: The popup announcement module is exactly what i needed. It provides a popup block where i paste the FB iframe code. It works perfect.
Answer: From Comments:
If you want to do a popup for first time visitors, try the Popup announcements or Popup on load modules.
Answer: This can be done easily if you have a control to find the visitor is first time coming to your site.
Set a persistent cookie once the use visists your site for the first time. Example Set a cookie called persistCookie to true/1. If the user visits for the next time check the cookie (persistCookie) exists/enabled to true.
var persistCookie = getCookie("persistCookie");
With a simple condition below you can have a control over the fb like popup,
/* Check the cookie exists in IN your Domain*/
if (persistCookie) {
    /*If cookie exists check the autoopen false*/
    $("#dialog").dialog({
        autoOpen: false
    });
} else {
    /*If cookie exists check the autoopen True*/
    $("#dialog").dialog({
        autoOpen: true
    });
}

Sample Demo listed here.

Facebook like box as popup


What's the difference between AzureAD scheme and OpendId aad scheme?

Question: I'm working on IdentityServer4, I used Microsoft.AspNetCore.Authentication.AzureAD.UI for Microsoft login and I see there is also OpenIdConnectExtension(AddOpenIdConnect("aad", ...)) for Microsoft login.
They both work the same in a client, but not when I set up an instance of IdentityServer like IS4 demo.io. Because if I use AzureAD.UI the idp is "AzureAD" and if I use OpenIdConnect extension the idp is "aad". Are they the same?
Answer: They're different packages. The former depens on the latter.


Lost our SVN server... what's the next step?

Question: So... we lost our SVN repository.  Through unfortunate circumstances, we also have no viable backup.
What to do?  Just do an "svn export" from someone with the most recent update and start over?  Is there any way to recover any history?  Or connect our local checkouts to a new server?
Comment: The very first thing to do is take a lesson from it and sort out your backups.
Answer: If all you have is a checkout, you've got a snapshot of the code, but no history.
You can create a new repo from that.  As to having existing checkouts connect to the new repo after you've set it up, it's probably more trouble than it's worth.  It'll be easier to create a new repo from the oldest checkout and then commit changes from newer checkouts as updates.
As to recovering history, I don't see how that's possible without a dump of the repo (via svnadmin dump) at some point in time.  Even an old dump would give you whatever history there was from the creation of the repo up to where the dump was made.
Maybe somebody used git-svn on your repo at some point?  That might have preserved some history but getting that back into a new Subversion repo might not be easy.
Data recovery is expensive, but it might also be an option if the storage device your repo was on hasn't been physically destroyed or destructively overwritten (and it wasn't an SSD).
Comment: Thanks Mark, you covered all the bases.  I will just recreate the repository from our latest working copy.
Answer: If you've truly lost your repo, with no backups, then yes - its gone. There's no magic that can restore truly lost data.
So, if you have a working copy from someone, you can create a new repo and add the files to it. You lose all history (obviously) but history has already been lost.
You could send the hard drive off to a recovery company to try and get any data restored. Once you have the repo (or at least the db/revs and db/revprops directories), you can re-create the repo from them (by copying them to a new repo's directory). If it turns out you don't have the entire db then things will be a bit more tricky, but its still possible to restore the rest of the data - you may have to send it all off to collabnet and get some support from them to restore the mangled data files into a new, working repo.
Now, you say "svn export" from someone with the most recent update.... that suggests the person has a backup copy that you can export from. Is this the case, or were you confusing it with a working copy?
Comment: Export doesn't require the server... I just meant that we should get the export from whoever had done the latest update on their tree, so we get as close as possible to what was on the server.  Thanks for the advice.
Answer: In addition to the useful information in the other answers, you might consider this as an opportunity to look into distributed source control (DVCS, e.g. git and mercurial, I think there are others as well).  With a DVCS each user has a copy of the repository, providing more redundancy, reducing the risk of losing the full repository.
There is a very nice online book about git here: http://progit.org/
Comment: +1 for taking the opportunity to migrate to git (or another distributed version control system, like Mercurial).  You may have just lost all your history, but taking advantage of the disaster can net you some long-term wins...
Comment: +1 @voretaq7: thanks for your thoughtful expansion my my answer, it motivated me to edit - taking your comment as an example of a much more useful contribution.  Thank you.
Answer: As Mark is saying above, if you don't have a dump of the repo (from the server), you can't construct the history.  An SVN checkout only contains the tip of where it is checked out.  Sorry, you are SOL. I guess this is why people like distributed source control.  In our company I set it up to send svn diffs to amazon s3 twice a day.
Comment: +1 for S3 as a inexpensive off-site backup store for SVN
Answer: First: having no backup of a SVN-Repo is a pain and should never happen.
But, the same happened to me. I started to add thousands of files again, file for file, because I don't want to checkin temporary or other no-source-files.
Nearly all history is lost. But not the last checkin state is lost!
I managed to get the last checkin state, so the new repo was similar to the old one and I got also the last diff!
This is my recipe:

backup your local checkout
clean up your local checkout see: screenshot svn-cleanup_to_last_state
create new repo
checkout new repo into new empty folder
copy the old cleaned copy into new folder
add simply all files
commit it
overwrite your new checkout folder with the backup from 1.

now you've got all files as before, with the last diff and only the files versioned, like before.


How many parameters can your model possibly have?

Question: This is a rather general question that came to mind while attending a talk on deep learning. A deep net had been trained with 32x32 images and there were 10 classes(CIFAR dataset). There were about 60000 images in the dataset. The person giving the talk said that the network has learnt more than 10 million parameters. That seems a bit weird to me because the number of pixels in all these images is around that order. How does one justify a model using this many parameters? Don't the chances of overfitting to a dataset increase with the number of parameters?
Comment: If there are 60000 images with 32*32 pixels each, and each pixel contains red, green and blue values, the total number of inputs would be 32*32*60000*3 = 184320000, some 18 times more than the number of parameters.
Comment: Those are approximate numbers. I might have worded by question wrongly. How do you not overfit when the parameter space is of such a huge order would be a better way to put it?
Answer: Yes, there should not be 10 million parameters of a model which trained on CIFAR-10 as its input dimension is small (32*32*3 = 3072). It can barely reach to million of parameters, but that model becomes prone to over-fitting. Here is a reasonable structure of convnet trained on CIFAR-10;
2 convolution layer 1 fully connected layer and 1 classification layer (also fully connected). Most of the parameters are concentrated on the last two layers as they are fully connected.

Filter size at the first convolution layer is 7x7@32
Pooling size at the first pooling layer is 2x2
Filter size at the second convolution layer is 5x5@16
Pooling size at the second pooling layer is 1x1 (no pooling)

I'm assuming a valid convolution and stride number of pooling is equal to pooling size. At his configurations, dimension of the first feature maps are (32-7+1)/2 x (32-7+1)/2 = 13x13@32. Dimension of the second feature maps are (13-5+1)/1 x (13-5+1)/1 = 9x9@16
As convolution layers are rolled into vector before passing into fully connected layer, dimension of the first fully connected layer is 9*9*16 = 1296.
Lets assume that the last hidden layer contains 500 units (this hyper-parameter is the most significant one for total number of parameters). And the last layer is 10.
At total the number of parameters are 7*7*32 + 5*5*16 +  1296*500 + 500*10 = 1568 + 400 + 648000 + 5000 = 654968.
But I expect smaller network can yield better results as the number of samples is relatively small. So if the 500 neurons reduced to 100 neurons, the total number of parameters reduces to 1568 + 400 + 129600 + 5000 = 136568. Maybe it would be better to include another pooling layer at the second layer, or discarding first fully connected layer.
As you can see most of the parameters are concentrated at the first fully connected layer. I don't think deeper network can yield significant gain as the dimension of input layer is small (relative to ImageNet). So your point is right.
If you concern about over-fitting you can check 'Recuding overfitting' section of Alex's convnet paper
Comment: Actually I have  to disagree with you, ResNet was trained on Cifar10 as well, using 50,101, and later 1000 layer and got ~95.50 percent accuracy.
so a deep network can do pretty well on a small dataset like cifar10 as well.
I myself could get 93.86% accuracy on cifar10 using my own deep architecture (a 12 layer network) .
apart from that, using BN and dropout helps with over-fitting and that would not be a problem as ResNet showed.


Error in mapmyindia API

Question: enter image description hereI am developing an application in which user search for places using Auto-suggest API of mapmyindia. But in the activity, I am not able to import License Manager. I have followed every step for setting up project guided by mapmyindia website.
demoapplication
<code>import android.app.Application;

import com.mmi.LicenceManager;

public class DemoApplication extends Application {

@Override
public void onCreate() {
    super.onCreate();
    LicenceManager.getInstance().setRestAPIKey("");
    LicenceManager.getInstance().setMapSDKKey("");
}
}
</code>
Comment: Can you check if you have included jar/aar file into your project.
Comment: yes, compile files('libs/map_sdk_2.1.jar')
Comment: Sorry for delayed response, can you check if you are able to run demo application? .
Comment: Actually, it got some of it's own errors, getResources() and getActivity() are highlighted in red in the activities.
Comment: did you tried syncing the gradle?
Comment: yes, i did sync it.
Comment: try to create new project and run it without adding mapmyindia jar. This Issue is not related to library.
Comment: Ok, i did it without adding jar and it is showing the same thing in demo application, you can see the image from the link i have added
Answer: OK, i figured it out, i did not added map jar file into libs folder that is why some of its feature were not working, i pasted the file from libs folder from mapmyindia SDK android project.
Comment: can u share the jar file ?  I am getting zip file when i download from here [https://www.mapmyindia.com/api/advanced-maps/sdk/android]

Error in mapmyindia API


Keys in Real Book vs. Real Vocal Book

Question: I like seeing the lyrics to jazz standards, but I would also like to practice standards in their original/standard keys.
How many tunes in the Real Vocal Book "High Voice" (which has the lyrics) are actually transposed compared to their Real Book version?
Comment: Keys in real books are not always the same as original either. A famous example is _Girl from Ipanema_, most often printed in F, while composed in Db... or maybe G?
Comment: See 'Ranges in the real vocal book'. What is so important about 'the original key' - which may well have *not* been the key the composer wrote it in anyway.
Comment: @user1079505 Thanks, I was aware of this famous example, and there will likely further exceptions. Still, I would expect there to be some bias towards original (or just popular) keys in the *Real Book* and I'm wondering if this bias is completely lost due to transposition in the vocal edition.
Answer: The only way to know is to have both books side by side as there is no key list in the table of contents as I recall. A few things to keep in mind are:
Are you looking for the true original keys or the keys the songs are mostly played in nowadays? Sometimes you have to go all the way back to an ancient recording or a very old movie or musical to find the true original key as a lot of standards came from cinema and musical theater. Many times the key of an iconic recording will become the new "standard" key.
The Real Book (or any published sheet music for that matter) is not guaranteed to have all songs in original keys. "Ipanema" was mentioned in comments, F in the book, Db on the recording. "Triste" is Bb in the book, A on the recording. "Misty" is in Eb, the Johnny Mathis key in the book (iconic recording) but Errol Garner, the composer played it in a Ab. "Windows" by Chick Corea is Bm but was originally in Bb minor in the Real Book (I believe that one has been corrected). These are just a few examples.
Your "high vocal" book probably contains at least some original keys if the songs were originally performed by a female (or high?) vocalist.
Comment: Good examples. Then we have the recordings that were speeded up/slowed down. I can't think what key some of those would have.
Comment: @Tim Not to mention the B 1/2 flats or the slightly under F's etc. lol!

Keys in Real Book vs. Real Vocal Book


Cloud file storage with file tagging and search by tags/filename

Question: My project needs to meet next requirements.

store large amount of files for reasonable price
tag individual files with custom tags
have API method to search files by name (contains) and tags (exact)
do it all via JS SDK (keep project serverless)

I made some work with Amazon S3 and turned out

no search method in JS SDK http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#listObjectsV2-property
listObjects accepts param Key Prefix (i.e. filename starts with), so there is no way to find by contains
no param to search by tag at all, i can only get it for individual file with getObjectTagging

So question is - what stable service can i use for file storage WITH functionality described above
Azure?  Google Cloud? Backblaze B2? something else?
thanks!
Answer: If you use Azure blob storage, you can use Azure Search blob indexer to index both the metadata and textual content of your blobs. For a walkthrough of setting this up, see Build and query your first Azure Search index in the portal.
Comment: thanks. in the meantime i found https://www.storagemadeeasy.com/

It is aggregator   40+ cloud service providers with tagging ability and S3 compatible API


Issue when trying to retrieve data from a referenced table using Hibernate.

Question: Error:

org.hibernate.LazyInitializationException  SEVERE: could
  not initialize proxy - the owning Session was closed

Code:
System.out.println(resultStudent.getStudentAddress().getCity());

Here the getAddress() returns an Address object and i am trying to get the city of the student using getCity() of Address class.  Why do I get the above error?
Just getting the Address object doesn't throw any error.
Answer: Hibernate loads the relationships lazily. So when you are navigating the relationship you need to either have the hibernate session open or fetch the data eagerly. Read the below articles

Understanding Lazy Fetching
Fetch strategies


How to construct a term of a particular type

Question: I am reading the article "Introduction to Type Theory" by Herman Geuvers, the chapter explaining the Fitch style of natural inference.  I stuck at the exercise 1.3 (first two are simple):

Construct a term of type ((α→β)→α)→(α→α→β)→α

Only abstraction and application rules are allowed.
This math.stackexchange article suggest that to get a term of type α I should assume
<code>| x : (α→β)→α
|-----------------
|   | y: α→(α→β)
|   |-------------
</code>
I have no idea what next.
UPD:
The full answer is:
<code>| x : (α→β)→α
|-----------------
|   | y : α→(α→β)
|   |-------------
|   |   | z : α
|   |   |-----------
|   |   | yz : α→β
|   |   | yzz : β
|   |   λz.yzz : α→β
|   |   x(λz.yzz) : α
|   λy.x(λz.yzz) : α→(α→β)→α
λx.λy.x(λz.yzz) : ((α→β)→α)→(α→(α→β))→α
</code>
Thank you Rob
Answer: It may help to think of this as an exercise in (functional) programming. You want a function that maps a function $f$ of type $ (\alpha \rightarrow \beta) \rightarrow \alpha$ and a function $g$ of type $\alpha \rightarrow \alpha \rightarrow \beta$ to an element of type $\alpha$. To do this, the only promising approach is to apply $f$ to something and that something must have type $\alpha \rightarrow \beta$. But we can get something of that type using $g$, namely $\lambda x :\alpha \cdot g x x$. So the bottom line of the proof can be the following $\lambda$-expression:
$$\lambda f : (\alpha \rightarrow \beta)\rightarrow \alpha\cdot\lambda g : \alpha \rightarrow \alpha \rightarrow \beta\cdot f(\lambda x :\alpha\cdot g x x)$$
Comment: Thank you, looks like it really helps.  Now I am trying to make the inference.


Make Synchronous database operation asynchronous

Question: I want to use bulk database operations in Asp.Net MVC 4.5.2 app.
There is good lib Z.EntityFramework.Extensions, but it costs 800$.
And free lib EF.BulkExtensions, but it doesn't contains async methods.
Question: if I will do this trick
await Task.Run(() =>
{
    MyDatabaseContext.BulkUpdate(entities);
});

Will it work the same as if i used the async method (in terms of working with IIS' thread pool ) ?
Comment: It would be a lot faster if you inserted the rows into a staging table using `SqlBulkCopy` and then performed an UPDATE on the target that joined with the staging table ie `UPDATE target SET .... FROM target inner join staging on staging.ID=target.ID`.
Comment: [EF.BulkExtensions](https://github.com/borisdj/EFCore.BulkExtensions) *does* have a `BulkUpdateAsync`. There's no `bulk update` or `delete` in reality though, only BULK INSERT (ie inserting lots of data with minimal logging). All libraries that talk about "bulk updates" actually generate a SQL command in the background that typically sends the data as a table-valued type or part of a MERGE command.
Comment: @PanagiotisKanavos no, it doesn't have async. I'm using EF.BulkExtensions.1.4.2 with .netframework 4.5.2.
Your link points to EFCore  lib. I'll try to upgrade framework version.
Comment: EFCore  compatible with net framework 4.6+ and EF.Core 3+.
But I have dependency on EntityFramework.extenDED, which is depends on simple entity framework
Comment: Don't use that method then. There's no BULK UPDATE command that behaves the way `BULK INSERT` does, ie using minimal logging to insert data in the fastest way possible. What every library claiming to do "bulk updated" does, is generate a plain-old, fully logged `UPDATE` command that somehow joins with the new data, either in the form of a table-value parameter or data in a `VALUES` clause. Neither of these options can make good use of indexes which results in suboptimal performance and just doesn't scale to a large number of rows
Comment: In any case this is a long running operation that *shouldn't* be performed by an action. It should be handled as a background task using eg `QueueBackgroundWorkItem`. Check [How to run Background Tasks in ASP.NET](https://www.hanselman.com/blog/how-to-run-background-tasks-in-aspnet)
Answer: You should at least do something like:
var task = new Task(() => { MyDatabaseContext.BulkUpdate(entities) }, TaskCreationOptions.LongRunning);
await task;

This ensures, that your task runs in its own thread and doesn't occupy a task from the task pool.


Given a large supply of jelly beans of 10 different colors, how many ways are there to make up a bag of 5 jellybeans?

Question: Question: Given a large supply of jelly beans of 10 different colors, how many ways are there to make up a bag of 5 jellybeans?
Answer: The answer is 14C9 or $\frac{14!}{(14-9)!9!}$ = 2002
I know that this is a combination problem with repetition, but I don't know why that is the answer. You are choosing 5 different colored jelly beans from 10 colors of jelly bean with replacement so shouldn't it be 14C5 ????
After all, the formula for the number of combinations with repetition allowed is:
$\frac{(n+r-1)!}{r!(n-1)!}$
Answer: Ultrashort answer:  14C5 = 14C9.
This is a stars and bars problem.  (Theorem 2 at the link)
You have nine bars
$$  |||||||||  $$
The spaces before, between, and after the bars correspond to the ten colors (where the $\cdots$ represent other colors)
$$  \text{orange}|\text{red}|\cdots|\cdots|\cdots|\cdots|\cdots|\cdots|\cdots|\text{black}  $$
You distribute 5 stars among the bars, corresponding to a choice of numbers of jelly beans of the corresponding colors.  For example
$$  \ast \ast |\ast||||||||\ast\ast  $$
represents two orange, one red, and two black jelly beans.
So the revised question is "How many ways to arrange these $n$ stars and $k-1$ bars are there?"  (There are $k = 10$ colors, but only $k-1 = 9$ bars by fenceposting, by our use of the spaces between the bars to represent colors.) Well, there are $n+k-1$ objects and choosing the positions of the stars completely specifies the locations of the bars (and vice versa), so there are $\binom{n+k-1}{n} = \binom{n+k-1}{k-1}$ arrangements of stars and bars.  For $n = 5, k = 10$, this gives $\binom{14}{5} = \binom{14}{9}$ different bags of five jellybeans from 10 colors.
Many people choose to remember the form $\binom{n+k-1}{k-1}$ because the "$-1$" is common to both arguments in the binomial coefficient.  There is another way to represent this number using multinomial coefficients, but that is not your question.


How to rotate compressed log file on centos 7?

Question: I have customized kafka server and the app is creating log files and configures log rotation. My problem is that I need to compress the rotation logs that were created.
I tried to create new log rotation file on /etc/logrotate.d with the following configuration:
<code>/opt/kafka/logs/server.log.* {

  monthly

  compress

  dateext

  dateformat _%Y-%m-%d

  extension .tar.gz

  missingok

  ifempty

}
</code>
With this configuration I cannot compress my old files, and the message is:
log does not need rotating (log has been already rotated).
Thanks for your help :-)
Comment: Perhaps https://unix.stackexchange.com/questions/15546/logrotate-log-does-not-need-rotating-why will help? Try to Google the error message first, to show you did some research prior to posting.
Answer: Try with the following configs:
General logs files:
<code>/var/log/kafka/*.log
{
    daily
    dateext
    dateformat _%Y-%m-%d-%s
    compress
    nodelaycompress
    nocopy
    nocopytruncate
    nocreate
    missingok
    notifempty
}
</code>
Logs generates by log4j
<code>/var/log/kafka/*.log*[0-9][0-9]
{
    daily
    # remove after compressed
    rotate 0
    nodateext
    compress
    nodelaycompress
    nocopy
    nocopytruncate
    nocreate

    missingok
    notifempty
}
</code>
Change the frequency as you need


highcharts - changing color of area depending on y-value

Question: How can I change the color of an area type plot depending on y-values which are in same series ?

type of chart - area

y- values possible - 1, 2 and 3

desired output - Green color area for values between 3 & 2, red color area for values between 2 & 1
Comment: Please explain clearly what you want, and create a reference fiddle !
Answer: There are two options, your desrciption lacks of information, so I show you both of them: http://jsfiddle.net/4vzEt/13/

Threshold with negative color:
<code>$("#container1").highcharts({
  series: [{
    threshold: 2,
    negativeColor: 'red',
    color: 'green',
    type: 'area',
    data: [1, 2, 2, 1, 3, 3, 2, 3, 2, 1, 1, 3, 1, 1]
  }]
});
</code>
Note: Threshold sets starting y-value of series in that value.
Gradient color:
<code>$("#container2").highcharts({
    series: [{
        threshold: 1,
        color: {
            linearGradient: {
                x1: 0,
                x2: 0,
                y1: 0,
                y2: 1
            },
            stops: [
                [0, 'green'],
                [0.49, 'green'],
                [0.5, 'red'],
                [1, 'red']
            ]
        },
        type: 'area',
        data: [1, 2, 2, 1, 3, 3, 2, 3, 2, 1, 1, 3, 1, 1]
    }]
});
</code>
Note: Markers inherit series color. Disable them, or set for each point color directly.


For which $n$'s $2^n\mid n!$?

Question:
I am trying to find for which natural numbers $2^n$ divides $n!$.

This is similar to asking for which $n$, $\nu_2(n!) \geq n$, where $\nu_2$ is the 2-adic valuation.
I am pretty stuck on finding these $n$'s. I have tried for example $n=10$ and it didn't divide $10!$.
I have tried to work with the Legendre formula but achieved no progression
Comment: $2^{n-1}$ divides $n!$ when $n$ is a power of $2$.
Answer: $2^n$ never divides $n!$ because
$$\nu_2(n!) = \sum_{k=1}^{\lfloor\log_2(n)\rfloor} \Big\lfloor \frac{n}{2^k} \Big\rfloor <  \sum_{k=1}^\infty \frac{n}{2^k} = n.$$
Edit: Here, $n>0$.
Comment: You used https://en.wikipedia.org/wiki/Legendre%27s_formula.

For which $n$'s $2^n\mid n!$?


Non-perpendicular hyperplane decision trees

Question: The usual implementation of decision trees, and estimators based on ensembles of decision trees, use decision trees that threshold on a single predictor at each split. So they divide the feature space into perpendicular hyperlanes.
Is there any benefit to generalizing this a tiny bit. So at each split, the decision tree uses 2 features, and thresholds based on whether $ax_1 + bx_2 \ge T$. In other words, the feature space is now getting divided into hyperplanes that are not necessarily perpendicular.
I think the main disadvantages would be:

increased computation, and
overfitting since now using more degrees of freedom

I guess to help both of these issues, randomization could be used analogous to extra trees. The pair of features could be randomly selected, instead of exhaustively trying every pair, and the threshold T and parameters a and b could be chosen randomly as well (perhaps from a set of few discrete values such that the hyperplanes could have a few orientations).
Are there some cases where you think these non-perpendicular decision trees would be an improvement over the regular implementation of various decision tree algorithms?
Comment: I'm not sure if they are technically hyperplanes. Trees can successively use the same variable multiple times at different heights of the tree. The decision boundaries do not extend indefinitely therefore, but end when they encounter other decision boundaries.
Comment: True, I guess some boundaries are hyperplanes and the rest have finite edges along subsets of the axes.
Answer: Oblique Random Forests
The conventional random forest algorithm selects one feature to split at each node. This implies that if we want to achieve some kind of elaborate split (like defining a diagonal in two dimensions), many such splits will be required. On the other hand, if each node is something like a logistic regression, then we can directly accommodate linear combinations of features in a single node.
Breiman mentioned this possibility in passing in his original random forest paper, but has not drawn the same attention that "orthogonal" random forest have.
"On Oblique Random Forests"
Bjoern H. Menze, B. Michael Kelm, Daniel N. Splitthoff, Ullrich Koethe,
and Fred A. Hamprecht
Rotation Forests
Rotation forest uses PCA on the subsample of selected features at each split, and then splits in the orthogonal PCA basis. The effect is kinda like what you suggest here, because the projection into the PCA basis is a linear combination of the inputs.
This isn't exactly the same as your suggestion, though, since the parameter estiamtes in the linear combinations of PCA are entirely fixed by the data used to fit them. This stands in contrast to the method you propose, which estiamtes a linear function of the data.
Clearly, the cost of doing PCA at each split could be quite high, especially when the number of observations is large.
But the upside to rotation forest is that it can ameliorate a weakness of Random Forest. Random Forest struggles when the decision boundary is diagonal in the original space. To the extent that rotation by PCA re-aligns that diagonal, the model will be better able to capture that effect.
Here's the paper:
Rodríguez JJ1, Kuncheva LI, Alonso CJ. "Rotation forest: A new classifier ensemble method." IEEE Trans Pattern Anal Mach Intell. 2006 Oct;28(10):1619-30.


HTML5/javascript streaming of an already playing list of videos

Question: I am currently trying to set up a sort of video stream using HTML5. The goal is this: I need to create a television-like interface wherein the user, once they click on the link to the stream, will see the already in progress video.
Right now, I have set things up to a point where I can play a playlist of videos in a row, but I can't figure out how to set it up so that it will already be playing when the user accesses it. basically, I want it to perpetually play regardless of user input.
I originally tried using streaming software, but made little to no progress. Also, considering that the long term goal is to run approximately 60 channels at one time, having the overhead of running software like that would be bad.
The filetype of the videos is .mp4.
I am currently using a javascript linked list to cycle through the videos. That is working great, I just need it to not have to be started every time.
Thank you for any help. The implementation of this has been driving me crazy for about two weeks now, and I'm at a pretty hard dead end at this point.
Answer: No need to play all of them, instead:

keep track of when those videos should have started (e.g. video 1 starts at 8pm EST)
When they click to go to a channel, start that video playing at the elapsed time

For example, if video 1 starts at 8pm EST and it's now 8:17pm EST, when they click for that video, it would start playing it at 17 minutes into the video. This will simulate that the channels/videos have been playing all along.
Comment: I hadn't even thought of that. So it would run based on time instead of perpetually. Each user would have the video start based on a running time variable.

Thank you very much, that will work much much better than what I was trying.


Unable to cast COM object '(Microsoft.Office.Interop.Excel._Worksheet)workbook.Sheets'

Question: I am trying to read excel file through this way but getting error:
Microsoft.Office.Interop.Excel.Application appExcel;
Microsoft.Office.Interop.Excel.Workbook workbook;
Microsoft.Office.Interop.Excel.Range range;
Microsoft.Office.Interop.Excel._Worksheet worksheet;

appExcel = new Microsoft.Office.Interop.Excel.Application();
workbook = appExcel.Workbooks.Open(path, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
// getting error on the below line
worksheet = (Microsoft.Office.Interop.Excel._Worksheet)workbook.Sheets;

Error is below..

Unable to cast COM object of type 'System.__ComObject' to interface
  type 'Microsoft.Office.Interop.Excel._Worksheet'. This operation
  failed because the QueryInterface call on the COM component for the
  interface with IID '{000208D8-0000-0000-C000-000000000046}' failed due
  to the following error: No such interface supported (Exception from
  HRESULT: 0x80004002 (E_NOINTERFACE)).

have any idea about this exception?
Answer: Apparently, workbook.Sheets is not a type of Worksheet.
worksheet = (Microsoft.Office.Interop.Excel._Worksheet)workbook.Sheets;

UPDATE:
Sheets is a collection of Worksheet, so instead add an index:
Microsoft.Office.Interop.Excel._Worksheet worksheet;

worksheet = (Microsoft.Office.Interop.Excel._Worksheet)workbook.Sheets[0];

Hope it helps!
Comment: range = worksheet.UsedRange;  'Microsoft.Office.Interop.Excel.Sheets worksheet' does not contain definition for "UsedRange'
Comment: I think worksheet is a collection. Try adding an index worksheet[0].UsedRange;
Comment: I updated my answer. I guess you can just do it like this so you don't have to change alot on your code.
Comment: Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX)) on this line 'worksheet = (Microsoft.Office.Interop.Excel._Worksheet)workbook.Sheets[0]'
Comment: Excel sheet only have one sheet & one column
UserID
48689
48690
48690
48690
48690
48690
48690
48690
Comment: I suggest you use breakpoints to inspect your code. Check what's wrong with the `workbook`. As far as I know, the casting is now solved. I'm guessing the problem is now with the `Workbook` (how you set it up)


JS: armazenar valores sorteados em array

Question: Boa noite. Estou construindo um Bingo com Javascript e quero armazenar os números já sorteados numa array para depois consultá-la e evitar repetições, mas não sei como fazer isso. Alguém pode ajudar, por favor?
O código está assim:

O jogo está rodando em https://monono.com.br/bingo
Aceito sugestões de melhorias.
Obrigado
Answer: Boa noite, Roberto. Logo após você sortear um número verifique se existe algum numero idêntico no array "sorteados", se existir, você executa a função getBingo() novamente (isso se chama recursividade, quando você chama a função dentro dela mesma) e obtém outro numero, e faz a mesma verificação, caso ele não exista no array, voce adiciona ele com o .push e continua com o resto da função. Outra dica: ao invés de usar um if dentro de um else, basta usar "else if(condição){}". Gostei bastante do layout da sua aplicação, parabéns. :)
function getBingo() {
  var bola = Math.floor(Math.random() * (75 - 1 + 1)) + 1;
  let indice;
  for (indice = 0; indice < 99; indice++) {
    if(sorteados[indice] == bola) {
      getBingo();
    }
  }
  sorteados.push(bola);
  console.log(...sorteados)
  if (bola < 16) {
    letra = "B";
  }
    else if (bola < 31){
      letra = "I";
    }
      else if (bola < 46) {
        letra = "N";
      }
        else if (bola < 61) {
          letra = "G";
        }
          else if (bola < 76) {
            letra = "O";
          }
}

Comment: Isso está com cara de loop infinito. Você tentou rodar essa função? Execute ela duas vezes.
Comment: sim, me enganei usando aquele else dentro do for. obrigado! :D
Comment: Obrigado, João, mas o que não estou conseguindo fazer é alimentar a array com as bolas sorteadas.
Answer: O método find do javascript parece se adequar melhor para o que pretende.
//...
let sorteados = [99];

function getBingo() {

  let bola = Math.floor(Math.random() * (75 - 1 + 1)) + 1;

  /*
   O método find () retorna o valor do primeiro elemento na
   matriz (Array) que satisfaz a função de teste fornecida. Caso contrário,
   undefined é retornado
   */

  let existeBola = sorteados.find(item => item === bola);

  if (existeBola) {
    getBingo(); // Tenta novamente
    return;
  }

  sorteados.push(bola);

  if (bola < 16) {
    letra = "B";
  } else {
    if (bola < 31) {
      letra = "I";
    } else {
      if (bola < 46) {
        letra = "N";
      } else {
        if (bola < 61) {
          letra = "G";
        } else {
          if (bola < 76) {
            letra = "O";
          }
        }
      }
    }
  }

  //...

}

Já agora aconselho vivamente a considerar uma condição de desistência (paragem da repitição) caso ele não encontre nenhuma bola não sorteado.
EX:
//...
let sorteados = [99];
let max_tentativas = 10; // Numero maximo aceitavel de tentativas;
let tentativas = 0;

function getBingo() {

  let bola = Math.floor(Math.random() * (75 - 1 + 1)) + 1;
  let existeBola = sorteados.find(item => item === bola);

  if (existeBola) {

    if (tentativas < max_tentativas) { //

      getBingo(); // Tenta novamente

    } else {

      alert('Atingiu o numero maximo de tentativas');
      // ...
    }

    tentativas++;

    return; // Para tudo aqui
  }

  sorteados.push(bola);
  tentativas = 0; // Zera o numero de tentativas ja feitas

  if (bola < 16) {
    letra = "B";
  } else {
    if (bola < 31) {
      letra = "I";
    } else {
      if (bola < 46) {
        letra = "N";
      } else {
        if (bola < 61) {
          letra = "G";
        } else {
          if (bola < 76) {
            letra = "O";
          }
        }
      }
    }
  }

  //...

}

Comment: Obrigado, Cristiano, vou testar suas sugestões.
Answer: Melhor que ver se sorteou repetido e sortear de novo é usar um algoritmo diferente, tipo o Fisher-Yates que lhe randomiza o array, e depois você apenas retira elementos do inicio, com a garantia que são únicos.
Exemplo:

//função para baralhar os elementos do array
function shuffle(array) {
  let tamanho = array.length, temp, i;
  while (tamanho) {
    i = Math.floor(Math.random() * tamanho--);
    temp = array[tamanho];
    array[tamanho] = array[i];
    array[i] = temp;
  }
  return array;
}

const QTD_BOLAS = 75, LETRAS = "BINGO";
let bolas = []; //criar o array para as bolas
for (let i = 1;i <= QTD_BOLAS; ++i){
  bolas.push(i); //inserir as bolas disponiveis
}
shuffle(bolas);

document.getElementById("sortear").addEventListener("click", function(){
  let bolaSorteada = bolas.shift(); //retirar a primeira bola do array
  let bolasPorLetra = QTD_BOLAS / LETRAS.length;
  let letra = LETRAS[Math.floor((bolaSorteada - 1) / bolasPorLetra)]; //obter a letra escolhida
  console.log("Sorteada - " + letra + bolaSorteada);
});
<button id="sortear">Sortear</button>

Note que substitui os ifs que tinha para descobrir a letra por uma lógica diferente. Neste código, ele assume que existem X bolas para cada letra, dividindo a quantidade de bolas que existem pela quantidade de letras, que no caso vai dar 15 letras por bola. O que significa que se dividir a bola sorteada por 15, da-lhe a posição da letra a mostrar, o que torna o código mais direto e menos repetitivo.
Exemplo:

Se sair a bola 10, ao dividir por 15 resulta em 0 o que lhe dá a primeira letra da palavra "BINGO" o "B".
Se sair a bola 20, ao dividir por 15 resulta em 1 o que lhe dá a segunda letra da palavra "BINGO" o "I".

Se quiser pode também evitar que o usuário sorteie bolas se já saíram todas, verificando o tamanho do array bolas antes de sortear, algo como if (bolas.length > 0).


Can I download my iPhone app xcode?

Question: I have a quick question, I have lost the files and code for my iPhone app. I have my iTunes connect login details... is there any way of getting the code from my developers section?
Thanks for any pointers.
Answer: No, neither iTunes connect nor Apple Developer act as source control. You can get the provisioning profiles and create new certificates, but your code is yours to manage.
I would very strongly recommend looking into proper source control in the future (GitHub, for example).
Answer: Sorry, but I'm afraid not. The "code" you upload to iTunes connect is actually your project's binary. You can't download this from the portal, and even if you could, there's no way to transcribe it back into your original application code.

Can I download my iPhone app xcode?


What is the idiomatic way to refer to a singleton list constructor in Haskell?

Question: Short form: is there a more idiomatic way to write (\a->[a]) ?
Long form: For any data type Foo a, if I have a function f :: Foo a -> b and I need to write something like...
wrapAndF a = f $ Foo a

...I can make it point-free by writing
wrapAndF = f . Foo

But if my function g :: [a] -> b operates on lists and my wrapper looks like this...
wrapAndG a = g [a]

...what is the most idiomatic way to write it point-free? I know I can write an explicit lambda:
wrapAndG = g . (\x->[x])

or, mirroring the use of the constructor in the Foo example, use the list constructor (:), but then I have to flip the arguments:
wrapAndG = g . flip (:) []

...but what is the idiomatic way to refer to the singleton list constructor function? I expected to find a standard function with signature a -> [a], but I couldn't find it on Hoogle or Data.List.
There is of course the alternative of simply not writing it point-free (and that is certainly a valid answer) but since passing type constructors around as wrapper functions seems really useful, it felt odd that I couldn't find a standard function to wrap a value into a list, so I figured I may be missing something.
Comment: Seems a bit obvious, but what about an operator section - `(:[])`. 5 characters, technically longer than `pure` but it certainly better expresses the intent of 'making a singleton list'.
Comment: @user2407038 I think I saw that called the "monkey head operator" a few years ago. I'd guess it's fairly idiomatic.
Comment: Indeed, solutions using `return` or `pure` are interesting because they look beyond the list, but I agree that `(:[])` represents the intent in that particular case better!
Comment: I does indeed look like a monkey head ! Lol
Answer: Echoing the comments by user2407038 and chi, (:[]) is a fine way of spelling it if you want something specific to lists:
wrapAndG = g . (:[])

Answer: For lists pure does what you want:
wrapAndG = g . pure

Answer: Just found the answer, thanks to pointfree.io, which was linked to in another question. A nice, point-free way to write wrapAndG is
wrapAndG = g . return

This uses the fact that lists are instances of Monad.


Unable to add newly created term to the node

Question: I want to add new taxonomy term to group of nodes that have specific terms already.
To retrive them I use this function
<code>function getNodesByTaxonomyTermIds($termIds){
    $termIds = (array) $termIds;
    if(empty($termIds)){
      return NULL;
    }

    $query = \Drupal::database()->select('taxonomy_index', 'ti');
    $query->fields('ti', array('nid'));
    $query->condition('ti.tid', $termIds, 'IN');
    $query->distinct(TRUE);
    $result = $query->execute();

    if($nodeIds = $result->fetchCol()){
      return Node::loadMultiple($nodeIds);
    }

    return NULL;
  }
</code>
then in foreach loop I'm adding im adding them to new term based on their creation date but adding does not work.
Here is my field structure:

Firstly I tried all of those:
<code>$node->set('field_news_categories', ['target_id' => $termID])
$node->set('field_news_categories', $termID)
$node->set('field_news_categories', array($termID))
$node->field_news_categories->setValue(['target_id' => $termID])
$node->field_news_categories->setValue($termID)
$node->field_news_categories->setValue(array($termID))
</code>
none worked so I thought maybe I should pass whole field with exact same structure, so I wanted to get field and just append the id and then pass it as argument in set method, but I wasn't able to get field either. All I was receving were null values or empty arrays.
I tried:
<code>$node->field_news_categories->getValue()
$node->get('field_news_categories')->getValue()
$node->field_news_categories->value
$node->get('field_news_categories')->value
$node->field_news_categories
$node->get('field_news_categories')
$node->get('field_news_categories')->target_id
$node->get('field_news_categories')->entity
$node->get('field_news_categories')->referencedEntities()
</code>
Project is in drupal 9
Comment: I'll ask the obvious - are you saving the node?
Answer: To append an entity reference item use:
<code>$node->field_foo[] = ['target_id' => 123];
</code>
And as Kevin noted in the comments, make sure you save the entity.
<code>$node->save();
</code>


Full outer join acting like inner join

Question: I expect the SQL below to return 3 records; but it returns only 2. I expect a row with the following details to be returned, but it's not:
I expect a row with Description Id = 21 to render, and in that row all the columns that begin with "part" should have NULL values.
The two records which are output are correct. The FULL OUTER JOIN seems to be acting like an inner join in terms of which rows it will return, but it properly returns a row where the description on a part and category are returned in one row (Description Ids 18 and 20).
A visual representation of my actual and expected results can be seen here:
https://ibb.co/Vq9GWLw
The first two rows of my actual and desired results are the same. My desired results also includes the record where the part's category has a description, but the part does not have its own description whose Code (9) matches the code of the description associated with the Category.
create table Temp.Part (
    Id int PRIMARY KEY CLUSTERED (Id asc),
    CategoryId int not null
)

create table Temp.Category (
    Id int PRIMARY KEY CLUSTERED (Id asc)
)

create table Temp.Description (
    Id int PRIMARY KEY CLUSTERED (Id asc),
    Code int not null,
    Value nvarchar(200) not null
)

create table Temp.DescriptionLink (
    Id int PRIMARY KEY CLUSTERED (Id asc),
    EntityId int not null,
    DescriptionId int not null
)
go

declare @partId int = 1
declare @categoryId int = 15

insert into Temp.Category values (@categoryId)
insert into Temp.Part values (@partId, @categoryId)

-- descriptions directly on part
insert into Temp.Description values (17, 6, 'This should output: desc #1 on Part')
insert into Temp.DescriptionLink values (50, @partId, 17)

insert into Temp.Description values (18, 4, 'This should output: desc #2 on Part (common)')
insert into Temp.DescriptionLink values (51, @partId, 18)

 -- descriptions on the part's category
insert into Temp.Description values (20, 4, 'This should output: desc #1 on Category (common)')
insert into Temp.DescriptionLink values (70, @categoryId, 20)

insert into Temp.Description values (21, 9, 'This should output: desc #2 on Category')
insert into Temp.DescriptionLink values (71, @categoryId, 21)

-- description not related to part (should not output)
insert into Temp.DescriptionLink values (72, -4, 99)
insert into Temp.Description values (99, 9, 'This should not be in the output because it belongs to a category not assigned to the part')

go

select  partDesc.DescriptionId 'Part Desc Id', catDesc.DescriptionId 'Category''s Description Id',
    partDesc.Value 'part''s desc', catDesc.Value 'Category''s Desc',
    case when partDesc.Code is not null then partDesc.Code else catDesc.Code end 'DescriptionCode'
from
    Temp.Category
    join Temp.Part on Part.CategoryId = Category.Id

    left join (
        select  L.entityId 'PartId', d.Code, d.Id 'DescriptionId', d.Value
        from
            Temp.DescriptionLink L
            join Temp.Description d on d.Id = L.DescriptionId
    ) partDesc on partDesc.PartId = Part.Id

     full outer join (
        select  l2.EntityId 'CategoryId', d2.Code, d2.Id 'DescriptionId', d2.Value
        from
            Temp.DescriptionLink L2
            join Temp.Description d2 on d2.Id = L2.DescriptionId
    ) catDesc on
        catDesc.Code = partDesc.Code
        and catDesc.CategoryId = Category.Id
where
    Part.Id = 1

So what I want is to list all Descriptions related to a part (either directory or indirectly via the Category). When the part and category have a description with the same code, they should display as one row (see the second row of my sample actual output: https://ibb.co/Vq9GWLw).
Comment: Provide a [mre] with emphasis on *minimal*, including sample data and desired results. Not many people are going to try and make sense of that monstrosity of a query - especially not without sample data to illustrate what is going on.
Comment: Put the `WHERE` conditions into the respective `ON` conditions.
Comment: @Charlieface I can't because the inner query can't refer to table Category.
Comment: Move `Part.Id = 1302860` to the `ON` clause of `Part`, and move `catDesc.CategoryId = Category.Id` to the `ON` of `catDesc`. Sample data for all tables, as well as expected results, would help us understand what you are actually trying to achieve.
Comment: @Charlieface If I move `catDesc.CategoryId = Category.Id` to the ON Clause it means it won't always be applied (FULL outer join after all) I need that condition to always be applied to the final results. I will create sample data when I've free time, just preparing for work.
Comment: WHERE Part.Id = 1302860
 AND ISNULL(catDesc.CategoryId, Category.Id) = ISNULL(Category.Id, catDesc.CategoryId);
Comment: @DaleK SQL added with my expectations of which record should render but does not. Does this help?
Comment: So what is your expected result? Can you show it in some kind of tablular format, and/or explain the steps involved, because I can't understand how the joins are supposed to work. Do you perhaps just want this result? https://dbfiddle.uk/_2jnzakv
Comment: @Charlieface the post now says my expectations. If you want a visual example, I can provide it, but I don't suppose I can add that in stack?
Comment: @Charlieface This image shows my desired output: https://ibb.co/Vq9GWLw   In the past when I've linked to 3rd party sites showing reference images people have complained. The first 2 rows from my desired output and actual output are the same. In the desired output note that it also has a record where the category has a description but the part does not have a Description with the same DescriptionCode.
Comment: That data structure is broken. There is nothing to prevent a description for part 1 also linking to category 1. Do you actually also have a column in the link table to specify what entity TYPE it relates to?
Comment: @MatBailie it's a small sample that's to the point, can you help with the query?
Comment: https://dbfiddle.uk/qyJezuGB
Answer: I think I've worked out what you actually want, after much to-ing and fro-ing.
You want to take the join of DescriptionLink Description Category, and Full-Join it to the join of DescriptionLink Description Part.
The join between Part and Category should separately filter those two sets.
So the full logic goes like this:

Take each Part row.
Cross-apply the following set to each row:

take the join of DescriptionLink Description where the DescriptionLink.EntityId refers to the outer Part.Id.
Take the join of DescriptionLink Description Category where that Category.Id is the same as the outer Part.CategoryId.
Full-join these two together, by Code.

Note the use of nested FULL JOIN ... JOIN ... ON ... ON ... clauses, in order to full-join the join result in each case. You can use () parentheses around the joins if you like, it does not make any difference.
SELECT
  d.*
FROM Temp.Part p
CROSS APPLY (
    SELECT
      pd.Id [Part Desc Id],
      cd.Id [Categorys Description Id],
      pd.Value [parts desc],
      cd.Value [Categorys Desc],
      ISNULL(pd.Code, cd.Code) DescriptionCode
    FROM (
        Temp.DescriptionLink pl
        JOIN Temp.Description pd
          ON pd.Id = pl.DescriptionId
         AND p.Id = pl.entityId   -- outer reference
    )
    FULL JOIN (
        Temp.DescriptionLink cl
        JOIN Temp.Description cd
          ON cd.Id = cl.DescriptionId
        JOIN Temp.Category c
            ON c.Id = cl.EntityId
           AND c.Id = p.CategoryId   -- outer reference
    ) ON cd.Code = pd.Code
) d
ORDER BY
  d.DescriptionCode;

db<>fiddle
Note that the DescriptionLink table has two major design flaws:

There is no discriminator column indicating which type of entity to join, so in theory there could be joins across both Part and Category.
It has a separate primary key. In a many-many join table, you should just have a composite primary key of both foreign keys (EntityId, DescriptionId).
Comment: So maybe like @MatBailie 's solution? Or without requerying `Part` you can use a `CROSS APPLY` https://dbfiddle.uk/quOQUjyf
Comment: The join between part and category is relevant because I only want to list Descriptions directly associated to the part, or to that part's category. Imagine instead of "Category" it was "Brand". I want to see all Descriptions for that part, and all Descriptions for the part's brand. So if the part is say "McNuggets", the brand might be McDonalds. The Description might be marketing info, so it might speak about the specific product and also about the brand in general.
Comment: I agree on the composite key, but that was not my choice.
Comment: That's not what you show in your screenshot. If that were the case then an inner-join on those columns would have worked. What you are showing is the results from the category descriptions and the part descriptions, joined *only* by `Code`. If you want something else, I suggest you *very clearly* describe what results you would like, taking into account all edge cases, and describe in words what the logic is.
Comment: Putting it another way, how can the third row have nulls in it if you are joining by `Part`? what possible joining logic could induce that?
Comment: @charlieface I'm not sure what you mean. The part might not have direct descriptions, but I still want to see the descriptions for that part's category (if there are any).
Comment: @Charlieface Do you see how the first record shows the part's description which has no matching category description for the same DescriptionCode? Well I want the opposite also, I want the part's category's Description even if the part doesn't provide its own Description with the same description code.
Comment: Because the NULLs just say that no DescriptionLink.EntityId points to that part where the DescriptionCode is the same as the Category's Description that is attached to the part. ie: I have a Description on a category where the Description code is 9, but there is no Description whose EntityId is the part where the Description code is 9.
Comment: Your fiddle output is what I expect, but I don't get the same results (I still get 2 rows) when I run your query against my DB. Did you change the data? At a glance it looked the same.
Comment: Oh. ya, I get two rows. I don't get the row where [Categorys Description Id] = 21 using your SQL at your posted fiddle link.
Comment: Can't help you with that. It works in the fiddle with your data https://dbfiddle.uk/quOQUjyf
Comment: is your "ON cd.Code = pd.Code" for the FULL join? I didn't realize you could join and not provide the ON clause for that join until after you've done other joins.
Comment: Yes that is exactly my point. It might be a bit clearer with brackets but I personally find it messy https://dbfiddle.uk/HGm00iwL

Full outer join acting like inner join


Real world sample of Financial accounting data models

Question: i am going to develop financial accounting software. But i am not experienced at accounting software data models.
if anyone has real world samples of data models of financial accounting software,
please share.
thanks.
Comment: You will want to ask a specific question to get a decent answer on this site.  As it stands, your post is far too vague, and has a good chance of being closed.
Answer: Martin Fowler's book "Analysis patterns" is more about object oriented design than databases, but you should read it anyway - he has sections on how to treat time, currency and financial transactions that you need to know about.
The "Data model resource book" by Silverston has a good section on modelling financial data.
Finally, if you want "real world" examples, you may have to grab some open source projects related to your particular requirements, and see how they store the data.
Answer: Each design starts with business requirements gathering and analysis, then follows the application model and then you start thinking bout the database.
Real life data models of financial accounting systems are way to big to share here.