Clojure で分割した行の全ての空行を保持したいんだけど…
文字列中の最後の空行を Clojure だとどうやって残したらいいの?という質問。
user=> (require 'clojure.string) nil user=> (clojure.string/split-lines "a\n\nb\n") ["a" "" "b"] user=> (clojure.string/split "a\n\nb\n" #"\n") ["a" "" "b"]
Python だと
>>> 'a\n\nb\n'.split('\n') ['a', '', 'b', '']
こう書けるのに…。という質問者。
Clojure だともし空行を残したいのなら clojure.string/split の最後の引数にネガティブ(マイナス)の値を付加してあげればいいようですね。
(clojure.string/split "a\n\nb\n" #"\n" -1)
ドキュメント中の example にも書いてあります。
;; If you want all of the fields, even trailing empty ones, use a ;; negative limit. user=> (split "root::0:0:admin:/var/root:" #":" -1) ["root" "" "0" "0" "admin" "/var/root" ""] user=> (split "root::0:0:admin::" #":" -1) ["root" "" "0" "0" "admin" "" ""]
split - clojure.string | ClojureDocs - Community-Powered Clojure Documentation and Examples