如何改善此功能以测试下划线示例“ _.sample()” [英] How to improve this function to test Underscore Sample `_.sample()`

查看:65
本文介绍了如何改善此功能以测试下划线示例“ _.sample()”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次在某些客户的代码中使用 _。sample()并且我想对其进行测试以确保它一次又一次地产生均匀的样本分布。

I am using _.sample() for my first time in some client's code and I wanted to test it to make sure that it produced an even distribution of samples time after time.

为了对此进行测试,我创建了以下代码:

To test this, I created the following code:

(function(arraySize, timesToRun){
  arraySize = arraySize || 10;
  timesToRun = timesToRun || 1000;
  let myArray = Array.apply(null, {length: arraySize}).map(Number.call, Number);
  let resultsArray = Array.apply(null, Array(arraySize)).map(Number.prototype.valueOf,0);
  for (let i = 0; i < timesToRun; i++) {
    var result = _.sample(myArray);
    resultsArray[result]++;
  }
  return resultsArray;
})(10, 1000);

这在我的Chrome浏览器中完美运行,可得到以下结果:

This runs perfectly in my Chrome browser to give me the following result:

[93, 112, 97, 87, 97, 107, 97, 104, 105, 101]

如预期的那样,使用其他输入(20,10000),我们得到:

As expected, with other inputs (20, 10000) we get:

[646, 663, 641, 749, 648, 686, 642, 631, 663, 688, 691, 639, 672, 663, 678]

所以...就是这样,代码满足了我的需要,完全可以接受。

So... That's it, the code does what it needs for me, completely acceptable.

以下内容的解释:

如果答案能够回答这三个问题中的任何一个/全部,则该答案将被接受。

An answer will be accepted if it can answer any/all of these three questions.


  1. 我不明白 Array.apply(null,{length:arraySize})。map(Number.call,Number); 正在工作。具体来说,令我非常困惑的部分是 map 调用中发生的事情: map(Number.call,Number)-为什么这会产生一个看起来像 [0,1,2,3,4,5,6,7,8,9] 的数组?看到这个问题被分解成单独的问题,我问了这个具体问题其他地方

  2. 为什么在 map(Number.Number中需要 Number.prototype.valueOf prototype.valueOf,0)我将此问题提交给了新帖子已在此处回答

  3. 是否有更好的方法将默认值设置为 arraySize timesToRun (注意:我为了说服而在函数调用中添加了相同的值-我知道这不是必需的。)

  1. I do not understand how Array.apply(null, {length: arraySize}).map(Number.call, Number); is working. Specifically the part that is VERY confusing to me is what is happening in the map call: map(Number.call, Number) - Why does this produce an array that looks like [0,1,2,3,4,5,6,7,8,9]? Seeing as this question is getting sliced up into separate questions, I've asked this specific question elsewhere
  2. Why do we need Number.prototype.valueOf in map(Number.prototype.valueOf,0) I farmed this question out to a new post this has been answered here
  3. Is there a better way to set default values to arraySize and timesToRun? (Note: I'm putting the same value in the function call just for convince - I know this is not required.)

请对代码进行其他任何评论,我问这个问题是为了进一步了解Javascript,看看是否可以对该函数进行任何明显的改进。

Please make any other critiques to the code, I'm asking this question to learn more about Javascript and to see if there are any obvious improvements which could be made to this function.

也许任何人都可以发表评论,如果他们知道这样做的好方法:

Perhaps anyone could comment if they know a good way to do this:

我想找到一种简洁的干净方法来创建此测试的图形输出。有谁知道一个易于使用的库,以便我可以对此进行绘图。也许需要更多的输入,我们可以获得不错的钟形曲线? -这是一种简单的方法-我将其放在这行是因为同样,这个问题有

I'd like to find a neat clean way to create graphical output for this test. Does anyone know an easy library to use so that I can graph this. Maybe for more input we could get a nice bell curve? - Here is a simple way - I'm putting this here inline because again, this question has been downvoted, but I wanted to give the information for anyone else looking to do this.

此问题的格式不正确 。我将把这个问题分解成较小的问题,然后分别发布。我在阅读以下内容后进行了此修改: http://blog.stackoverflow.com/2010/ 09 / good-subjective-bad-subjective /

...嗯,我无法关闭此问题-因此,我将保留原始帖子下面。如果有人决定回答这个问题,我仍在寻找这些问题的答案,我会接受。

... Well, I can't close this question - so I'll leave the original post below. If anyone decides to answer this, I'm still looking for the answer to these questions and I will accept.

推荐答案

由于对于这个问题不是很直接的投诉,我创建了其他帖子来获得我想要的答案。

Due to the complaints that this question was not very direct I created other posts to get the answers I was looking for. This answers the main questions I had.


  1. 了解如何 array.prototype.call()作品解释了此模式的输出。可以在此处找到完整的详细答案:用顺序号映射Javascript中的数组

  2. 原因是由于 array.prototype.map()的工作原理-此处的完整答案:为什么我们使用<$ c $在 map()函数内的c> Number.prototype.valueOf

  3. 是,从ES6开始,还有一种方法可以为javascript中的参数设置默认值:

  1. Understanding how array.prototype.call() works explains the output of this pattern. A full detailed answer can be found here: Mapping Array in Javascript with sequential numbers
  2. The reason why is because of how array.prototype.map() works - full answer here: Why do we use Number.prototype.valueOf inside of a map() function
  3. Yes, as of ES6 there is an alternate way to set a default for an argument in javascript:

(function(arraySize = 10, timesToRun = 1000){
  let myArray = Array.apply(null, {length: arraySize}).map(Number.call, Number);
  let resultsArray = Array.apply(null, Array(arraySize)).map(Number.prototype.valueOf,0);
  for (let i = 0; i < timesToRun; i++) {
    var result = _.sample(myArray);
    resultsArray[result]++;
  }
  // Turned into console.log() for StackOverflow Snippet
  // return resultsArray;
  console.log(resultsArray);
})();

<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

然后如果有人感兴趣,这是一种图形化设置的简单方法。

And here is a simple way to set this up graphically if anyone is interested.

这篇关于如何改善此功能以测试下划线示例“ _.sample()”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆