def tointervals(tlist):
count=0
for i in tlist:
if count == 0:
count = count + 1
yield 0
else:
count = count + 1
yield i - tlist[count -2]
I test it like so:
In [272]: list(tointervals([1,2,3,4,10]))
Out[272]: [0, 1, 1, 1, 6]
Great, that’s exactly what I was looking for
But I want to do it in a more functional way (I’m mostly using Scala)
I’m terminally confused about this, mainly due to unfamiliarity with Python. The closest I’ve gotten before giving up in terminal confusion is this.
In [292]: reduce(lambda x,y: (x[0] + [y - x[0][x[1]] ],x[1]+1), [1,2,3,4,10], ([0],0))
Out[292]: ([0, 1, 1, 2, 2, 8], 5)
Which is obviously wrong, but that’s the direction I was trying… Am I being foolish, trying to use reduce, or can I just not code for toffee? Be gentle, please.
A list comprehension will be more pythonic: