Zip with default value instead of dropping values?

后端 未结 9 809
夕颜
夕颜 2020-12-01 18:30

I\'m looking for a function in haskell to zip two lists that may vary in length.
All zip functions I could find just drop all values of a lists that is longer than the o

9条回答
  •  离开以前
    2020-12-01 19:03

    This can be expressed using These ("represents values with two non-exclusive possibilities") and Align ("functors supporting a zip operation that takes the union of non-uniform shapes") from the these library:

    import Data.Align
    import Data.These
    
    zipWithDefault :: Align f => a -> b -> f a -> f b -> f (a, b)
    zipWithDefault da db = alignWith (fromThese da db)
    

    salign and the other specialised aligns in Data.Align are also worth having a look at.

    Thanks to u/WarDaft, u/gallais and u/sjakobi over at r/haskell for pointing out this answer should exist here.

提交回复
热议问题