如何组合一起返回Bool的函数

有没有办法将这些功能组合在一起?

data Patient = Patient
  { name :: Name,
    sex :: Sex,
    age :: Int,
    height :: Int,
    weight :: Int,
    bloodType :: BloodType
  }

canDonate :: BloodType -> BloodType -> Bool

canDonateTo :: Patient -> Patient -> Bool

目前我只是手动应用它们

canDonateTo :: Patient -> Patient -> Bool
canDonateTo x y = canDonate (bloodType x) (bloodType y)

但是,我想知道是否有更好的方法来做到这一点。

回答

使用Data.Function.on

import Data.Function (on)

canDonateTo = canDonate `on` bloodType

(基本上,您的方法只是内联了 的定义on,可以定义为

on f g x y = f (g x) (g y)

)


以上是如何组合一起返回Bool的函数的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>