Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Clojure:如何从元组中获取特定值_Clojure_Tuples - Fatal编程技术网

Clojure:如何从元组中获取特定值

Clojure:如何从元组中获取特定值,clojure,tuples,Clojure,Tuples,我一直在Clojure中构建我自己的项目,我希望通过搜索前两个值并获取第三个值,从这个元组中获取值 我四处搜索,找不到问题的解决方案,我想构建一个函数,它会要求每个特定向量中的前两个值中有两个,所以它会得到第三个 这是到目前为止的元组 (def cars '#{[has car wheels] [has car radio] [has vauxhall suspension] [colour vauxhall red] [colour ford blue] [is

我一直在Clojure中构建我自己的项目,我希望通过搜索前两个值并获取第三个值,从这个元组中获取值

我四处搜索,找不到问题的解决方案,我想构建一个函数,它会要求每个特定向量中的前两个值中有两个,所以它会得到第三个

这是到目前为止的元组

(def cars
'#{[has car wheels]
   [has car radio]
   [has vauxhall suspension]
   [colour vauxhall red]
   [colour ford blue]
   [is vauxhall car]
   [is ford car]})
例如,如果我构建了一个名为search的函数,并输入向量的前两个值,它会是这样的

user   => (search 'has 'vauxhall)
answer => suspension
因为元组中的向量是[has vauxhall suspension]

我只是好奇有没有什么办法?因为我在网上找不到任何进程,但我想构建一个类似于我提出的搜索功能的函数,而不是任何可以得到答案的REPL快捷方式


如果您能提供帮助或意见,我将不胜感激。

这是一种方法

(def cars
  #{[:has :car :wheels]
    [:has :car :radio]
    [:has :vauxhall :suspension]
    [:colour :vauxhall :red]
    [:colour :ford :blue]
    [:is :vauxhall :car]
    [:is :ford :car]})

(defn search [word1 word2]
  (some (fn [[w1 w2 w3]]
          (and (= word1 w1) (= word2 w2) w3)
        cars))

(search :has :vauxhall)
;; =>:suspension
注:我将元素编码为关键字而不是符号-关键字自行计算

搜索函数使用该函数

对于coll中的任何x,返回pred x的第一个逻辑真值,否则返回nil

如果所有参数均为true,则返回最后一个参数。在本例中,这是请求的元素

这仅返回第一次命中,如果您想使用它们,请执行以下操作:


这是一种方法

(def cars
  #{[:has :car :wheels]
    [:has :car :radio]
    [:has :vauxhall :suspension]
    [:colour :vauxhall :red]
    [:colour :ford :blue]
    [:is :vauxhall :car]
    [:is :ford :car]})

(defn search [word1 word2]
  (some (fn [[w1 w2 w3]]
          (and (= word1 w1) (= word2 w2) w3)
        cars))

(search :has :vauxhall)
;; =>:suspension
注:我将元素编码为关键字而不是符号-关键字自行计算

搜索函数使用该函数

对于coll中的任何x,返回pred x的第一个逻辑真值,否则返回nil

如果所有参数均为true,则返回最后一个参数。在本例中,这是请求的元素

这仅返回第一次命中,如果您想使用它们,请执行以下操作:


我会让它更通用,提供一个通过前缀获取数据的函数

user> (defn with-prefix [prefix data]
        (filter #(= prefix (take (count prefix) %)) data))
#'user/with-prefix


user> (->> cars
           (with-prefix '[has vauxhall])
           first
           last)

;;=> suspension

我会让它更通用,提供一个通过前缀获取数据的函数

user> (defn with-prefix [prefix data]
        (filter #(= prefix (take (count prefix) %)) data))
#'user/with-prefix


user> (->> cars
           (with-prefix '[has vauxhall])
           first
           last)

;;=> suspension

不确定您的用例是什么,但是如果您将其建模为一个嵌套映射,那么问题将非常简单

def汽车 {:有{:汽车[车轮:收音机] :沃克斯霍尔:暂停} :颜色{:沃克斯霍尔:红色 :福特:蓝色} :是{:沃克斯霍尔:车 :福特:汽车} 上车[:has:vauxhall];=>:暂停 上车[:has:car];=>[:轮子:收音机] get in允许您轻松查询嵌套映射,从而大大简化了代码


同样不确定您的用例是什么,但是将列表查询转换为这种嵌套映射结构将非常容易。

不确定您的用例是什么,但是如果将其建模为嵌套映射,那么问题将非常简单

def汽车 {:有{:汽车[车轮:收音机] :沃克斯霍尔:暂停} :颜色{:沃克斯霍尔:红色 :福特:蓝色} :是{:沃克斯霍尔:车 :福特:汽车} 上车[:has:vauxhall];=>:暂停 上车[:has:car];=>[:轮子:收音机] get in允许您轻松查询嵌套映射,从而大大简化了代码


同样,我不确定您的用例是什么,但将列表查询转换为这种嵌套映射结构将非常容易。

很高兴听到这个消息。祝你的项目好运!:很高兴听到。祝你的项目好运!: