Clojurescript 多词替换(以及更多)
Clojurescript Multiple Word Replacement (and more)
我在数据库中有一些卡片,每张卡片都有一些文字段落。我想用 HTML 符号替换 "Specific Phrase(s)"。这样当它们显示时,短语是符号而不是文本。
我的代码运行得非常完美,除了我调用最终函数的方式。
(我会尝试不要过多地参与信息过载但是)
我这样调用最终函数:
...{:__html (add-symbols
(add-symbols
(add-symbols
(add-symbols
(add-symbols
(add-symbols
(add-symbols
(add-symbols
(add-symbols
(add-symbols
(add-symbols
(add-symbols
(:text card)))))))))))))}
我之所以这样调用这个函数,是因为 add-symbols 函数接受 "card-text" 并找到第一次出现,然后 returns "card-text" 或 (:文本卡)更新,然后再次调用它。
由于第一次出现现在已经消失并替换为 html 编码符号,我可以再次扫描并替换等,此次数将涵盖文本量和符号频率。
但最近,我一直在用 Clojure for the Brave 复习 Clojure,偶然发现并关注了 fn (REDUCE ...)
我知道这并不容易。但我在想一些事情:
(reduce add-symbols (:text card))
就像我说的,我知道这不会那么容易,;) 但是,我应该循环吗?或者什么?
我以前的代码有效,但它太可怜了,完全遍历所有和任何文本并替换所有短语,无论段落的长度如何,使用一些敏锐的减少,将是理想的!
感谢您的帮助!
作为一个粗略的大纲,您可能需要这样的东西:
{ :__html
(loop [curr-card (:text card)]
(if (is-finished curr-card)
curr-card
(recur (add-symbols curr-card)))) }
但是 is-finished
函数和 add-symbols
将取决于您的详细信息(请更新问题)。
好吧,我正在使用 loop,然后我阅读了您的 post,并认为我可能在正确的轨道上,但是呃(这种语言是艰难,但当你成功时,你会得到那种很好的奖励感,比如 "old days.")
我什至尝试使用 let 和 loop,因为我意识到我所做的只是 "pseudo-recusively" 调用我的函数手动,13 次,然后说,这就足够了。在学习了这么多之后,最后,我决定(在 Redefining a let'd variable in Clojure loop 的帮助下
) 使用 when 因为 "is-finished" 子句这将是最好的。
此代码完成作业:
...{:__html
(let [new-text (atom (add-symbols (:text card)))]
(when (= @new-text (add-symbols @new-text))
@new-text
(swap! new-text #(add-symbols @new-text))))}...
使用循环
...{:__html
(loop [new-text (:text card)]
(if (= new-text (add-symbols new-text))
new-text
(recur (add-symbols new-text))))}...
他们(两者)工作干净利落。我不想使用 "more information" 的原因是因为我从 "Clojure for the Brave":
中读到这一行
"Clojure’s structure is very simple and consistent by comparison. No matter which operator you’re using or what kind of data you’re operating on, the structure is the same."
这对我来说意味着,"Look no matter what you are doing, flipping pancakes or changing tires, if you have an operator and data, at some "核心”操作将是相同的,为此我喜欢 Clojure!我知道我没有坚持使用纯粹的不可变对象,但我认为 atom 最终必须使用...
哦,基本上 "is-finished" 正在检查,运行 FN,文本是否更改?
谢谢!!
其他源代码:
(defn make-span [text symbol path]
(.replace text (apply str symbol) (str "<img src='" path "'style=\"width:16px;height:16px;\"></img>")))
(defn add-symbols [card-text]
(-> (if (nil? card-text) "" card-text)
(make-span "[b]" "img/dc/me_bl.png")
(make-span "[c]" "img/dc/me_cs.png")
(make-span "[d]" "img/dc/me_dd.png")
(make-span "[f]" "img/dc/me_fd.png")
(make-span "[j]" "img/dc/me_ju.png")
(make-span "[s]" "img/dc/me_sl.png")
(make-span "[w]" "img/dc/me_wi.png")))
(defn- card-text
"Generate text html representation a card"
[card cursor]
[:div
[:h4 (:title card)]
[:div.text
[:p [:span.type (str (:type card))] (if (= (.toLowerCase (:type card)) (:Secondary card))
""
(str ": " (:Secondary card)))]
[:pre {:dangerouslySetInnerHTML #js {:__html
(let [new-text (atom (add-symbols (:text card)))]
(when (= @new-text (add-symbols @new-text))
@new-text
(swap! new-text #(add-symbols @new-text))))}}]]])
还有更多代码,但这并不是这个问题的真正目的或应用。我真的很高兴能够找到问题的确切要点或 "MEAT of the MATTER",再次感谢!!
您 可以 使用 reduce
如果您将它与 iterate
结合起来产生一系列新的文本值,以及一个停止减少的函数当看不到任何变化时。 add-symbols
的 ,从值 (:text card)
:
开始
(reduce #(if (= %1 %2)
(reduced %1)
%2)
(iterate add-symbols (:text card)))
作为奖励,使用 ClojureScript 1.10.238 iterate
生成 directly reducible
结果。没有产生中间序列(很像低级 loop
/ recur
方法)。
我在数据库中有一些卡片,每张卡片都有一些文字段落。我想用 HTML 符号替换 "Specific Phrase(s)"。这样当它们显示时,短语是符号而不是文本。
我的代码运行得非常完美,除了我调用最终函数的方式。
(我会尝试不要过多地参与信息过载但是) 我这样调用最终函数:
...{:__html (add-symbols
(add-symbols
(add-symbols
(add-symbols
(add-symbols
(add-symbols
(add-symbols
(add-symbols
(add-symbols
(add-symbols
(add-symbols
(add-symbols
(:text card)))))))))))))}
我之所以这样调用这个函数,是因为 add-symbols 函数接受 "card-text" 并找到第一次出现,然后 returns "card-text" 或 (:文本卡)更新,然后再次调用它。
由于第一次出现现在已经消失并替换为 html 编码符号,我可以再次扫描并替换等,此次数将涵盖文本量和符号频率。
但最近,我一直在用 Clojure for the Brave 复习 Clojure,偶然发现并关注了 fn (REDUCE ...)
我知道这并不容易。但我在想一些事情:
(reduce add-symbols (:text card))
就像我说的,我知道这不会那么容易,;) 但是,我应该循环吗?或者什么?
我以前的代码有效,但它太可怜了,完全遍历所有和任何文本并替换所有短语,无论段落的长度如何,使用一些敏锐的减少,将是理想的!
感谢您的帮助!
作为一个粗略的大纲,您可能需要这样的东西:
{ :__html
(loop [curr-card (:text card)]
(if (is-finished curr-card)
curr-card
(recur (add-symbols curr-card)))) }
但是 is-finished
函数和 add-symbols
将取决于您的详细信息(请更新问题)。
好吧,我正在使用 loop,然后我阅读了您的 post,并认为我可能在正确的轨道上,但是呃(这种语言是艰难,但当你成功时,你会得到那种很好的奖励感,比如 "old days.")
我什至尝试使用 let 和 loop,因为我意识到我所做的只是 "pseudo-recusively" 调用我的函数手动,13 次,然后说,这就足够了。在学习了这么多之后,最后,我决定(在 Redefining a let'd variable in Clojure loop 的帮助下 ) 使用 when 因为 "is-finished" 子句这将是最好的。
此代码完成作业:
...{:__html
(let [new-text (atom (add-symbols (:text card)))]
(when (= @new-text (add-symbols @new-text))
@new-text
(swap! new-text #(add-symbols @new-text))))}...
使用循环
...{:__html
(loop [new-text (:text card)]
(if (= new-text (add-symbols new-text))
new-text
(recur (add-symbols new-text))))}...
他们(两者)工作干净利落。我不想使用 "more information" 的原因是因为我从 "Clojure for the Brave":
中读到这一行"Clojure’s structure is very simple and consistent by comparison. No matter which operator you’re using or what kind of data you’re operating on, the structure is the same."
这对我来说意味着,"Look no matter what you are doing, flipping pancakes or changing tires, if you have an operator and data, at some "核心”操作将是相同的,为此我喜欢 Clojure!我知道我没有坚持使用纯粹的不可变对象,但我认为 atom 最终必须使用...
哦,基本上 "is-finished" 正在检查,运行 FN,文本是否更改?
谢谢!!
其他源代码:
(defn make-span [text symbol path]
(.replace text (apply str symbol) (str "<img src='" path "'style=\"width:16px;height:16px;\"></img>")))
(defn add-symbols [card-text]
(-> (if (nil? card-text) "" card-text)
(make-span "[b]" "img/dc/me_bl.png")
(make-span "[c]" "img/dc/me_cs.png")
(make-span "[d]" "img/dc/me_dd.png")
(make-span "[f]" "img/dc/me_fd.png")
(make-span "[j]" "img/dc/me_ju.png")
(make-span "[s]" "img/dc/me_sl.png")
(make-span "[w]" "img/dc/me_wi.png")))
(defn- card-text
"Generate text html representation a card"
[card cursor]
[:div
[:h4 (:title card)]
[:div.text
[:p [:span.type (str (:type card))] (if (= (.toLowerCase (:type card)) (:Secondary card))
""
(str ": " (:Secondary card)))]
[:pre {:dangerouslySetInnerHTML #js {:__html
(let [new-text (atom (add-symbols (:text card)))]
(when (= @new-text (add-symbols @new-text))
@new-text
(swap! new-text #(add-symbols @new-text))))}}]]])
还有更多代码,但这并不是这个问题的真正目的或应用。我真的很高兴能够找到问题的确切要点或 "MEAT of the MATTER",再次感谢!!
您 可以 使用 reduce
如果您将它与 iterate
结合起来产生一系列新的文本值,以及一个停止减少的函数当看不到任何变化时。 add-symbols
的 (:text card)
:
(reduce #(if (= %1 %2)
(reduced %1)
%2)
(iterate add-symbols (:text card)))
作为奖励,使用 ClojureScript 1.10.238 iterate
生成 directly reducible
结果。没有产生中间序列(很像低级 loop
/ recur
方法)。