Comments (13)
@dayuoba 啥意思?
from underscore-analysis.
@dayuoba
同求,没有明白什么意思。。
我能想到的是,在循环过程中,随着index的增大,~~(Math.random() * (index + 1))
能够选到首元素的概率会降低。但其实因为被random基数的增加,之前的每个元素被随机选到的可能性是一样的(不考虑Math.random伪随机的情况下)
for (var index = 0, rand; index < length; index++) {
rand = ~~(Math.random() * (index + 1));
// .....
}
from underscore-analysis.
@ecmadao 不知道你说的没明白是没明白哪块?
from underscore-analysis.
@hanzichi
就是 @dayuoba 所说的Fisher–Yates Shuffle 算法,首元素被洗掉的概率会越来越低
from underscore-analysis.
@ecmadao 首元素被洗掉的概率确实越来越低了,但是首元素一直在变的呀,而且我们关心的是每个元素随机到每个位置的概率是否一致。
from underscore-analysis.
@hanzichi
嗯那其实我们理解的一致,每个元素分配到的概率是总相同的。其实之前我都不知道有Fisher–Yates Shuffle,THX
from underscore-analysis.
请教一下,_.intersection这个求数组交集的方法,为什么if (j === argsLength)的时候,就可以result.push(item);,这个判断条件没想明白
from underscore-analysis.
@zhoucumt
遍历其他数组,如果其他数组中不存在 item 这个元素,那么就会 break 掉,这时 j 肯定小于 argsLength;而如果每个数组都存在 item 元素,则 for 循环可以执行到最后,for 循环结束时 j === argsLength,所以 j === argsLength 满足则说明每个数组都存在 item 元素。
其实我觉得源码注释说的好像蛮清楚了 ...
// Produce an array that contains every item shared between all the
// passed-in arrays.
// 寻找几个数组**有的元素
// 将这些每个数组中都有的元素存入另一个数组中返回
// _.intersection(*arrays)
// _.intersection([1, 2, 3, 1], [101, 2, 1, 10, 1], [2, 1, 1])
// => [1, 2]
// 注意:返回的结果数组是去重的
_.intersection = function(array) {
// 结果数组
var result = [];
// 传入的参数(数组)个数
var argsLength = arguments.length;
// 遍历第一个数组的元素
for (var i = 0, length = getLength(array); i < length; i++) {
var item = array[i];
// 如果 result[] 中已经有 item 元素了,continue
// 即 array 中出现了相同的元素
// 返回的 result[] 其实是个 "集合"(是去重的)
if (_.contains(result, item)) continue;
// 判断其他参数数组中是否都有 item 这个元素
for (var j = 1; j < argsLength; j++) {
if (!_.contains(arguments[j], item)) break;
}
// 遍历其他参数数组完毕
// j === argsLength 说明其他参数数组中都有 item 元素
// 则将其放入 result[] 中
if (j === argsLength) result.push(item);
}
return result;
};
另外,如果跟本主题无关的问题,希望能开个新的 issue ... 第一反应以为是关于数组乱序的问题 ... thanks
from underscore-analysis.
感觉 GitHub 怎么自动把 <>
取消掉了 ... 囧
from underscore-analysis.
请问 _.sample 里面有个参数n 代表 返回n个元素 源码中return _.shuffle(obj).slice(0, Math.max(0,n))
这里为什么要用Math.max(0,n)
? underscore中有好多地方都用了Math.max(0,n)
,为什么不能直接写n
?
from underscore-analysis.
如果没记错的话,不是Math.random影像了结果,而是sort的实现,在20以内是插入排序,大于20是快排,而且不同浏览器的实现也不一样,ff好像是合并排序解决长数组的,恩,maybe吧
from underscore-analysis.
Fisher–Yates Shuffle 这个算法从尾部开始,将index 的内容和index 之前的数组中随机的一个交换,index 一直往前移动,但是到快要终止的时候,只能是 index =1 的索引和 index =0的索引交换,这样的新的数组,首部的数字相对固定。 在一些场景下,有一定缺陷。
第一种的方式则不会存在这个问题。
from underscore-analysis.
从循环开始时起,首部数字即有被后面数字交换的概率,并不是当循环往前移动到相应位置时才能发生交换。所以不存在首部相对固定的问题。
from underscore-analysis.
Related Issues (20)
- 浅谈 underscore 内部方法 group 的设计原理 HOT 3
- 带注释的源码中var _ = function (obj) {....}中有个疑问 HOT 4
- 关于 bind 你可能需要了解的知识点以及使用场景 HOT 2
- bind 方法的兼容实现 HOT 1
- JavaScript 函数节流和函数去抖应用场景辨析 HOT 17
- underscore 函数去抖的实现 HOT 20
- underscore 函数节流的实现 HOT 8
- 从斐波那契数列求值优化谈 _.memoize 方法 HOT 6
- Function Functions 相关源码拾遗
- 浅谈 Web 中前后端模板引擎的使用 HOT 19
- Underscore _.template 方法使用详解 HOT 3
- Underscore 整体架构浅析 HOT 15
- 源码解读这半年 HOT 20
- 疑问:模块导出中,向后兼容老的require() API HOT 1
- 源码
- 打扰一下,有一个问题想咨询你一下 HOT 9
- 你好请教个问题 HOT 1
- 你好...在谷歌浏览器中运行`Object.prototype.toString().call(abb)`会报错 //var abb = true HOT 2
- 關於underscore源碼中提到了25個扩展方法之疑問? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from underscore-analysis.