|
2187 | 2187 | [coll] |
2188 | 2188 | (let [do-reverse (fn do-reverse |
2189 | 2189 | [in out] |
2190 | | - (if (seq in) |
| 2190 | + (if-let [in (seq in)] |
2191 | 2191 | (recur (rest in) (cons (first in) out)) |
2192 | 2192 | out))] |
2193 | 2193 | (do-reverse coll '()))) |
|
2668 | 2668 | (defn every? |
2669 | 2669 | "Return ``true`` if every element in ``coll`` satisfies ``pred``\\." |
2670 | 2670 | [pred coll] |
2671 | | - (cond |
2672 | | - (nil? (seq coll)) true |
2673 | | - (pred (first coll)) (recur pred (rest coll)) |
2674 | | - :else false)) |
| 2671 | + (let [coll (seq coll)] |
| 2672 | + (cond |
| 2673 | + (nil? coll) true |
| 2674 | + (pred (first coll)) (recur pred (rest coll)) |
| 2675 | + :else false))) |
2675 | 2676 |
|
2676 | 2677 | (defn every-pred |
2677 | 2678 | "Return a predicate composed of all of the input predicates, which returns ``true`` |
|
2700 | 2701 | (defn some |
2701 | 2702 | "Return ``true`` if at least one element in ``coll`` satisfies ``pred``\\." |
2702 | 2703 | [pred coll] |
2703 | | - (when (seq coll) |
| 2704 | + (when-let [coll (seq coll)] |
2704 | 2705 | (or (pred (first coll)) |
2705 | 2706 | (recur pred (rest coll))))) |
2706 | 2707 |
|
|
2745 | 2746 | (rf result (apply f input inputs)))))) |
2746 | 2747 | ([f coll] |
2747 | 2748 | (lazy-seq |
2748 | | - (when (seq coll) |
| 2749 | + (when-let [coll (seq coll)] |
2749 | 2750 | (cons (f (first coll)) (map f (rest coll)))))) |
2750 | 2751 | ([f coll & colls] |
2751 | 2752 | (lazy-seq |
2752 | | - (when (and (seq coll) (every? seq colls)) |
2753 | | - (cons (apply f (first coll) (map first colls)) |
2754 | | - (apply map f (rest coll) (map rest colls))))))) |
| 2753 | + (when-let [coll (seq coll)] |
| 2754 | + (let [colls (map seq colls)] |
| 2755 | + (when (every? some? colls) |
| 2756 | + (cons (apply f (first coll) (map first colls)) |
| 2757 | + (apply map f (rest coll) (map rest colls))))))))) |
2755 | 2758 |
|
2756 | 2759 | (def ^{:doc "Return a vector of ``(f elem)`` for elements in ``coll``\\. More than one |
2757 | 2760 | collection may be supplied. If more than one collection is supplied, the |
|
2806 | 2809 | result))))) |
2807 | 2810 | ([pred coll] |
2808 | 2811 | (lazy-seq |
2809 | | - (when (seq coll) |
| 2812 | + (when-let [coll (seq coll)] |
2810 | 2813 | (if (pred (first coll)) |
2811 | 2814 | (cons (first coll) (filter pred (rest coll))) |
2812 | 2815 | (filter pred (rest coll))))))) |
|
2841 | 2844 | (rf result v))))))) |
2842 | 2845 | ([f coll] |
2843 | 2846 | (lazy-seq |
2844 | | - (when (seq coll) |
| 2847 | + (when-let [coll (seq coll)] |
2845 | 2848 | (let [elem (f (first coll))] |
2846 | 2849 | (if (nil? elem) |
2847 | 2850 | (keep f (rest coll)) |
|
2867 | 2870 | (let [keep-idx (fn keep-idx |
2868 | 2871 | [rng coll] |
2869 | 2872 | (lazy-seq |
2870 | | - (when (seq coll) |
| 2873 | + (when-let [coll (seq coll)] |
2871 | 2874 | (let [elem (f (first rng) (first coll))] |
2872 | 2875 | (if (nil? elem) |
2873 | 2876 | (keep-idx (rest rng) (rest coll)) |
|
2889 | 2892 | ([n coll] |
2890 | 2893 | (lazy-seq |
2891 | 2894 | (when (> n 0) |
2892 | | - (when (seq coll) |
| 2895 | + (when-let [coll (seq coll)] |
2893 | 2896 | (cons (first coll) (take (dec n) (rest coll)))))))) |
2894 | 2897 |
|
2895 | 2898 | (defn take-while |
|
2907 | 2910 | (ensure-reduced result)))))) |
2908 | 2911 | ([pred coll] |
2909 | 2912 | (lazy-seq |
2910 | | - (when (seq coll) |
| 2913 | + (when-let [coll (seq coll)] |
2911 | 2914 | (when (pred (first coll)) |
2912 | 2915 | (cons (first coll) (take-while pred (rest coll)))))))) |
2913 | 2916 |
|
|
2927 | 2930 | (rf result input))))))) |
2928 | 2931 | ([n coll] |
2929 | 2932 | (lazy-seq |
2930 | | - (when (seq coll) |
| 2933 | + (when-let [coll (seq coll)] |
2931 | 2934 | (if (> n 0) |
2932 | 2935 | (drop (dec n) (rest coll)) |
2933 | 2936 | (seq coll)))))) |
|
2949 | 2952 | :else result)))))) |
2950 | 2953 | ([pred coll] |
2951 | 2954 | (lazy-seq |
2952 | | - (when (seq coll) |
| 2955 | + (when-let [coll (seq coll)] |
2953 | 2956 | (if (pred (first coll)) |
2954 | 2957 | (drop-while pred (rest coll)) |
2955 | 2958 | (seq coll)))))) |
|
3019 | 3022 | (rf (rf result sep) input))))))) |
3020 | 3023 | ([sep coll] |
3021 | 3024 | (lazy-seq |
3022 | | - (when (seq coll) |
| 3025 | + (when-let [coll (seq coll)] |
3023 | 3026 | (if (seq (rest coll)) |
3024 | 3027 | (cons (first coll) |
3025 | 3028 | (cons sep (interpose sep (rest coll)))) |
|
3038 | 3041 | (when (seq colls) |
3039 | 3042 | (cons (ffirst colls) (apply coll-firsts (rest colls))))))] |
3040 | 3043 | (lazy-seq |
3041 | | - (when (and (seq coll) (every? seq colls)) |
3042 | | - (concat (apply coll-firsts coll colls) |
3043 | | - (apply interleave (rest coll) (map rest colls)))))))) |
| 3044 | + (when-let [coll (seq coll)] |
| 3045 | + (let [colls (map seq colls)] |
| 3046 | + (when (every? some? colls) |
| 3047 | + (concat (apply coll-firsts coll colls) |
| 3048 | + (apply interleave (rest coll) (map rest colls)))))))))) |
3044 | 3049 |
|
3045 | 3050 | (defn cycle |
3046 | 3051 | "Cycle the items in ``coll`` infinitely." |
3047 | 3052 | [coll] |
3048 | | - (let [coll-cycle (fn coll-cycle |
| 3053 | + (let [coll (seq coll) |
| 3054 | + coll-cycle (fn coll-cycle |
3049 | 3055 | [curr] |
3050 | 3056 | (lazy-seq |
3051 | 3057 | (if (seq curr) |
|
3091 | 3097 | result))))))) |
3092 | 3098 | ([n coll] |
3093 | 3099 | (lazy-seq |
3094 | | - (when (seq coll) |
| 3100 | + (when-let [coll (seq coll)] |
3095 | 3101 | (if (<= n 0) |
3096 | 3102 | (repeat (first coll)) |
3097 | 3103 | (cons (first coll) |
|
3108 | 3114 | (partition n n coll)) |
3109 | 3115 | ([n step coll] |
3110 | 3116 | (lazy-seq |
3111 | | - (when (seq coll) |
| 3117 | + (when-let [coll (seq coll)] |
3112 | 3118 | (let [s (take n coll)] |
3113 | 3119 | (when (= n (count s)) |
3114 | 3120 | (cons s (partition n step (drop step coll)))))))) |
3115 | 3121 | ([n step pad coll] |
3116 | 3122 | (lazy-seq |
3117 | | - (when (seq coll) |
| 3123 | + (when-let [coll (seq coll)] |
3118 | 3124 | (let [s (take n coll) |
3119 | 3125 | ns (count s) |
3120 | 3126 | s (if (< ns n) |
|
3151 | 3157 | (partition-all n n coll)) |
3152 | 3158 | ([n step coll] |
3153 | 3159 | (lazy-seq |
3154 | | - (when (seq coll) |
| 3160 | + (when-let [coll (seq coll)] |
3155 | 3161 | (cons (take n coll) (partition-all n step (drop step coll))))))) |
3156 | 3162 |
|
3157 | 3163 | (defn partition-by |
|
3191 | 3197 | (rf result elem))))))))) |
3192 | 3198 | ([f coll] |
3193 | 3199 | (lazy-seq |
3194 | | - (when (seq coll) |
| 3200 | + (when-let [coll (seq coll)] |
3195 | 3201 | (let [elem (first coll) |
3196 | 3202 | felem (f elem) |
3197 | 3203 | run (cons elem (take-while #(= felem (f %)) (next coll)))] |
|
3216 | 3222 | (let [coll-distinct (fn coll-distinct |
3217 | 3223 | [coll found] |
3218 | 3224 | (lazy-seq |
3219 | | - (when (seq coll) |
| 3225 | + (when-let [coll (seq coll)] |
3220 | 3226 | (let [e (first coll)] |
3221 | 3227 | (if-not (contains? found e) |
3222 | 3228 | (cons e (coll-distinct (rest coll) (conj found e))) |
|
3250 | 3256 | (cons e (coll-dedupe (rest coll) e)) |
3251 | 3257 | (coll-dedupe (rest coll) prev))))))] |
3252 | 3258 | (lazy-seq |
3253 | | - (when-let [e (first coll)] |
3254 | | - (cons e (coll-dedupe (rest coll) e))))))) |
| 3259 | + (let [coll (seq coll)] |
| 3260 | + (when-let [e (first coll)] |
| 3261 | + (cons e (coll-dedupe (rest coll) e)))))))) |
3255 | 3262 |
|
3256 | 3263 | (defn flatten |
3257 | 3264 | "Flatten any combination of nested sequences (such as lists or vectors) into a single |
|
3269 | 3276 | (defn take-last |
3270 | 3277 | "Return the last ``n`` items in ``coll`` in linear time." |
3271 | 3278 | [n coll] |
3272 | | - (loop [c (seq coll) |
3273 | | - rem (seq (drop n coll))] |
3274 | | - (if rem |
3275 | | - (recur (next c) (next rem)) |
3276 | | - c))) |
| 3279 | + (let [coll (seq coll)] |
| 3280 | + (loop [c coll |
| 3281 | + rem (seq (drop n coll))] |
| 3282 | + (if rem |
| 3283 | + (recur (next c) (next rem)) |
| 3284 | + c)))) |
3277 | 3285 |
|
3278 | 3286 | (defn min-key |
3279 | 3287 | "Return the arg for which ``(k arg)`` is the smallest number. If multiple values |
|
0 commit comments