- 前言
- Lazy
- createWrapper
- generate
- range
- repeat
- strict
- Sequence
- define
- async
- chunk
- compact
- concat
- consecutive
- contains
- countBy
- dropWhile
- each
- equals
- every
- filter
- find
- findWhere
- first
- flatten
- get
- getIterator
- groupBy
- indexBy
- indexOf
- initial
- intersection
- invoke
- isEmpty
- join
- last
- lastIndexOf
- map
- max
- memoize
- min
- none
- ofType
- pluck
- reduce
- reduceRight
- reject
- rest
- reverse
- shuffle
- size
- some
- sort
- sortBy
- sortedIndex
- sum
- takeWhile
- tap
- toArray
- toObject
- union
- uniq
- where
- without
- zip
- ArrayLikeSequence
- define
- concat
- first
- get
- length
- map
- pop
- push
- rest
- reverse
- shift
- slice
- unshift
- ObjectLikeSequence
- define
- assign
- async
- defaults
- functions
- get
- invert
- keys
- merge
- omit
- pairs
- pick
- toArray
- toObject
- values
- watch
- StringLikeSequence
- define
- charAt
- charCodeAt
- contains
- endsWith
- first
- indexOf
- last
- lastIndexOf
- mapString
- match
- reverse
- split
- startsWith
- substring
- toLowerCase
- toUpperCase
- GeneratedSequence
- each
- length
- AsyncSequence
- contains
- each
- find
- getIterator
- indexOf
- Iterator
- current
- moveNext
- AsyncHandle
- cancel
- onComplete
- onError
Sequence
The Sequence
object provides a unified API encapsulating the notion of zero or more consecutive elements in a collection, stream, etc.
Lazy evaluation
Generally speaking, creating a sequence should not be an expensive operation, and should not iterate over an underlying source or trigger any side effects. This means that chaining together methods that return sequences incurs only the cost of creating the Sequence
objects themselves and not the cost of iterating an underlying data source multiple times.
The following code, for example, creates 4 sequences and does nothing with source
:
var seq = Lazy(source) // 1st sequence
.map(func) // 2nd
.filter(pred) // 3rd
.reverse(); // 4th
Lazy's convention is to hold off on iterating or otherwise doing anything (aside from creating Sequence
objects) until you call each
:
seq.each(function(x) { console.log(x); });
Defining custom sequences
Defining your own type of sequence is relatively simple:
- Pass a method name and an object containing function overrides to Sequence.define. If the object includes a function called
init
, this function will be called upon initialization. - The object should include at least either a
getIterator
method or aneach
method. The former supports both asynchronous and synchronous iteration, but is slightly more cumbersome to implement. The latter supports synchronous iteration and can be automatically implemented in terms of the former. You can also implement both if you want, e.g. to optimize performance. For more info, see Iterator and AsyncSequence.
As a trivial example, the following code defines a new method, sample
, which randomly may or may not include each element from its parent.
Lazy.Sequence.define("sample", {
each: function(fn) {
return this.parent.each(function(e) {
// 50/50 chance of including this element.
if (Math.random() > 0.5) {
return fn(e);
}
});
}
});
(Of course, the above could also easily have been implemented using #filter instead of creating a custom sequence. But I did say this was a trivial example, to be fair.)
Now it will be possible to create this type of sequence from any parent sequence by calling the method name you specified. In other words, you can now do this:
Lazy(arr).sample();
Lazy(arr).map(func).sample();
Lazy(arr).map(func).filter(pred).sample();
Etc., etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论