[英]Chan of chars to chan of words
How can I make a words chan out of chars chan?我怎样才能用chars chan制作一个单词chan?
I have chan c that read all characters from file.我有 chan c 从文件中读取所有字符。 And i need words(str) chan from c chan.( words need to be made from с, not from a file)
我需要来自 c chan 的 words(str) chan。(单词需要由 с 组成,而不是来自文件)
(def file-as-str (slurp "src/clojure2/text.txt"))
(def read (str/split file-as-str #""))
(defn ch
[c]
(go
(doseq [o read]
(>! c o)
))
)
(defn word
[c]
(let [k (chan)]
(go-loop []
(let [o (<! c)]
(when (not= " " o)
(>! k o)
(recur))))
k))
(defn -main
[& args]
(let [c (chan)
words (chan)]
;(go-loop []
; (let [o (<! c)]
; (println o))
; (recur))
(ch c)
(word c)
)
)
UPDATE And so I made a function that returns a chan of characters for 1 word.更新所以我做了一个函数,它返回 1 个单词的字符 chan。 But I don't understand how to combine this into 1 word
但我不明白如何将其组合成 1 个词
You can create chan
with transducer which converts incoming characters to outgoing words.您可以使用转换器创建
chan
,将传入的字符转换为传出的单词。
(let [ch (async/chan 100 (comp
(partition-by (complement #{\space}))
(map #(apply str %))
(remove #{" "})))]
(doseq [c "Hello, world! "]
(async/>!! ch c))
(async/<!! (async/into [] (async/take 2 ch))))
=> ["Hello," "world!"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.