将多个谓词功能组合为一个

将多个谓词功能组合为一个

本文介绍了将多个谓词功能组合为一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以编写例如:

(defn- multiple-of-three? [n] (zero? (mod n 3))
(defn- multiple-of-five? [n] (zero? (mod n 5))

变为:

multiple-of-three-or-five?

所以我可以将其用于过滤:

so I can use it for filtering:

(defn sum-of-multiples [n]
  (->> (range 1 n)
       (filter multiple-of-three-or-five?)
       (reduce +)))

我也不想这样定义:

(defn- multiple-of-three-or-five? [n]
  (or (multiple-of-three? n)
      (multiple-of-five? n)))

例如,使用Javascript模块Ramda可以实现为:

For example with Javascript module Ramda it would be achieved as: http://ramdajs.com/docs/#either

const multipleOfThreeOrFive = R.either(multipleOfThree, multipleOfFive)


推荐答案

可以,在Clojure中为。

Sure, in Clojure this is some-fn.

(def multiple-of-three-or-five?
  (some-fn multiple-of-three? multiple-of-five?))





(multiple-of-three-or-five? 3)  ; => true
(multiple-of-three-or-five? 4)  ; => false
(multiple-of-three-or-five? 5)  ; => true

这篇关于将多个谓词功能组合为一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-03 17:08