Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

42 Commits

Repository files navigation

npm versionBuild Status

Pythonic

Python like utility functions for JavaScript: range, enumerate, items, zip and zipLongest.

These functions return an Iterator instance similar to Python Iterators. This Iterator implementation is lazy evaluated, offers map, filter, reduce, some, every and Symbol.asyncIterator interfaces.

Install

npm install pythonic --save

Functions

range

import{range}from'pythonic';range(3).map(x=>console.log(x+1));// 1// 2// 3for(constiofrange(10,25,5))console.log(i);// 10// 15// 20console.log(range(5).reduce((accumulator,current)=>accumulator+current));// 10

enumerate

import{enumerate}from'pythonic';constarr=['a','b'];for(const[index,value]ofenumerate(arr))console.log(`index: ${index}, value: ${value}`);// index: 0, value: a// index: 1, value: b

zip | zipLongest

import{zip,zipLongest}from'pythonic';constarr1=['a','b'];constarr2=['c','d','e'];for(const[first,second]ofzip(arr1,arr2))console.log(`first: ${first}, second: ${second}`);// first: a, second: c// first: b, second: dfor(const[first,second]ofzipLongest(arr1,arr2))console.log(`first: ${first}, second: ${second}`);// first: a, second: c// first: b, second: d// first: undefined, second: e// unzipconst[arrayFirst,arraySecond]=[...zip(...zip(arr1,arr2))];

items

import{items}from'pythonic';constobj={a: 'aa',b: 'bb'};for(const[key,value]ofitems(obj))console.log(`key: ${key}, value: ${value}`);// key: a, value: aa// key: b, value: bbconstmap=newMap([[1,'one'],[2,'two']]);for(const[key,value]ofitems(map))console.log(`key: ${key}, value: ${value}`);// key: 1, value: one// key: 2, value: two

Iterator

import{Iterator,range}from'pythonic';constrandomIntegers=(size,start,stop)=>newIterator(function*(){for(const_ofrange(size))yieldMath.floor(Math.random()*(stop-start)+start);});constrandomNumbers=randomIntegers(3,10,1000);for(constrandomNumberofrandomNumbers)console.log(randomNumber);// 685// 214// 202console.log(randomNumbers.some(value=>value>10));// trueconsole.log(randomNumbers.every(value=>value<1000));// true

License

MIT

About

Python like utility functions for JavaScript: range, enumerate, zip and items.

Topics

Resources

Stars

30 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages