-
Notifications
You must be signed in to change notification settings - Fork 5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TypeScript 中带生成器的惰性管道 #6694
TypeScript 中带生成器的惰性管道 #6694
Conversation
TypeScript中带生成器的惰性管道
校对认领 |
@GJXAIOU 校对怎么样啦?有空记得来看看哈 |
@lsvih 校对认领 |
@xionglong58 妥妥哒 🍻 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@febrainqu @lsvih 只校对了部分内容,大致意思翻译到了,需要再做调整,后面再来校对,译者辛苦啦
|
||
![Photo by [Quinten de Graaf](https://unsplash.com/@quinten149?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)](https://cdn-images-1.medium.com/max/9704/1*wEQnHaPoHc_QJo5vxwrCEg.jpeg) | ||
|
||
In recent years the JavaScript community has embraced the functional array methods like `map` and `filter`. Writing for-loops has become something that gets associated with 2015 and JQuery. But the array methods in JavaScript are far from ideal when we are talking about performance. Lets look at an example to clarify the issues: | ||
近年来,JavaScript 社区已经接受了函数数组方法,例如 `map` 和 `filter`。 编写 for 循环已经成为与2015年和 JQuery 相关的内容。但是在性能方面,JavaScript 中的数组方法远远达不到预期。让我们看一个例子来说明这些问题: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
functional =>函数式
|
||
![Photo by [Quinten de Graaf](https://unsplash.com/@quinten149?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)](https://cdn-images-1.medium.com/max/9704/1*wEQnHaPoHc_QJo5vxwrCEg.jpeg) | ||
|
||
In recent years the JavaScript community has embraced the functional array methods like `map` and `filter`. Writing for-loops has become something that gets associated with 2015 and JQuery. But the array methods in JavaScript are far from ideal when we are talking about performance. Lets look at an example to clarify the issues: | ||
近年来,JavaScript 社区已经接受了函数数组方法,例如 `map` 和 `filter`。 编写 for 循环已经成为与2015年和 JQuery 相关的内容。但是在性能方面,JavaScript 中的数组方法远远达不到预期。让我们看一个例子来说明这些问题: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[编写 for 循环已经成为与2015年和 JQuery 相关的内容]=>for 循环已经成为过去式,或者只能在Jquery中见到。
* create a new array with the numbers filtered | ||
* take the first item | ||
* 创建一个含有五个元素的数组 | ||
* 创建是个每个数字乘2的新数组 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
创建一个新数组,元素时前一个数组对应元素的2倍。
|
||
This involves a lot more stuff happening then is actually needed. The only thing has to happen is that the first item that passes `x > 5` gets processed and returned. In other languages (like Python) iterators are used to solve this issue. Those iterators are a lazy collection and only processes data when it is requested. If JavaScript would use lazy iterators for its array methods the following would happen instead: | ||
这涉及到比实际需要的更多的事情. 这里唯一必要的是处理并返回通过 `x > 5` 的第一项。 在其他语言中 (例如 Python),用迭代器来解决此类问题。 这些迭代器是一个惰性集合,只在请求时处理数据。 如果JavaScript将懒惰的迭代器用于其数组方法,则会发生以下情况: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
翻译过于生硬了
[这涉及到比实际需要的更多的事情. 这里唯一必要的是处理并返回通过 x > 5
的第一项。]=>[实际上有很步骤是多余的,唯一必要的就是返回第一个大于 5 的元素。]
|
||
This involves a lot more stuff happening then is actually needed. The only thing has to happen is that the first item that passes `x > 5` gets processed and returned. In other languages (like Python) iterators are used to solve this issue. Those iterators are a lazy collection and only processes data when it is requested. If JavaScript would use lazy iterators for its array methods the following would happen instead: | ||
这涉及到比实际需要的更多的事情. 这里唯一必要的是处理并返回通过 `x > 5` 的第一项。 在其他语言中 (例如 Python),用迭代器来解决此类问题。 这些迭代器是一个惰性集合,只在请求时处理数据。 如果JavaScript将懒惰的迭代器用于其数组方法,则会发生以下情况: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[JavaScript将懒惰]=>[JavaScript 将懒惰]
* `[0]` requests the first item from `filter` | ||
* `filter` requests items from `map` until it has found one item that passes the predicate and yields (‘returns’) it | ||
* `map` has processed an item for each time `filter` requested it | ||
* `[0]` 从 `filter` 请求第一项 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[[0]
从 filter
请求第一项]=>'[0]'请求经 ‘filter’ 操作后数组的第一个元素。
* `map` has processed an item for each time `filter` requested it | ||
* `[0]` 从 `filter` 请求第一项 | ||
* `filter` 从 `map` 中请求元素,直到发现一个符合条件的元素,并返回 (‘returns’) 它 | ||
* `map` 已经处理了每一次 `filter` 请求 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[map
已经处理了每一次 filter
请求]=>每当 ‘filter’ 发送一次请求,‘map’ 便处理一个元素。
|
||
Luckily for us JavaScript does actually support the concept of [iterators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators). They can be created with [generator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators) functions that yield the items of the collection. A generator function looks as follows: | ||
幸运的是JavaScript确实支持[迭代器](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators)的概念. 可以使用生成集合项的[生成器](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators) 函数来创建它们。一个生成器函数如下: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[幸运的是JavaScript确实支持]=>[幸运的是 JavaScript 确实支持]
|
||
The last part of the library is the public facing API. The library uses the builder pattern to allow you to chain methods like on arrays. This is done by creating a function that takes an iterator and returns an object with the methods on it. Those methods can call the constructor again with an updated iterator for the chaining: | ||
库的最后一部分是面向公众的API。 该库使用构建器模式来允许您像在数组上那样链接方法。 这是通过创建一个接受迭代器并返回带有方法的对象的函数来完成的。 这些方法可以再次调用构造函数与更新迭代器的链接: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[库的最后一部分是面向公众的API] =>[库的最后一部分是面向公众的 API]
@febrainqu @Glowin 大致校对完成,文中有不少的英文 |
@GJXAIOU @xionglong58 感谢校对,晚些时候会修改和自查的 |
@GJXAIOU @xionglong58 已更新,麻烦两位看一下还有什么问题 |
@@ -5,11 +5,11 @@ | |||
> * 译者: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
麻烦加一下译者和校对者的信息哈
@lsvih 已添加 |
还是有很多符号问题,比如许多 |
格式没啥问题了👍稍晚点我试着把部分语句顺一顺 |
@febrainqu 抱歉拖了这么久,之前都忘了这事了。。 这是您翻译的第一篇文章,还是有很多提升空间的,因此我直接在您的原文基础上进行了修改,现在应该更加通顺且容易理解了,您可以参考一下。当然您之后翻译的文章质量都挺高的! |
@febrainqu 已经 merge 啦~ 快快麻溜发布到掘金然后给我发下链接,方便及时添加积分哟。 掘金翻译计划有自己的知乎专栏,你也可以投稿哈,推荐使用一个好用的插件。 |
译文翻译完成,resolve #6675