Last active
November 28, 2023 16:09
-
-
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
This file contains 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-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
commented
Sep 8, 2021
(def __ (fn [sep l]
(butlast (mapcat #(vector %1 sep) l))))
(defn __ [item coll]
(butlast (interleave coll (repeat item))))
(defn my-interpose
[v [fst & rst]]
(lazy-seq
(if rst
(cons fst (cons v (my-interpose v rst)))
(list fst))))
(def __ my-interpose)
(fn [inter-val seq1]
(take
(dec (* 2 (count seq1)))
(mapcat list
seq1
(repeat (count seq1) inter-val)))) ; I didnt know about butlast
(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