Skip to content

Instantly share code, notes, and snippets.

@PEZ
Last active October 31, 2024 05:40
Show Gist options
  • Select an option

  • Save PEZ/65d3ee0ffa567e78927bbebbb9d9cc89 to your computer and use it in GitHub Desktop.

Select an option

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
(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
Copy link

szalai1 commented Sep 26, 2021

(def __ #(loop [a %1
               b %2
               res []]
          (if (or (empty? a) (empty? b))
            res
            (recur (rest a)
                   (rest b)
                   (conj (conj res (first a)) (first b))))))

@status203
Copy link

(defn my-interleave
  [[frst1 & rst1] [frst2 & rst2]]
  (when (and frst1 frst2)
    (lazy-seq
     (concat [frst1 frst2] (my-interleave rst1 rst2)))))

(def __ my-interleave)

@calherries
Copy link

calherries commented Apr 11, 2022

(defn __ [xs ys]
  (mapcat vector xs ys))

@StanTheBear
Copy link

(fn [seq1 seq2]
(mapcat #(list %1 %2)
seq1 seq2) )

@esciafardini
Copy link

esciafardini commented Oct 29, 2022

(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

@oezg
Copy link

oezg commented Oct 30, 2024

(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