This simple piece of code works perfectly fine. What I'm about to ask is completely unnecessary; however, I'm trying to learn more about the functional programming approach to doing things.
p=[0, 1, 0, 0, 0]
pHit = 0.6
pMiss = 0.2
pExact = 0.8
pOvershoot = 0.1
pUndershoot = 0.1
def move(p, U):
q = 
# A functional approach could be used here as well, but focusing on the outer loop at the moment.
for i in range(len(p)):
s = pExact * p[(i-U) % len(p)]
s = s + pOvershoot * p[(i-U-1) % len(p)]
s = s + pUndershoot * p[(i-U+1) % len(p)]
#Instead of this for loop here, is there a more functional approach to doing the same thing?
for i in range(0,1000):
p = move(p,1)
To replace that loop at the bottom, you could do something like:
reduce(lambda q,_: move(q, 1), range(1000), p)
Notice how the values of the range are never even used, so they're indicated as being irrelevant using a
The reduction automatically passes the result of
move to the next iteration.