Variadic compose function?

后端 未结 1 794
粉色の甜心
粉色の甜心 2020-12-09 18:46

I\'m trying to write a variadic function composition function. Which is basically the (.) except that the second argument function is variadic. This should allo

1条回答
  •  囚心锁ツ
    2020-12-09 19:11

    It is possible to type-hack it into working with polymorphic functions:

    {-# LANGUAGE FlexibleInstances, MultiParamTypeClasses,
      IncoherentInstances, UndecidableInstances,
      FunctionalDependencies, TypeFamilies,
      NoMonomorphismRestriction #-}
    
    
    class Comp a b c | a b -> c where
        (...) :: a -> b -> c
    
    instance (a ~ c, r ~ b) => Comp (a -> b) c r where
        f ... g = f g
    
    instance (Comp (a -> b) d r1, r ~ (c -> r1)) => Comp (a -> b) (c -> d) r where
        f ... g = \c -> f ... g c
    
    t1 = map even ... zipWith (+)
    t2 = map even ... zipWith
    t3 = (+1) ... foldr
    

    But I doubt you can avoid IncoherentInstances

    0 讨论(0)
提交回复
热议问题