Last active
October 31, 2024 05:40
-
-
Save PEZ/65d3ee0ffa567e78927bbebbb9d9cc89 to your computer and use it in GitHub Desktop.
Interleave Two Seqs – Rich 4Clojure Problem 39 – See: https://github.com/PEZ/rich4clojure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (ns rich4clojure.easy.problem-039 | |
| (:require [hyperfiddle.rcf :refer [tests]])) | |
| ;; = Interleave Two Seqs = | |
| ;; By 4Clojure user: dbyrne | |
| ;; Difficulty: Easy | |
| ;; Tags: [seqs core-functions] | |
| ;; | |
| ;; Write a function which takes two sequences and returns | |
| ;; the first item from each, then the second item from | |
| ;; each, then the third, etc. | |
| (def restricted [interleave]) | |
| (def __ :tests-will-fail) | |
| (comment | |
| ) | |
| (tests | |
| (__ [1 2 3] [:a :b :c]) := '(1 :a 2 :b 3 :c) | |
| (__ [1 2] [3 4 5 6]) := '(1 3 2 4) | |
| (__ [1 2 3 4] [5]) := [1 5] | |
| (__ [30 20] [25 15]) := [30 25 20 15]) | |
| ;; To participate, fork: | |
| ;; https://github.com/PEZ/rich4clojure | |
| ;; Post your solution below, please! |
szalai1
commented
Sep 26, 2021
(defn my-interleave
[[frst1 & rst1] [frst2 & rst2]]
(when (and frst1 frst2)
(lazy-seq
(concat [frst1 frst2] (my-interleave rst1 rst2)))))
(def __ my-interleave)(defn __ [xs ys]
(mapcat vector xs ys))
(fn [seq1 seq2]
(mapcat #(list %1 %2)
seq1 seq2) )
(def __
(fn [& colls]
(loop [cs colls
acc []]
(if
;; are any of the colls empty?
(seq (filter (fn [c] (not (seq c))) cs))
acc
(recur (map rest cs) (into acc (map first cs)))))))defined to handle & colls, rather than just 2 colls
(defn __
([c1 c2] (__ [] c1 c2))
([acc [h1 & t1] [h2 & t2]]
(if (or (nil? h1) (nil? h2))
acc
(recur (conj acc h1 h2) t1 t2))))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment