Even in JavaScript, I want to see Python `range ()`!

Introduction

for i in range(10):
    print(i)
    #Cool

The theory is good, so show it soon!

/**
 * @param  {...number} args
 */
const range = (...args) => {
  const rangeGen = function* (from = 0, to = Infinity, step = 1) {
    for (let v = from; v < to; v += step) {
      yield v;
    }
  };

  return args.length === 0
    ? rangeGen(undefined, undefined, undefined)
    : args.length === 1
    ? rangeGen(undefined, args[0], undefined)
    : args.length === 2
    ? rangeGen(args[0], args[1], undefined)
    : rangeGen(...args);
};

for (const v of range(2, 10)) {
  console.log(v);
  // 2 ~Up to 9 are output in order
}

what are you doing?

The range function returns the result of executing the generator function. It is a wrapper that just distributes the arguments to the generator function. rangeGen is the generator function.

――What is a generator? --A relatively new feature of JavaScript added in ES2015 ――What are you generating? --Generate repeatable objects ――What is repetition?

For details on how to use the generator function, please read MDN.

For the time being, I am making a guy who can enumerate the numbers from from to to.

in conclusion

Now you can also for (const i of range (10)) { in JavaScript.

bonus

Personally, Python's sledding is troublesome because you have to pay attention to the order of the arguments. So below (TypeScript).

const range = function* ({
  start = 0,
  stop = Infinity,
  step = 1,
}: {
  start?: number;
  stop: number;
  step?: number;
}) {
  while (start < stop) {
    yield start;
    start += step;
  }
};

for (const v of range({ stop: 10 })) {
  console.log(v);
}

that's all.

Recommended Posts

Even in JavaScript, I want to see Python `range ()`!
I want to do Dunnett's test in Python
I want to create a window in Python
I want to merge nested dicts in Python
I want to display the progress in Python!
I want to write in Python! (1) Code format check
I want to embed a variable in a Python string
I want to easily implement a timeout in python
I want to write in Python! (2) Let's write a test
I want to randomly sample a file in Python
I want to work with a robot in python.
I want to write in Python! (3) Utilize the mock
I want to use the R dataset in python
I want to do something in Python when I finish
I want to manipulate strings in Kotlin like Python!
I want to debug with Python
I want to do something like sort uniq in Python
I want to be able to run Python in VS Code
I want to make input () a nice complement in python
I tried to implement PLSA in Python
I tried to implement permutation in Python
I want to solve APG4b with Python (only 4.01 and 4.04 in Chapter 4)
I tried to implement PLSA in Python 2
I want to run Rails with rails s even in vagrant environment
I want to use jar from python
I want to build a Python environment
I want to analyze logs with Python
I want to play with aws with python
I want to improve efficiency with Python even in an experimental system (2) RS232C and pySerial
[Python / AWS Lambda layers] I want to reuse only module in AWS Lambda Layers
I tried to implement ADALINE in Python
I wanted to solve ABC159 in Python
I tried to implement PPO in Python
I want to improve efficiency with Python even in an experimental system (1) Install Anaconda with Chocolatey
I want to embed Matplotlib in PySimpleGUI
[C language] I want to generate random numbers in the specified range
I want to convert a table converted to PDF in Python back to CSV
I want to batch convert the result of "string" .split () in Python
I want to explain the abstract class (ABCmeta) of Python in detail.
Environment maintenance made with Docker (I want to post-process GrADS in Python
I want to color a part of an Excel string in Python
I want to do a monkey patch only partially safely in Python
I want to use MATLAB feval with python
To execute a Python enumerate function in JavaScript
I want to pin Datetime.now in Django tests
I want to memoize including Python keyword arguments
I want to email from Gmail using Python.
[Python] I want to manage 7DaysToDie from Discord! 1/3
I want to mock datetime.datetime.now () even with pytest!
I want to make a game with Python
I wrote "Introduction to Effect Verification" in Python
I want to store DB information in list
I tried to implement TOPIC MODEL in Python
I want to use Temporary Directory with Python2
I want to use ceres solver from python
#Unresolved I want to compile gobject-introspection with Python3
I want to solve APG4b with Python (Chapter 2)
I want to sell Mercari by scraping python
[Python] I want to manage 7DaysToDie from Discord! 2/3
I want to make C ++ code from Python code!
I tried to implement selection sort in python