zoukankan      html  css  js  c++  java
  • 杨辉三角形函数 JavaScript Generator 实现

    起源

    近年关,思乡情浓。于是看起了python,在廖雪峰的python教程中看到了python Generator感觉与 JavaScript 颇有神似,故译之。

    百度百科语言实现 唯独少了我大 JavaScript,这怎么能行呢?

    实现

    这种情况下,我是极力不想承认我的工作是翻译python

    • ES6 Generator 实现
    function *triangles() {
        var arr = [1], tempArr = [];
    
        while (true) {
            yield arr;
            arr.push(0)
    	// console.log(arr)
    	tempArr = Array.from({length: arr.length}).map((item, index) => index++)
    	// console.log(tempArr);
    
            arr = arr.map((t, i) => (arr[i-1] || 0) + arr[i])
    	// console.log(arr)
        }
    }
    
    
    • Test:
    const yhItrator = triangles()
    
    console.log(yhItrator.next().value)
    console.log(yhItrator.next().value)
    console.log(yhItrator.next().value)
    console.log(yhItrator.next().value)
    console.log(yhItrator.next().value)
    console.log(yhItrator.next().value)
    console.log(yhItrator.next().value)
    console.log(yhItrator.next().value)
    console.log(yhItrator.next().value)
    
  • 相关阅读:
    time fly
    小论文初稿终于完成
    leetcode之Length of Last Word
    static关键字
    参数传递
    this关键字
    面向对象有三大特征
    空指针异常
    变量按数据类型分为
    构造方法
  • 原文地址:https://www.cnblogs.com/givingwu/p/8393775.html
Copyright © 2011-2022 走看看