Skip to content

Instantly share code, notes, and snippets.

@jgomo3
Created January 7, 2020 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgomo3/0fd89173fa4c2799edc9b9aa14d151cf to your computer and use it in GitHub Desktop.
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
'''
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