我试图理解clojure中此笛卡尔产物功能的语法

发布于 2025-01-29 12:23:54 字数 795 浏览 1 评论 0原文

这是笛卡尔产品的一些代码,可以是两个列表,两个向量或两者中的任何数量组合。我真的很感谢第二,第四和最后一行的帮助,解释了每一行在做什么

(defn cartesian-product ;function name definition
      ([] '(())) ;need help understanding this
      ([xs & more] ; at least two variables, xs is one of them
       (mapcat #(map (partial cons %) ;mapcat means a create a concatenated map of the following
                                      ;still trying to figure out partial, but cons takes a
                                      ;variable and puts it in front of a sequence
                     (apply cartesian-product more)) ; this is the sequence that is mapped
                                                     ; using (partial cons %)
               xs))) ;not sure what this is here for

Here's some code for a cartesian product, it can be two lists, two vectors, or any number of combinations of the two. I'd really appreciate help with the second, fourth, and final lines, explaining what each line is doing

(defn cartesian-product ;function name definition
      ([] '(())) ;need help understanding this
      ([xs & more] ; at least two variables, xs is one of them
       (mapcat #(map (partial cons %) ;mapcat means a create a concatenated map of the following
                                      ;still trying to figure out partial, but cons takes a
                                      ;variable and puts it in front of a sequence
                     (apply cartesian-product more)) ; this is the sequence that is mapped
                                                     ; using (partial cons %)
               xs))) ;not sure what this is here for

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事还在继续 2025-02-05 12:23:54

这是一个经过重新设计的版本,说明正在发生的事情(以及如何):

(ns tst.demo.core
  (:use demo.core tupelo.core tupelo.test))

;----------------------------------------------------------------------------
; Lesson: how map & mapcat work
(defn dup [x]
  "Return 2 of the arg in a vector"
  [x x])

(dotest
  (let [nums [0 1 2]]
    (is= (mapv inc nums) [1 2 3])
    (is= (mapv dup nums) [[0 0] ; like a matrix, 2-D
                          [1 1]
                          [2 2]])

    ; mapcat glues together the inner "row" vectors. So the result is 1-D instead of 2-D
    (is= (mapcat dup nums) [0 0 1 1 2 2])))

然后是重新设计的代码

;----------------------------------------------------------------------------
(def empty-matrix [[]]) ; 0 rows, 0 cols

(defn cartesian-product ;function name definition
  "When called with 1 or more sequences, returns a list of all possible combinations
  of one item from each collection"
  ([]     ; if called with no args
   empty-matrix) ; return an empty matrix

  ; if called with 1 or more args,
  ([xs    ; first arg is named `xs` (i.e. plural for x values)
    & more] ; all other args are wrapped in a list named `more`
   (let [recursion-result (apply cartesian-product more) ; get cartesian prod of sequences 2..N
         inner-fn         (fn [arg] (map ; for each recursion-result
                                       (partial cons arg) ; glue arg to the front of it
                                       recursion-result))
         ; for each item in the first sequence (xs), glue to front of 
         ; each recursion result and then convert 2D->1D
         output           (mapcat inner-fn xs)]
     output)))

和一些单元测试以显示在行动中

(dotest
  (is= (cartesian-product [1 2 3]) [[1] [2] [3]])

  (is= (cartesian-product [1 2 3] [:a :b])
    [[1 :a]
     [1 :b]
     [2 :a]
     [2 :b]
     [3 :a]
     [3 :b]])

  (is= (cartesian-product [1 2 3] [:a :b] ["apple" "pea"])
    [[1 :a "apple"]
     [1 :a "pea"]
     [1 :b "apple"]
     [1 :b "pea"]
     [2 :a "apple"]
     [2 :a "pea"]
     [2 :b "apple"]
     [2 :b "pea"]
     [3 :a "apple"]
     [3 :a "pea"]
     [3 :b "apple"]
     [3 :b "pea"]]))

Here is a reworked version that illustrates what is going on (and how):

(ns tst.demo.core
  (:use demo.core tupelo.core tupelo.test))

;----------------------------------------------------------------------------
; Lesson: how map & mapcat work
(defn dup [x]
  "Return 2 of the arg in a vector"
  [x x])

(dotest
  (let [nums [0 1 2]]
    (is= (mapv inc nums) [1 2 3])
    (is= (mapv dup nums) [[0 0] ; like a matrix, 2-D
                          [1 1]
                          [2 2]])

    ; mapcat glues together the inner "row" vectors. So the result is 1-D instead of 2-D
    (is= (mapcat dup nums) [0 0 1 1 2 2])))

then the reworked code

;----------------------------------------------------------------------------
(def empty-matrix [[]]) ; 0 rows, 0 cols

(defn cartesian-product ;function name definition
  "When called with 1 or more sequences, returns a list of all possible combinations
  of one item from each collection"
  ([]     ; if called with no args
   empty-matrix) ; return an empty matrix

  ; if called with 1 or more args,
  ([xs    ; first arg is named `xs` (i.e. plural for x values)
    & more] ; all other args are wrapped in a list named `more`
   (let [recursion-result (apply cartesian-product more) ; get cartesian prod of sequences 2..N
         inner-fn         (fn [arg] (map ; for each recursion-result
                                       (partial cons arg) ; glue arg to the front of it
                                       recursion-result))
         ; for each item in the first sequence (xs), glue to front of 
         ; each recursion result and then convert 2D->1D
         output           (mapcat inner-fn xs)]
     output)))

and some unit tests to show it in action

(dotest
  (is= (cartesian-product [1 2 3]) [[1] [2] [3]])

  (is= (cartesian-product [1 2 3] [:a :b])
    [[1 :a]
     [1 :b]
     [2 :a]
     [2 :b]
     [3 :a]
     [3 :b]])

  (is= (cartesian-product [1 2 3] [:a :b] ["apple" "pea"])
    [[1 :a "apple"]
     [1 :a "pea"]
     [1 :b "apple"]
     [1 :b "pea"]
     [2 :a "apple"]
     [2 :a "pea"]
     [2 :b "apple"]
     [2 :b "pea"]
     [3 :a "apple"]
     [3 :a "pea"]
     [3 :b "apple"]
     [3 :b "pea"]]))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文