Back
map-indexed (clj)
(source)function
(map-indexed f)
(map-indexed f coll)
Returns a lazy sequence consisting of the result of applying f to 0
and the first item of coll, followed by applying f to 1 and the second
item in coll, etc, until coll is exhausted. Thus function f should
accept 2 arguments, index and item. Returns a stateful transducer when
no collection is provided.
Examples
mikera/core.matrix
WARNING: because they lack efficient indexed access, sequences will perform badly for most
array operations. In general they should be converted to other implementations before use."
(:require [clojure.core.matrix.protocols :as mp]
[clojure.core.matrix.implementations :as imp]
#?(:clj [clojure.core.matrix.macros :refer [scalar-coerce error]]))
#?(:clj (:import [clojure.lang ISeq])
:cljs (:require-macros [clojure.core.matrix.macros :refer [scalar-coerce error]])))
(extend-protocol mp/PMapIndexed
ISeq
(element-map-indexed
([ms f]
(mapv (fn [i m] (mp/element-map-indexed m #(apply f (cons i %1) %&)))
(range (count ms)) ms))
([ms f as]
(let [[ms as] (mp/broadcast-compatible ms as)]
(mapv (fn [i m a]
(mp/element-map-indexed m #(apply f (cons i %1) %&) a))
(range (count ms)) ms (mp/get-major-slice-seq as))))
([ms f as more]
(let [[ms as & more] (apply mp/broadcast-compatible ms as more)] ; FIXME
(mapv (fn [i m a & mr]
(mp/element-map-indexed m #(apply f (cons i %1) %&) a mr))
(range (count ms)) ms
(mp/get-major-slice-seq as)
(map mp/get-major-slice-seq more)))))
(element-map-indexed!
([m f]
(error "Sequence arrays are not mutable!"))
([m f a]
(error "Sequence arrays are not mutable!"))
([m f a more]
(error "Sequence arrays are not mutable!"))))
mikera/core.matrix
(ns clojure.core.matrix.test-double-array
(:refer-clojure :exclude [==])
(:require [clojure.core.matrix.protocols :as mp]
[clojure.core.matrix.impl.pprint :as pprint]
[clojure.core.matrix.dataset :as ds]
[clojure.core.matrix.compliance-tester]
[clojure.core.matrix :refer :all]
[clojure.core.matrix.operators :refer [==]]
[clojure.core.matrix.macros :refer [error]]
[clojure.core.matrix.macros-clj :refer [error?]]
[clojure.test :refer :all]))
(deftest test-functional-ops
(testing "mapping"
(let [da (matrix :double-array [1 2])]
(is (= [2.0 3.0] (seq (emap inc da))))
(emap! inc da)
(is (= [2.0 3.0] (vec da)))))
(testing "nested double arrays"
(is (= [1.0 2.0 3.0 4.0] (eseq [(double-array [1 2]) (double-array [3 4])]))))
(testing "mapping indexed"
(let [da (matrix :double-array [4 5])
da2 (matrix :double-array [6 7])
da3 (matrix :double-array [8 9])]
(is (= [5.0 7.0] (seq (emap-indexed #(+ (reduce + %1) (inc %2)) da))))
(is (= [10.0 13.0] (seq (emap-indexed #(apply + (reduce + %1) %&) da da2))))
(is (= [18.0 22.0] (seq (emap-indexed #(apply + (reduce + %1) %&) da da2 da3)))))))
typedclojure/typedclojure
(ns ^:no-doc typed.clj.ext.clojure.core__doseq
"Typing rules clojure.core/doseq"
(:require [clojure.core.typed.contract-utils :as con]
[clojure.core.typed.errors :as err]
[typed.clj.checker.check :refer [check-expr]]
[typed.clj.checker.parse-unparse :as prs]
[typed.clj.analyzer.passes.emit-form :as emit-form]
[typed.clj.analyzer.utils :as ana-utils]
[typed.clj.ext.clojure.core__for :as ext-for]
[typed.clj.ext.clojure.core__let :as ext-let]
[typed.cljc.analyzer :as ana2]
[typed.cljc.checker.check.let :as let]
[typed.cljc.checker.check-below :as below]
[typed.cljc.checker.check.if :as if]
[typed.cljc.checker.filter-ops :as fo]
[typed.cljc.checker.lex-env :as lex]
[typed.cljc.checker.var-env :as var-env]
[typed.cljc.checker.type-rep :as r]
[typed.cljc.checker.type-ctors :as c]
[typed.cljc.checker.cs-gen :as cgen]
[typed.cljc.checker.utils :as u]
[typed.cljc.checker.check.unanalyzed :refer [defuspecial]]))
(defuspecial defuspecial__doseq
"defuspecial implementation for clojure.core/doseq"
[{ana-env :env :keys [form] :as expr} expected]
(let [_ (assert (<= 2 (count form)) form)
[args-syn & body-syns] (next form)
{:keys [new-syms expanded-bindings prop-env ana-env reachable]}
(ext-for/check-list-comprehension-binder
{:form form
:args-syn args-syn
:ana-env ana-env
:prop-env (lex/lexical-env)})
expr (-> expr
(update :form
(fn [form]
(-> (map-indexed
(fn [i args-syn]
;; add back short-circuited args
(if (= 1 i)
expanded-bindings
args-syn))
form)
(with-meta (meta form))))))]
(if-not reachable
(assoc expr
u/expr-type (below/maybe-check-below
(r/ret r/-nil
(fo/-false-filter))
expected))
(let [cbody (var-env/with-lexical-env prop-env
(-> `(do ~@body-syns)
(ana2/unanalyzed ana-env)
check-expr))
expr (-> expr
(update :form
(fn [form]
(-> form
vec
(subvec 0 2)
(conj (emit-form/emit-form cbody))
list*
(with-meta (meta form))))))]
(assoc expr
u/expr-type (below/maybe-check-below
(r/ret r/-nil
(fo/-false-filter))
expected))))))
typedclojure/typedclojure
(ns ^:no-doc typed.ann.clojure
"Type annotations for the base Clojure distribution."
#?(:cljs (:require-macros [typed.ann-macros.clojure :as macros]))
(:require [clojure.core :as cc]
[typed.clojure :as t]
#?(:clj [typed.ann-macros.clojure :as macros])
#?(:clj typed.ann.clojure.jvm) ;; jvm annotations
#?(:clj clojure.core.typed))
#?(:clj
(:import (clojure.lang PersistentHashSet PersistentList
APersistentMap #_IPersistentCollection
#_ITransientSet
IRef)
(java.util Comparator Collection))))
cc/map-indexed (t/All [x y] (t/IFn [[t/Int x :-> y] :-> (t/Transducer x y)]
[[t/Int x :-> y] (t/Seqable x) :-> (t/ASeq y)]))
cc/keep-indexed (t/All [a c] (t/IFn [[t/Int a :-> (t/U nil c)] :-> (t/Transducer a c)]
[[t/Int a :-> (t/U nil c)] (t/Seqable a) :-> (t/Seq c)]))
cc/bounded-count [(t/U t/Counted t/AnySeqable) :-> t/Int]
cc/keep (t/All [a b] (t/IFn [[a :-> (t/Option b)] :-> (t/Transducer a b)]
[[a :-> (t/Option b)] (t/Seqable a) :-> (t/ASeq b)]))
cc/dedupe (t/All [x] (t/IFn [:-> (t/Transducer x x)]
[(t/Seqable x) :-> (t/ASeq x)]))
cc/random-sample (t/All [x] (t/IFn [t/Num :-> (t/Transducer x x)]
[t/Num (t/Seqable x) :-> (t/ASeq x)]))
cc/halt-when (t/All [x] (t/IFn [[x :-> t/Any]
;; TODO opens a can of worms. requires knowledge of transduction context.
;; overrides final value of `into`, while doing nothing in `sequence`.
;(t/Option [t/Any v :-> t/Any]) :?
:-> (t/Transducer x x)]
))
damballa/parkour
(ns parkour.reducers-test
(:require [clojure.core.reducers :as r]
[parkour (reducers :as pr)]
[clojure.test :refer :all]))
(deftest test-map-indexed
(is (= [[0 :foo] [1 :bar] [2 :baz]]
(->> [:foo :bar :baz]
(pr/map-indexed vector)
(into [])))))