Skip to content

Instantly share code, notes, and snippets.

@PEZ
Last active November 28, 2023 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PEZ/4f5ffbf020d90a968db4f99478cf358e to your computer and use it in GitHub Desktop.
Save PEZ/4f5ffbf020d90a968db4f99478cf358e to your computer and use it in GitHub Desktop.
Interpose a Seq – Rich 4Clojure Problem 40 – See: https://github.com/PEZ/rich4clojure
(ns rich4clojure.easy.problem-040
(:require [hyperfiddle.rcf :refer [tests]]))
;; = Interpose a Seq =
;; By 4Clojure user: dbyrne
;; Difficulty: Easy
;; Tags: [seqs core-functions]
;;
;; Write a function which separates the items of a
;; sequence by an arbitrary value.
(def restricted [interpose])
(def __ :tests-will-fail)
(comment
)
(tests
(__ 0 [1 2 3]) := [1 0 2 0 3]
(apply str (__ ", " ["one" "two" "three"])) := "one, two, three"
(__ :z [:a :b :c :d]) := [:a :z :b :z :c :z :d])
;; To participate, fork:
;; https://github.com/PEZ/rich4clojure
;; Post your solution below, please!
@chenj7
Copy link

chenj7 commented Sep 8, 2021

(def __ #(butlast (mapcat list %2 (repeat %))))

@szalai1
Copy link

szalai1 commented Sep 26, 2021

(def __ (fn [sep l]
            (butlast (mapcat #(vector %1 sep) l))))

@blogscot
Copy link

(defn __ [item coll]
  (butlast (interleave coll (repeat item))))

@status203
Copy link

(defn my-interpose
  [v [fst & rst]]
  (lazy-seq
   (if rst
     (cons fst (cons v (my-interpose v rst)))
     (list fst))))

(def __ my-interpose)

@StanTheBear
Copy link

(fn [inter-val seq1]
(take
(dec (* 2 (count seq1)))
(mapcat list
seq1
(repeat (count seq1) inter-val)))) ; I didnt know about butlast

@Herrmelit
Copy link

  (defn __ [val my-list]
    (drop-last (mapcat #(concat  [%] [val]) my-list)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment