How to abstract over a “back and forth” transformation?

前端 未结 5 771
栀梦
栀梦 2021-02-19 04:53

Consider this example (from https://codereview.stackexchange.com/questions/23456/crtitique-my-haskell-function-capitalize):

import Data.Char

capWord [] = []
cap         


        
5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-19 05:17

    Your lifted is actually the same as dimap from Data.Profunctor:

    onWords = dimap words unwords
    capitalize = onWords (map capWord)
    

    That might not be the direction of generalization you thought about. But look at the type of the equivalent function in Control.Functor from category-extras:

    dimap :: Bifunctor f (Dual k) k k => k b a -> k c d -> k (f a c) (f b d)
    

    This version generalizes it over everything which is both a QFunctor and a co-PFunctor. Not that useful in everyday scenarios, but interesting.

提交回复
热议问题