繁体   English   中英

如何在 Clojure 中使用 Remove

[英]How to Use Remove in Clojure

(ns untitled1.core
(:require [clojure.string :as str]))

(defn foo

[half full] 

;Assume full = I am a taco
;Assume half = taco

(remove full half)

这是我收到的消息:“在 clojure.core/complement$fn (core.clj:1441) 处打印返回值时出错 (ClassCastException)。类 clojure.lang.LazySeq 不能转换为类 clojure.lang.IFn (clojure. lang.LazySeq 和 clojure.lang.IFn 位于加载程序‘app’的未命名模块中)”

我要(删除整半)返回 -> 我是

我是 Clojure 的新手,所以我可以想象 (remove) 可能是错误的功能。 如果有人能引导我朝着正确的方向前进,我将不胜感激。

查看remove函数的文档,您会发现,正如您所怀疑的,这不是您要查找的函数https://clojuredocs.org/clojure.core/remove (您还会看到它的第一个参数是一个函数,它解释了您看到的错误消息)

为此,我将使用clojure.string/replace (在这种情况下,用clojure.string/replace与删除相同)。 例如:

user> (def full "I am a taco")
#'user/fulluser> 
user> (def half "taco")
#'user/half
user> (clojure.string/replace full half "")
"I am a "

作为函数

(ns untitled1.core
  (:require [clojure.string :as str]))

(defn foo
  [full half]
  (str/replace full half ""))

调用函数:

untitled1.core> (foo "I am a taco" "taco")
"I am a "

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM