|
2198 | 2198 | [coll] |
2199 | 2199 | (let [do-reverse (fn do-reverse |
2200 | 2200 | [in out] |
2201 | | - (if (seq in) |
| 2201 | + (if-let [in (seq in)] |
2202 | 2202 | (recur (rest in) (cons (first in) out)) |
2203 | 2203 | out))] |
2204 | 2204 | (do-reverse coll '()))) |
|
2680 | 2680 | (defn every? |
2681 | 2681 | "Return ``true`` if every element in ``coll`` satisfies ``pred``\\." |
2682 | 2682 | [pred coll] |
2683 | | - (cond |
2684 | | - (nil? (seq coll)) true |
2685 | | - (pred (first coll)) (recur pred (rest coll)) |
2686 | | - :else false)) |
| 2683 | + (let [coll (seq coll)] |
| 2684 | + (cond |
| 2685 | + (nil? coll) true |
| 2686 | + (pred (first coll)) (recur pred (rest coll)) |
| 2687 | + :else false))) |
2687 | 2688 |
|
2688 | 2689 | (defn every-pred |
2689 | 2690 | "Return a predicate composed of all of the input predicates, which returns ``true`` |
|
2712 | 2713 | (defn some |
2713 | 2714 | "Return ``true`` if at least one element in ``coll`` satisfies ``pred``\\." |
2714 | 2715 | [pred coll] |
2715 | | - (when (seq coll) |
| 2716 | + (when-let [coll (seq coll)] |
2716 | 2717 | (or (pred (first coll)) |
2717 | 2718 | (recur pred (rest coll))))) |
2718 | 2719 |
|
|
2757 | 2758 | (rf result (apply f input inputs)))))) |
2758 | 2759 | ([f coll] |
2759 | 2760 | (lazy-seq |
2760 | | - (when (seq coll) |
| 2761 | + (when-let [coll (seq coll)] |
2761 | 2762 | (cons (f (first coll)) (map f (rest coll)))))) |
2762 | 2763 | ([f coll & colls] |
2763 | 2764 | (lazy-seq |
2764 | | - (when (and (seq coll) (every? seq colls)) |
2765 | | - (cons (apply f (first coll) (map first colls)) |
2766 | | - (apply map f (rest coll) (map rest colls))))))) |
| 2765 | + (when-let [coll (seq coll)] |
| 2766 | + (let [colls (map seq colls)] |
| 2767 | + (when (every? some? colls) |
| 2768 | + (cons (apply f (first coll) (map first colls)) |
| 2769 | + (apply map f (rest coll) (map rest colls))))))))) |
2767 | 2770 |
|
2768 | 2771 | (def ^{:doc "Return a vector of ``(f elem)`` for elements in ``coll``\\. More than one |
2769 | 2772 | collection may be supplied. If more than one collection is supplied, the |
|
2818 | 2821 | result))))) |
2819 | 2822 | ([pred coll] |
2820 | 2823 | (lazy-seq |
2821 | | - (when (seq coll) |
| 2824 | + (when-let [coll (seq coll)] |
2822 | 2825 | (if (pred (first coll)) |
2823 | 2826 | (cons (first coll) (filter pred (rest coll))) |
2824 | 2827 | (filter pred (rest coll))))))) |
|
2853 | 2856 | (rf result v))))))) |
2854 | 2857 | ([f coll] |
2855 | 2858 | (lazy-seq |
2856 | | - (when (seq coll) |
| 2859 | + (when-let [coll (seq coll)] |
2857 | 2860 | (let [elem (f (first coll))] |
2858 | 2861 | (if (nil? elem) |
2859 | 2862 | (keep f (rest coll)) |
|
2879 | 2882 | (let [keep-idx (fn keep-idx |
2880 | 2883 | [rng coll] |
2881 | 2884 | (lazy-seq |
2882 | | - (when (seq coll) |
| 2885 | + (when-let [coll (seq coll)] |
2883 | 2886 | (let [elem (f (first rng) (first coll))] |
2884 | 2887 | (if (nil? elem) |
2885 | 2888 | (keep-idx (rest rng) (rest coll)) |
|
2901 | 2904 | ([n coll] |
2902 | 2905 | (lazy-seq |
2903 | 2906 | (when (> n 0) |
2904 | | - (when (seq coll) |
| 2907 | + (when-let [coll (seq coll)] |
2905 | 2908 | (cons (first coll) (take (dec n) (rest coll)))))))) |
2906 | 2909 |
|
2907 | 2910 | (defn take-while |
|
2919 | 2922 | (ensure-reduced result)))))) |
2920 | 2923 | ([pred coll] |
2921 | 2924 | (lazy-seq |
2922 | | - (when (seq coll) |
| 2925 | + (when-let [coll (seq coll)] |
2923 | 2926 | (when (pred (first coll)) |
2924 | 2927 | (cons (first coll) (take-while pred (rest coll)))))))) |
2925 | 2928 |
|
|
2939 | 2942 | (rf result input))))))) |
2940 | 2943 | ([n coll] |
2941 | 2944 | (lazy-seq |
2942 | | - (when (seq coll) |
| 2945 | + (when-let [coll (seq coll)] |
2943 | 2946 | (if (> n 0) |
2944 | 2947 | (drop (dec n) (rest coll)) |
2945 | 2948 | (seq coll)))))) |
|
2961 | 2964 | :else result)))))) |
2962 | 2965 | ([pred coll] |
2963 | 2966 | (lazy-seq |
2964 | | - (when (seq coll) |
| 2967 | + (when-let [coll (seq coll)] |
2965 | 2968 | (if (pred (first coll)) |
2966 | 2969 | (drop-while pred (rest coll)) |
2967 | 2970 | (seq coll)))))) |
|
3031 | 3034 | (rf (rf result sep) input))))))) |
3032 | 3035 | ([sep coll] |
3033 | 3036 | (lazy-seq |
3034 | | - (when (seq coll) |
| 3037 | + (when-let [coll (seq coll)] |
3035 | 3038 | (if (seq (rest coll)) |
3036 | 3039 | (cons (first coll) |
3037 | 3040 | (cons sep (interpose sep (rest coll)))) |
|
3050 | 3053 | (when (seq colls) |
3051 | 3054 | (cons (ffirst colls) (apply coll-firsts (rest colls))))))] |
3052 | 3055 | (lazy-seq |
3053 | | - (when (and (seq coll) (every? seq colls)) |
3054 | | - (concat (apply coll-firsts coll colls) |
3055 | | - (apply interleave (rest coll) (map rest colls)))))))) |
| 3056 | + (when-let [coll (seq coll)] |
| 3057 | + (let [colls (map seq colls)] |
| 3058 | + (when (every? some? colls) |
| 3059 | + (concat (apply coll-firsts coll colls) |
| 3060 | + (apply interleave (rest coll) (map rest colls)))))))))) |
3056 | 3061 |
|
3057 | 3062 | (defn cycle |
3058 | 3063 | "Cycle the items in ``coll`` infinitely." |
3059 | 3064 | [coll] |
3060 | | - (let [coll-cycle (fn coll-cycle |
| 3065 | + (let [coll (seq coll) |
| 3066 | + coll-cycle (fn coll-cycle |
3061 | 3067 | [curr] |
3062 | 3068 | (lazy-seq |
3063 | 3069 | (if (seq curr) |
|
3103 | 3109 | result))))))) |
3104 | 3110 | ([n coll] |
3105 | 3111 | (lazy-seq |
3106 | | - (when (seq coll) |
| 3112 | + (when-let [coll (seq coll)] |
3107 | 3113 | (if (<= n 0) |
3108 | 3114 | (repeat (first coll)) |
3109 | 3115 | (cons (first coll) |
|
3120 | 3126 | (partition n n coll)) |
3121 | 3127 | ([n step coll] |
3122 | 3128 | (lazy-seq |
3123 | | - (when (seq coll) |
| 3129 | + (when-let [coll (seq coll)] |
3124 | 3130 | (let [s (take n coll)] |
3125 | 3131 | (when (= n (count s)) |
3126 | 3132 | (cons s (partition n step (drop step coll)))))))) |
3127 | 3133 | ([n step pad coll] |
3128 | 3134 | (lazy-seq |
3129 | | - (when (seq coll) |
| 3135 | + (when-let [coll (seq coll)] |
3130 | 3136 | (let [s (take n coll) |
3131 | 3137 | ns (count s) |
3132 | 3138 | s (if (< ns n) |
|
3163 | 3169 | (partition-all n n coll)) |
3164 | 3170 | ([n step coll] |
3165 | 3171 | (lazy-seq |
3166 | | - (when (seq coll) |
| 3172 | + (when-let [coll (seq coll)] |
3167 | 3173 | (cons (take n coll) (partition-all n step (drop step coll))))))) |
3168 | 3174 |
|
3169 | 3175 | (defn partition-by |
|
3203 | 3209 | (rf result elem))))))))) |
3204 | 3210 | ([f coll] |
3205 | 3211 | (lazy-seq |
3206 | | - (when (seq coll) |
| 3212 | + (when-let [coll (seq coll)] |
3207 | 3213 | (let [elem (first coll) |
3208 | 3214 | felem (f elem) |
3209 | 3215 | run (cons elem (take-while #(= felem (f %)) (next coll)))] |
|
3228 | 3234 | (let [coll-distinct (fn coll-distinct |
3229 | 3235 | [coll found] |
3230 | 3236 | (lazy-seq |
3231 | | - (when (seq coll) |
| 3237 | + (when-let [coll (seq coll)] |
3232 | 3238 | (let [e (first coll)] |
3233 | 3239 | (if-not (contains? found e) |
3234 | 3240 | (cons e (coll-distinct (rest coll) (conj found e))) |
|
3262 | 3268 | (cons e (coll-dedupe (rest coll) e)) |
3263 | 3269 | (coll-dedupe (rest coll) prev))))))] |
3264 | 3270 | (lazy-seq |
3265 | | - (when-let [e (first coll)] |
3266 | | - (cons e (coll-dedupe (rest coll) e))))))) |
| 3271 | + (let [coll (seq coll)] |
| 3272 | + (when-let [e (first coll)] |
| 3273 | + (cons e (coll-dedupe (rest coll) e)))))))) |
3267 | 3274 |
|
3268 | 3275 | (defn flatten |
3269 | 3276 | "Flatten any combination of nested sequences (such as lists or vectors) into a single |
|
3281 | 3288 | (defn take-last |
3282 | 3289 | "Return the last ``n`` items in ``coll`` in linear time." |
3283 | 3290 | [n coll] |
3284 | | - (loop [c (seq coll) |
3285 | | - rem (seq (drop n coll))] |
3286 | | - (if rem |
3287 | | - (recur (next c) (next rem)) |
3288 | | - c))) |
| 3291 | + (let [coll (seq coll)] |
| 3292 | + (loop [c coll |
| 3293 | + rem (seq (drop n coll))] |
| 3294 | + (if rem |
| 3295 | + (recur (next c) (next rem)) |
| 3296 | + c)))) |
3289 | 3297 |
|
3290 | 3298 | (defn min-key |
3291 | 3299 | "Return the arg for which ``(k arg)`` is the smallest number. If multiple values |
|
0 commit comments