The composition of functions in a list of functions!

后端 未结 5 2042
灰色年华
灰色年华 2020-12-31 20:47

I need to define a function \'Compose\' which takes a list \'L\' which is a list of functions. When I specify a parameter that will suit all the functions in the list, the l

5条回答
  •  醉话见心
    2020-12-31 21:27

    The same using monoids, point-free

    import Data.Monoid
    compose :: [a -> a] -> a -> a
    compose = appEndo . mconcat . map Endo
    

    Or somewhat more generally:

    import Data.Monoid
    compose :: (Functor t, Foldable t) => t (a -> a) -> a -> a
    compose = appEndo . foldl1 (<>) . fmap Endo
    

提交回复
热议问题