|
4800 | 4800 | "; Note that recur can be surprising when using variadic functions.\n\n(defn foo [& args]\n (let [[x & more] args]\n (prn x)\n (if more (recur more) nil)))\n\n(defn bar [& args]\n (let [[x & more] args]\n (prn x)\n (if more (bar more) nil)))\n\n; The key thing to note here is that foo and bar are identical, except\n; that foo uses recur and bar uses \"normal\" recursion. And yet...\n\nuser=> (foo :a :b :c)\n:a\n:b\n:c\nnil\n\nuser=> (bar :a :b :c)\n:a\n(:b :c)\nnil\n\n; The difference arises because recur does not gather variadic/rest args\n; into a seq." |
4801 | 4801 | ";;This will generate the first 1000 Fibonacci numbers \n;;(using incrementing and decrementing): \n\n(loop [res [0 1]]\n (if (>= (count res) 1000)\n res\n (recur (conj res (+' (inc (last res)) (dec (last (butlast res))))))))" |
4802 | 4802 | ";; The recursion point can be a 'loop' or a 'fn' method.\n\n(loop [n (bigint 5), accumulator 1]\n (if (zero? n)\n accumulator ; we're done\n (recur (dec n) (* accumulator n))))\n;;=> 120N\n\n\n((fn factorial\n ([n] (factorial n 1))\n\n ([n accumulator]\n (if (zero? n)\n accumulator ; we're done\n (recur (dec n) (* accumulator n)))))\n\n (bigint 5))\n;;=> 120N" |
4803 | | - ";; Trailing position could be multiple\n(loop [x 1]\n (println \"x= \" x)\n (cond\n (> x 10) (println \"ending at \" x )\n (even? x) (recur (* 2 x))\n :else (recur (+ x 1))))"], |
| 4803 | + ";; Trailing position could be multiple\n(loop [x 1]\n (println \"x= \" x)\n (cond\n (> x 10) (println \"ending at \" x )\n (even? x) (recur (* 2 x))\n :else (recur (+ x 1))))" |
| 4804 | + ";; see `foo` and `bar` above\n;; this `baz` uses normal recursion, this blows stacks but `foo` doesn't\n\n(defn baz [& args]\n (let [[x & more] args]\n (prn x)\n (if more (apply baz more) nil)))\n\n(baz :a :b :c)\n;; :a\n;; :b\n;; :c\n;;=> nil\n"], |
4804 | 4805 | :notes nil, |
4805 | 4806 | :arglists [], |
4806 | 4807 | :doc |
|
25811 | 25812 | :clojure.string/join], |
25812 | 25813 | :line 219, |
25813 | 25814 | :examples |
25814 | | - ["user=> (require '[clojure.string :as str])\n\nuser=> (str/split \"Clojure is awesome!\" #\" \")\n[\"Clojure\" \"is\" \"awesome!\"]\n\nuser=> (str/split \"q1w2e3r4t5y6u7i8o9p0\" #\"\\d+\")\n[\"q\" \"w\" \"e\" \"r\" \"t\" \"y\" \"u\" \"i\" \"o\" \"p\"]\n\n;; Note that the 'limit' arg is the maximum number of strings to\n;; return (not the number of splits)\nuser=> (str/split \"q1w2e3r4t5y6u7i8o9p0\" #\"\\d+\" 5)\n[\"q\" \"w\" \"e\" \"r\" \"t5y6u7i8o9p0\"]\n\n;; to get back all the characters of a string, as a vector of strings:\nuser=> (str/split \" q1w2 \" #\"\")\n[\" \" \"q\" \"1\" \"w\" \"2\" \" \"]\n;; Note: sequence, in contrast, would return characters.\n\n;; Using lookarounds (lookahead, lookbehind) one can keep the matching characters:\nuser=> (str/split \" something and ACamelName \" #\"(?=[A-Z])\")\n[\" something and \" \"A\" \"Camel\" \"Name \"]\n\n;; If the pattern is not found, we get back the original string untouched:\nuser=> (str/split \"a\" #\"b\")\n[\"a\"]\n\nuser=> (str/split \" \" #\"b\")\n[\" \"]\n\nuser=> (str/split \"\" #\"b\")\n[\"\"]\n\n;; If everything matches, an empty vector is returned!\nuser=> (str/split \"a\" #\"a\")\n[]\n\nuser=> (str/split \"aaa\" #\"a\")\n[]\n\n;; but:\nuser=> (str/split \"\" #\"\")\n[\"\"]" |
| 25815 | + ["user=> (require '[clojure.string :as str])\n\nuser=> (str/split \"Clojure is awesome!\" #\" \")\n[\"Clojure\" \"is\" \"awesome!\"]\n\nuser=> (str/split \"q1w2e3r4t5y6u7i8o9p0\" #\"\\d+\")\n[\"q\" \"w\" \"e\" \"r\" \"t\" \"y\" \"u\" \"i\" \"o\" \"p\"]\n\n;; Note that the 'limit' arg is the maximum number of strings to\n;; return (not the number of splits)\nuser=> (str/split \"q1w2e3r4t5y6u7i8o9p0\" #\"\\d+\" 5)\n[\"q\" \"w\" \"e\" \"r\" \"t5y6u7i8o9p0\"]\n\n;; to get back all the characters of a string, as a vector of strings:\nuser=> (str/split \" q1w2 \" #\"\")\n[\" \" \"q\" \"1\" \"w\" \"2\" \" \"]\n;; Note: sequence, in contrast, would return characters.\n\n;; Using lookarounds (lookahead, lookbehind) one can keep the matching characters (aka delimiters or splitters):\nuser=> (str/split \" something and ACamelName \" #\"(?=[A-Z])\")\n[\" something and \" \"A\" \"Camel\" \"Name \"]\n\n;; If the pattern is not found, we get back the original string untouched:\nuser=> (str/split \"a\" #\"b\")\n[\"a\"]\n\nuser=> (str/split \" \" #\"b\")\n[\" \"]\n\nuser=> (str/split \"\" #\"b\")\n[\"\"]\n\n;; If everything matches, an empty vector is returned!\nuser=> (str/split \"a\" #\"a\")\n[]\n\nuser=> (str/split \"aaa\" #\"a\")\n[]\n\n;; but:\nuser=> (str/split \"\" #\"\")\n[\"\"]" |
25815 | 25816 | ";; Splits a string on space character and joins \n;; the resulting collection with a line feed character\n\n(use '[clojure.string :only (join split)])\n\nuser=> (println\n (join \"\\n\"\n (split \"The Quick Brown Fox\" #\"\\s\")))\nThe\nQuick\nBrown\nFox\nnil" |
25816 | 25817 | "(use '[clojure.string :only (split triml)])\n\n;; Splitting on whitespace is a common desire.\nuser=> (split \"Some words to split\" #\"\\s+\")\n[\"Some\" \"words\" \"to\" \"split\"]\n\n;; By using the pattern #\"\\s+\", we split on all occurrences of one or\n;; more consecutive whitespace characters.\nuser=> (split \"Some words with\\tother whitespace \\n\" #\"\\s+\")\n[\"Some\" \"words\" \"with\" \"other\" \"whitespace\"]\n\n;; If you are used to Perl's special behavior of split(' ', $str),\n;; where it ignores leading whitespace in the string to be split, this\n;; does not quite do it.\nuser=> (split \" Leading whitespace causes empty first string\" #\"\\s+\")\n[\"\" \"Leading\" \"whitespace\" \"causes\" \"empty\" \"first\" \"string\"]\n\n;; This will do it.\nuser=> (defn perl-split-on-space [s]\n (split (triml s) #\"\\s+\"))\n#'user/perl-split-on-space\nuser=> (perl-split-on-space \" This is often what you want \")\n[\"This\" \"is\" \"often\" \"what\" \"you\" \"want\"]\n\n;; There might be cases where you want this instead.\nuser=> (split \"Some words with\\tother whitespace \\n\" #\"\\s\")\n[\"Some\" \"\" \"\" \"\" \"words\" \"\" \"\" \"with\" \"other\" \"whitespace\"]\n" |
25817 | 25818 | "(use '[clojure.string :only (split)])\n\n;; Split on every occurrence of : character\nuser=> (split \"root:*:0:0:admin:/var/root:/bin/sh\" #\":\")\n[\"root\" \"*\" \"0\" \"0\" \"admin\" \"/var/root\" \"/bin/sh\"]\n\n;; Empty strings are returned when two colons appear consecutively in\n;; the string to be split.\nuser=> (split \"root::0:0::/var/root:/bin/sh\" #\":\")\n[\"root\" \"\" \"0\" \"0\" \"\" \"/var/root\" \"/bin/sh\"]\n\n;; Without specifying a limit, any empty strings at the end are\n;; omitted.\nuser=> (split \"root::0:0:admin:/var/root:\" #\":\")\n[\"root\" \"\" \"0\" \"0\" \"admin\" \"/var/root\"]\nuser=> (split \"root::0:0:admin::\" #\":\")\n[\"root\" \"\" \"0\" \"0\" \"admin\"]\n\n;; If you want all of the fields, even trailing empty ones, use a\n;; negative limit.\nuser=> (split \"root::0:0:admin:/var/root:\" #\":\" -1)\n[\"root\" \"\" \"0\" \"0\" \"admin\" \"/var/root\" \"\"]\nuser=> (split \"root::0:0:admin::\" #\":\" -1)\n[\"root\" \"\" \"0\" \"0\" \"admin\" \"\" \"\"]\n\n;; Use a positive limit of n to limit the maximum number of strings in\n;; the return value to n. If it returns exactly n strings, the last\n;; contains everything left over after splitting off the n-1 earlier\n;; strings.\nuser=> (split \"root::0:0:admin:/var/root:\" #\":\" 2)\n[\"root\" \":0:0:admin:/var/root:\"]\nuser=> (split \"root::0:0:admin:/var/root:\" #\":\" 3)\n[\"root\" \"\" \"0:0:admin:/var/root:\"]\nuser=> (split \"root::0:0:admin:/var/root:\" #\":\" 4)\n[\"root\" \"\" \"0\" \"0:admin:/var/root:\"]\nuser=> (split \"root::0:0:admin:/var/root:\" #\":\" 15)\n[\"root\" \"\" \"0\" \"0\" \"admin\" \"/var/root\" \"\"]\n" |
|
0 commit comments