简体   繁体   English

Clojure While循环

[英]Clojure While Loop

I trying clojure i am trying to figure out how to implement the following algorithm, 我正在尝试clojure我试图弄清楚如何实现以下算法,

I am reading from an input stream i want to continue reading until it is not a delimiter character. 我正在从输入流中读取内容,我想继续读取直到它不是分隔符为止。

i can do this in java with a while loop but i can't seem to figure out how to do it in clojure? 我可以使用while循环在Java中执行此操作,但是我似乎无法弄清楚如何在clojure中执行此操作?

while
   read
   readChar != delimiter

   do some processing....
end while

I don't know Clojure, but it looks that, like Scheme, it supports "let loops": 我不了解Clojure,但看起来像Scheme一样,它支持“ let循环”:

(loop [char (readChar)]
   (if (= char delimiter)
       '()
       (do (some-processing)
           (recur (readChar)))))

Hope this is enough to get you started. 希望这足以让您入门。 I referred to http://clojure.org/special_forms#toc9 to answer this question. 我参考了http://clojure.org/special_forms#toc9来回答这个问题。

NOTE: I know that Clojure discourages side-effects, so presumably you want to return something useful instead of '(). 注意:我知道Clojure会阻止副作用,因此大概您想返回有用的东西而不是'()。

Working on Clojure 1.3.0, and for what it's worth, you can write while loops in Clojure now by doing something akin to 在Clojure 1.3.0上工作,以及它的价值,您可以通过执行类似于以下操作的方式在Clojure中编写while循环

(while truth-expression
  (call-some-function))

The loop approach will work fine in clojure however loop/recur are considered low level operations and higher order functions usually preferred. 循环方法在clojure中可以很好地工作,但是循环/重复被认为是低级操作,通常首选高阶函数。

Normally this sort of problem would be solved by creating a sequence of tokens (characters in your example) and applying on or more of clojure's sequence functions (doseq, dorun, take-while, etc.) 通常,可以通过创建令牌序列(示例中的字符)并在clojure的序列函数(doseq,dorun,take-while等)中应用一个或多个来解决此类问题。

The following example reads the first username from /etc/passwd on unix like systems. 以下示例在类似unix的系统上从/ etc / passwd读取第一个用户名。

(require '[clojure.java [io :as io]])

(defn char-seq
  "create a lazy sequence of characters from an input stream"
  [i-stream]
  (map char 
   (take-while
    (partial not= -1)
    (repeatedly #(.read i-stream)))))

;; process the sequence one token at a time
;; with-open will automatically close the stream for us 

(with-open [is (io/input-stream "/etc/passwd")]
  (doseq [c (take-while (partial not= \:) (char-seq is))]
    ;; your processing is done here
    (prn c)))

I came up with this in the spirit of line-seq . 我本着line-seq提出了这一点。 It's fully lazy and exhibits more of Clojure's functional nature than loop . 它完全是懒惰的,比loop具有更多Clojure的功能特性。

(defn delim-seq
  ([#^java.io.Reader rdr #^Character delim]
     (delim-seq rdr delim (StringBuilder.)))
  ([#^java.io.Reader rdr #^Character delim #^StringBuilder buf]
     (lazy-seq
       (let [ch (.read rdr)]
         (when-not (= ch -1)
           (if (= (char ch) delim)
             (cons (str buf) (delim-seq rdr delim))
             (delim-seq rdr delim (doto buf (.append (char ch))))))))))

Full paste . 全贴

A while-loop usually involves mutable variables, ie waiting until a variable meets a certain condition; while循环通常涉及可变变量,即等待直到变量满足特定条件。 in Clojure you'd usually use tail-recursion (which the compiler translates into a while-loop) 在Clojure中,您通常会使用tail递归(编译器将其转换为while循环)

The following is not quite a solution, but this variation of the for-loop might be of help in some cases: 以下内容并不是解决方案,但是for循环的这种变化在某些情况下可能会有所帮助:

(for [a (range 100) 
      b (range 100) 
      :while (< (* a b) 1000)] 
    [a b]
  )

This will create a list of all the pairs of a and b until (< (* ab) 1000) . 这将创建a和b所有对的列表, 直到 (< (* ab) 1000) 为止 That is it will stop as soon as the condition is met. 也就是说,一旦满足条件,它将立即停止。 If you replace :while with :when, they you can find all of the pairs that meet the condition, even after it finds one that doesn't. 如果用:when替换:while,则即使找到了不符合条件的对,也可以找到所有符合条件的对。

I came out with this version: 我出来了这个版本:

(defn read-until
  [^java.io.Reader rdr ^String delim]
  (let [^java.lang.StringBuilder salida (StringBuilder.) ]
    (while
      (not (.endsWith (.toString salida) delim))
      (.append salida (str (char (.read rdr))))
    )
    (.toString salida)
  )
)

It looks for a String, not a single char as delimiter! 它查找字符串,而不是单个char作为定界符!

Thanks! 谢谢!

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

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