zoukankan      html  css  js  c++  java
  • [Ramda] Create an Array From a Seed Value with Ramda's unfold

    In this lesson we'll look at how you can use Ramda's unfold function to generate a list of values based on an initial seed.
    const R = require('ramda');
    
    // if return false, then stop iteration
    // [n1, n2]: n1 is the value to be added to the result array
    // n2: is the value return to next iteration as a starting value
    const throughNByOne = R.curry((limit, n) => n > limit ? false: [n, n + 1]);
    const throughNByBaseTwo = R.curry((limit, n) => n > limit ? false: [n, n * 2]);
    
    const res1 = R.unfold(throughNByOne(23), 3); // [ 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 ]
    console.log(res1);
    
    const res2 = R.unfold(throughNByBaseTwo(256), 2); // [ 2, 4, 8, 16, 32, 64, 128, 256 ]
    console.log(res2);
  • 相关阅读:
    02
    01
    Redis、Mongo
    Django
    Django
    Django
    Django
    7.2
    Django
    contenttypes
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6479810.html
Copyright © 2011-2022 走看看