Back
subvec (clj)
(source)function
(subvec v start)
(subvec v start end)
Returns a persistent vector of the items in vector from
start (inclusive) to end (exclusive). If end is not supplied,
defaults to (count vector). This operation is O(1) and very fast, as
the resulting vector shares structure with the original and no
trimming is done.
Examples
mikera/core.matrix
(ns clojure.core.matrix.impl.test-wrappers
(:require [clojure.core.matrix.protocols :as mp]
[mikera.cljutils.error :refer [error?]]
[clojure.core.matrix.compliance-tester :as compliance]
[clojure.core.matrix :refer :all]
[clojure.core.matrix.impl.wrappers :refer :all]
[clojure.test :refer :all]))
(deftest test-wrapped-subvector
(let [m [1 2 3 4]]
(equals [2 3] (subvector m 1 2))))
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/subvec (t/All [x] [(t/Vec x) t/AnyInteger (t/? t/AnyInteger) :-> (t/Vec x)])
tel/clatrix
(ns clatrix.matrix-api-test
(:use clojure.test
clojure.core.matrix.protocols)
(:require [clatrix.core :as c]
[clojure.core.matrix :as m]
[clojure.core.matrix.protocols :as p]
[clojure.core.matrix.compliance-tester :as comp]))
(deftest regressions
(let [m (m/matrix :clatrix [1 2 3])
wm (clojure.core.matrix.impl.wrappers/wrap-nd m)]
(is (c/clatrix? m))
(is (== 1 (dimensionality m)))
(is (= [3] (seq (m/shape m))))
(is (m/equals m wm))
(is (= [3] (seq (m/shape wm))))
(is (== 1 (m/dimensionality wm)))
(is (p/is-vector? wm))
(is (== 2.0 (m/mget wm 1)))
(is (m/equals [2.0] (m/subvector wm 1 1)))
(is (m/equals [2.0] (m/subvector m 1 1))))
(is (m/equals [1.0 2.0] (m/subvector (m/matrix :clatrix [1 2 3]) 0 2)))
(is (m/equals [1.0 2.0] (m/coerce [] (m/matrix :clatrix [1 2]))))
(is (every? number? (m/slices (m/matrix :clatrix '(1 2 3)))))
(is (= [1.0 2.0 3.0] (m/eseq (m/matrix :clatrix '(1 2 3)))))
(is (= [1.0 2.0 3.0] (m/slices (m/matrix :clatrix [1 2 3]))))
(is (= [1.0 2.0 3.0 4.0] (m/eseq (m/matrix :clatrix [[1 2] [3 4]]))))
(is (m/equals [1 2 3] (m/matrix :clatrix [1 2 3]))))
mikera/timeline
(ns mikera.timeline.impl
(:use [mikera.timeline protocols utils])
(:require [clojure.core.rrb-vector :as fv])
(:import [mikera.timeline ISeekIndex])
(:import [org.joda.time Instant DateTimeUtils]))
(deftype Timeline
[times
values]
ISeekIndex
(seek [tl time]
(let [time (long time)
n (long (count values))]
(long
(loop [i (long 0) j n]
(if (>= i j)
(dec i)
(let [mi (long (quot (+ i j) 2))
moff (long (nth times mi))]
(if (>= time moff)
(recur (inc mi) j)
(recur i mi))))))))
clojure.lang.IFn
(invoke [tl time]
(.nth values (.seek tl (long-time time))))
clojure.lang.Counted
(count [tl]
(int (count values)))
clojure.lang.Indexed
(nth [tl i]
[(Instant. (nth times i)) (nth values i)])
(nth [tl i default]
(if (and (>= i 0) (< i (count values)))
[(Instant. (nth times i)) (nth values i)]
default))
;; TODO: Figure out if these should be Map entries?
clojure.lang.Seqable
(seq [tl]
(seq (for [i (range (count values))]
(clojure.lang.MapEntry. (Instant. (long (nth times i))) (nth values i)))))
Object
(toString [tl]
(str (vec (seq tl))))
PTimeline
(value-at [tl time]
(let [time (long time)
index (seek-index tl time)]
(if index
(nth values index)
nil)))
(seek-index [tl time]
(let [time (long time)
ix (.seek tl time)]
(if (>= ix 0) ix nil)))
(event-value [tl index]
(nth values index))
(event-time [tl index]
(nth times index))
(slice-indexes
[tl start-index end-index]
(Timeline. (fv/subvec times start-index end-index)
(fv/subvec values start-index end-index)))
(add-event [tl time value]
(let [ix (inc (.seek tl time))
n (count values)]
(Timeline. (fv/catvec (conj (fv/subvec times 0 ix) time) (fv/subvec times ix n))
(fv/catvec (conj (fv/subvec values 0 ix) value) (fv/subvec values ix n))))))