通过分区到另一个功能在题

0

的问题

我只是开始质量的技术资源,但我似乎不能找出使用/建立更高阶的职能。

我有分配一个收集和我想要通过进入另一个功能,将做些什么来的窗口项目。 我不知道该如何去这样做。

(def foo [:a :b :c :d :e])

(partition 3 1 foo)
;;=> ((:a :b :c) (:b :c :d) (:c :d :e))

(defn bar [start next end])

我认为基本轮廓。

(defn faz [collect]
    (partition 3 1 collect)
    ;;maybe do here before passing
    (bar stand next end)
)

我可能会得到之前的自己,但我也看到,还有其他功能,如减轻和适用,他们可以做类似的东西吗? 虽然,大多数例子,我看到它所以他们执行操作上的两个项目在一段时间这是类似于 (partition 2 1 foo)

clojure higher-order-functions
2021-11-20 10:49:57
2

最好的答案

1

你可以做的东西喜欢

(defn bar [start next end])


(defn faz [collect]
  (let [partitions (partition 3 1 collect)]
    (apply bar partitions)
    ))

或如果你想叫 bar 直接,可以用解构

(defn bar [start next end])

(defn faz [collect]
  (let [partitions (partition 3 1 collect)
        [start next end] partitions]
    (bar start next end)
    ))
2021-11-20 11:08:30
0

你的问题是一般性和有更多的方式来实现这一,基于预期成果和使用的功能。

如果你想要返回序的结果,使用 mapapply:

(defn results-for-triplets [collect]
  (map #(apply + %) (partition 3 1 collect)))

(results-for-triplets [1 2 3 4 5])
=> (6 9 12)

为了便于阅读,您可以使用 ->> 宏。

(defn results-for-triplets [collect]
  (->> collect
       (partition 3 1)
       (map #(apply + %))))

(results-for-triplets [1 2 3 4 5])
=> (6 9 12)

你可以避免 apply如果你能 destructures 通过顺序:

(defn sum3 [[a b c]]
  (+ a b c))

(defn results-for-triplets [collect]
  (->> collect
       (partition 3 1)
       (map sum3)))

(results-for-triplets [1 2 3 4 5])
=> (6 9 12)

如果你想要呼叫功能的副作用,然后返回 nil使用 run!:

(defn print3 [[a b c]]
  (println a b c))

(defn results-for-triplets [collect]
  (->> collect
       (partition 3 1)
       (run! print3)))

(results-for-triplets [1 2 3 4 5])
1 2 3
2 3 4
3 4 5
=> nil
2021-11-20 11:51:18

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................