8 karma · joined Jan 25, 2017 (9y ago) · 4 submissions
comment 8y ago · link · parent
import Control.Applicative
newtype ZList a = ZList { runZList :: [a] }
instance Functor ZList where
fmap f = ZList . fmap f . runZList
instance Applicative ZList where
pure a = ZList [a]
(<*>) f x = ZList $ zipper (runZList f) (runZList x)
where zipper (f:fs) (x:xs) = f x : zipper fs xs
zipper [] _ = []
zipper _ [] = []
Difference of behaviour: *Main> ZipList [(*3),(*5),(*6)] <*> ZipList [1,2,3,4]
ZipList {getZipList = [3,10,18]}
*Main> [(*3),(*5),(*6)] <*> [1,2,3,4]
[3,6,9,12,5,10,15,20,6,12,18,24]
*Main>comment 9y ago · link · parent
I would expect that ranges is on the end, but it is somewhere in the middle.
[item for item in subrange for subrange in ranges]
is clearer to me. Then I can scan from left to right. It would read like a pipeline.Now I need to start in the middle (ranges), scan to the left (for subrange), then to to the end (for item in subrange) and then back to the beginning (item). Or something like that, it is hard to follow your own eye movements. :)
Note that I seldom use python and the * pattern is something I recognize from another language, so I am biased. I imagine a seasoned python dev has no problems with the second case.
Btw, does python let you overload those comprehensions? That would be nice.
comment 9y ago · link · parent
TCE can also make debugging easier, how useful is a stack trace of 1000 lines consisting of
...
File "bla.py", line 4, in fib
return fib(n - 1) + fib(n - 2)
File "bla.py", line 4, in fib
return fib(n - 1) + fib(n - 2)
File "bla.py", line 4, in fib
return fib(n - 1) + fib(n - 2)
...Not so much I think.