Public Vars

Back

nthnext (clj)

(source)

function

(nthnext coll n)
Returns the nth next of coll, (seq coll) when n is 0.

Examples

clojure/core.typed
(ns clojure.core.typed.test.cljs-core
    (:require [cljs.core.typed :as t]
              [cljs.core :as core]
              [clojure.core.typed.test.cljs-utils :refer [is-tc-e tc-e]]
              [clojure.test :refer :all]))

(add-test nthnext
    [nil 8] nil
    [#{2 3 1} 1] (t/Option (t/NonEmptyASeq t/CLJSInteger))
    [[1 2 3]  9] (t/Option (t/NonEmptyASeq t/CLJSInteger)))
clojure/core.typed
(ns clojure.core.typed.test.nthnext-test
  (:refer-clojure :exclude [fn])
  (:require [clojure.core.typed.test.test-utils :refer :all]
            [clojure.test :refer :all]
            [clojure.core.typed :as t :refer [ann-form print-env fn]]))

(deftest nthnext-test
  (is-tc-e
   (fn [stmt :- '[Any Long]]
     (let [body (nthnext stmt 1)]
       (ann-form body (HSeq [Long])))))

  (is-tc-e
   (ann-form (nthnext nil 1) nil)))

(deftest nthnext-test-input-types
  (testing "HVec"
    (is-tc-e
     (fn [stmt :- (t/HVec [Any Long])]
       (let [body (nthnext stmt 1)]
         (ann-form body (HSeq [Long]))))))

  (testing "HSeq"
    (is-tc-e
     (fn [stmt :- (t/HSeq [Any Long])]
       (let [body (nthnext stmt 1)]
         (ann-form body (HSeq [Long]))))))

  (testing "HSequential"
    (is-tc-e
     (fn [stmt :- (t/HSequential [Any Long])]
       (let [body (nthnext stmt 1)]
         (ann-form body (HSeq [Long])))))))

(deftest nthnext-test-fixed-types
  (testing "skipping past all the fixed types"
    (is-tc-e
     (fn [stmt :- '[Any Long]]
       (let [body (nthnext stmt 100)]
         (ann-form body nil))))
    (is-tc-err
     (fn [stmt :- '[Any Long]]
       (let [body (nthnext stmt 100)]
         (ann-form body (HSeq [])))))))

(deftest nthnext-test-fixed-types-and-rest
  (testing "skipping past all the fixed types with a rest type"
    (is-tc-e
     (fn [stmt :- '[Any Long *]]
       (let [body (nthnext stmt 100)]
         (ann-form body (t/Option (HSeq [Long *]))))))
    (is-tc-err
     (fn [stmt :- '[Any Long *]]
       (let [body (nthnext stmt 100)]
         (ann-form body (HSeq [Long *])))))
    (is-tc-err
     (fn [stmt :- '[Any Long *]]
       (let [body (nthnext stmt 100)]
         (ann-form body nil))))))

(deftest nthnext-test-union
  (testing "a union of types is also refined"
    (is-tc-e
     (fn [stmt :- (U '[Any String]
                     '[Any Long])]
       (let [body (nthnext stmt 1)]
         (ann-form body (U (HSeq [String])
                           (HSeq [Long]))))))))

(deftest nthnext-test-destructuring
  (testing "implicitly used via destructuring"
    (is-tc-e
     (fn [stmt :- '[Any Long]]
       (let [[_ & body] stmt]
         (ann-form body (HSeq [Long]))))))
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/first (t/All [x] (t/IFn [(t/HSequential [x t/Any :*]) :-> x
                            :object {:id 0 :path [(Nth 0)]}]
                           [(t/EmptySeqable x) :-> nil]
                           [(t/NonEmptySeqable x) :-> x]
                           [(t/Seqable x) :-> (t/Option x)]))
cc/second (t/All [x] (t/IFn [(t/HSequential [t/Any x t/Any :*]) :-> x
                             :object {:id 0 :path [(Nth 1)]}]
                            [(t/Option (t/I (t/Seqable x) (t/CountRange 0 1))) :-> nil]
                            [(t/I (t/Seqable x) (t/CountRange 2)) :-> x]
                            [(t/Seqable x) :-> (t/Option x)]))
cc/ffirst (t/All [x] [(t/Seqable (t/Seqable x)) :-> (t/Nilable x)])
cc/nfirst (t/All [x] [(t/Seqable (t/Seqable x)) :-> (t/NilableNonEmptyASeq x)])
cc/group-by (t/All [x y] [[x :-> y] (t/Seqable x) :-> (t/Map y (t/NonEmptyAVec x))])
cc/fnext (t/All [x] [(t/Seqable x) :-> (t/Option x)])
cc/nnext (t/All [x] [(t/Seqable x) :-> (t/NilableNonEmptyASeq x)])
cc/nthnext (t/All [x] (t/IFn [nil t/AnyInteger :-> nil]
                             [(t/Seqable x) t/AnyInteger :-> (t/NilableNonEmptyASeq x)]))
cc/rest (t/All [x] [(t/Seqable x) :-> (t/ASeq x)])
cc/last (t/All [x] (t/IFn [(t/NonEmptySeqable x) :-> x]
                          [(t/Seqable x) :-> (t/U nil x)]))
cc/butlast (t/All [x] [(t/Seqable x) :-> (t/NilableNonEmptyASeq x)])
cc/next (t/All [x] (t/IFn [(t/Option (t/Coll x)) :-> (t/NilableNonEmptyASeq x)
                           :filters {:then (& (is (t/CountRange 2) 0)
                                              (! nil 0))
                                     :else (| (is (t/CountRange 0 1) 0)
                                              (is nil 0))}]
                          [(t/Seqable x) :-> (t/NilableNonEmptyASeq x)]))