Public Vars

Back

random-sample (clj)

(source)

function

(random-sample prob) (random-sample prob coll)
Returns items from coll with random probability of prob (0.0 - 1.0). Returns a transducer when no collection is provided.

Examples

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)]
                               ))
Netflix/mantis-mql
;
; Copyright 2022 Netflix, Inc.
;
; Licensed under the Apache License, Version 2.0 (the "License");
; you may not use this file except in compliance with the License.
; You may obtain a copy of the License at
;
;     http://www.apache.org/licenses/LICENSE-2.0
;
; Unless required by applicable law or agreed to in writing, software
; distributed under the License is distributed on an "AS IS" BASIS,
; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
; See the License for the specific language governing permissions and
; limitations under the License.
;
(ns io.mantisrx.mql.test-sample
  ""
  (:require [clojure.test :refer :all]
            [io.mantisrx.mql.jvm.core :as mql]
            [io.mantisrx.mql.jvm.interfaces.server :as mql-server]
            [io.mantisrx.mql.jvm.interfaces.core :as mqli]
            [rx.lang.clojure.core :as rx]
            [rx.lang.clojure.blocking :as rxb])
  (:import rx.Observable java.util.concurrent.TimeUnit))

(deftest test-random-sampler
  (testing ""
    (let
      [q "select * from stream sample {'strategy': 'RANDOM', 'threshold': 1}"
       n 100000
       percent (/ 1 10000)
       data (repeat n {"a" 1})
       context {"stream" (Observable/from data)}
       result (rxb/into [] (mql/eval-mql q context))
       ]
      (is (> (* (/ 2 10000) n) (count result)))
      (is (< (* (/ 0 10000) n) (count result))))))