Skip to content

Instantly share code, notes, and snippets.

@PEZ
Last active January 20, 2023 01:05
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/efa257369fd08ba1dcef6bec34d04ede to your computer and use it in GitHub Desktop.
Save PEZ/efa257369fd08ba1dcef6bec34d04ede to your computer and use it in GitHub Desktop.
Indexing Sequences – Rich 4Clojure Problem 157 – See: https://github.com/PEZ/rich4clojure
(ns rich4clojure.easy.problem-157
(:require [hyperfiddle.rcf :refer [tests]]))
;; = Indexing Sequences =
;; By 4Clojure user: jaycfields
;; Difficulty: Easy
;; Tags: [seqs]
;;
;; Transform a sequence into a sequence of pairs
;; containing the original elements along with their
;; index.
(def __ :tests-will-fail)
(comment
)
(tests
(__ [:a :b :c]) := [[:a 0] [:b 1] [:c 2]]
(__ [0 1 3]) := '((0 0) (1 1) (3 2))
(__ [[:foo] {:bar :baz}]) := [[[:foo] 0] [{:bar :baz} 1]])
;; To participate, fork:
;; https://github.com/PEZ/rich4clojure
;; Post your solution below, please!
@blogscot
Copy link

(defn pairs* [coll]
  (map (fn [a b] [a b]) coll (range)))

@StanTheBear
Copy link

(def __ (fn [seq-in]
          (partition 2 (interleave seq-in (iterate inc 0)))))

@StanTheBear
Copy link

Shush as blogscot wrote - range rather than (iterate inc 0) doh!

(def __ (fn [seq-in]
          (partition 2 (interleave seq-in (range)))))

@StanTheBear
Copy link

(defn pairs* [coll]
  (map (fn [a b] [a b]) coll (range)))

Shush as blogscot wrote - range rather than (iterate inc 0) doh!


(def __ (fn [seq-in]
          (partition 2 (interleave seq-in (range)))))

@onstop4
Copy link

onstop4 commented Jan 20, 2023

(def __ (fn [s]
          (map vector s (range))))

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