Created
January 7, 2020 19:48
-
-
Save jgomo3/0fd89173fa4c2799edc9b9aa14d151cf to your computer and use it in GitHub Desktop.
Kind of range, but for anything that supports adding, or can be combined by a given optional function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Defines the function serie, which... | |
From an starting point generates all the values following | |
by the given period. | |
It works with numbers, anything that support adding or | |
anything that support the optional binary opperation | |
provided. | |
>>> ns = serie(10, 4) | |
>>> next(ns); next(ns); next(ns) | |
10 | |
14 | |
18 | |
>>> from datetime import datetime, timedelta | |
>>> ns = serie(datetime(2020, 1, 1), timedelta(days=4)) | |
>>> next(ns); next(ns); next(ns) | |
datetime.datetime(2020, 1, 1, 0, 0) | |
datetime.datetime(2020, 1, 5, 0, 0) | |
datetime.datetime(2020, 1, 9, 0, 0) | |
''' | |
from itertools import accumulate, chain, repeat | |
def serie(start, period, func=None): | |
return accumulate(chain([start], repeat(period)), func=func) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment