통과 파티션을 또 다른 기능에 clojure

0

질문

나는 그냥 시작 clojure 할 수 있습을 그림을 사용하여/을 만드는 높은 함수입니다.

나는 분할된 컬렉션을 전하고 싶은 메시지는 것으로 다른 기능을 할 것입니다 무언가를 창의 항목입니다. 나는 확실하지 않는 방법에 대해 이동하는 것입니다.

(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)

하려는 경우 호출 기능에 대한 부작용과낸 다음 nilrun!:

(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ý
..................................................................................................................