Public Vars

Back

rand-nth (clj)

(source)

function

(rand-nth coll)
Return a random element of the (sequential) collection. Will have the same performance characteristics as nth for the given collection.

Examples

danielsz/system
(ns system.components.core-async-pubsub-test
  (:require [system.components.core-async-pubsub :refer [new-pubsub]]
            [com.stuartsierra.component :as component]
            [clojure.core.async :as a :refer [<!! >!! >! <! chan thread timeout]]
            [clojure.test :refer [deftest is]]))

(deftest pubsub
  (let [topic-fn :topic
        channel-fn (fn [_] (let [c (chan)]
                            (thread
                              (loop []
                                (>!! c {:topic :president :man (rand-nth presidents)})
                                (>!! c {:topic :scientist :woman (rand-nth scientists)})
                                (<!! (timeout 100))
                                (recur)))
                            c))
        pubsub (component/start (new-pubsub channel-fn topic-fn))
        president-subscriber (let [c (chan)
                                   o (a/take 5 c)]
                               (a/sub (:publication pubsub) :president c)
                               o)
        scientist-subscriber (let [c (chan)
                                   o (a/take 5 c)]
                               (a/sub (:publication pubsub) :scientist c)
                               o)]
    (dotimes [n 5]
      (let [{:keys [topic man]} (<!! president-subscriber)
            {:keys [topic woman]} (<!! scientist-subscriber)]
        (is (some #{man} presidents))
        (is (some #{woman} scientists))))
    (component/stop pubsub)))
PrecursorApp/precursor
(ns pc.http.routes.api
  (:require [cemerick.url :as url]
            [cheshire.core :as json]
            [clojure.core.memoize :as memo]
            [clojure.string :as str]
            [clojure.tools.reader.edn :as edn]
            [crypto.equality :as crypto]
            [defpage.core :as defpage :refer (defpage)]
            [pc.auth :as auth]
            [pc.crm :as crm]
            [pc.datomic :as pcd]
            [pc.early-access]
            [pc.http.doc :as doc-http]
            [pc.http.team :as team-http]
            [pc.http.handlers.custom-domain :as custom-domain]
            [pc.models.chat-bot :as chat-bot-model]
            [pc.models.doc :as doc-model]
            [pc.models.flag :as flag-model]
            [pc.models.team :as team-model]
            [pc.profile :as profile]
            [ring.middleware.anti-forgery :as csrf]
            [slingshot.slingshot :refer (try+ throw+)]))

(defpage new [:post "/api/v1/document/new"] [req]
  (let [params (some-> req :body slurp edn/read-string)
        read-only? (:read-only params)
        doc-name (:document/name params)]
    (if-not (:subdomain req)
      (let [cust-uuid (get-in req [:auth :cust :cust/uuid])
            intro-layers? (:intro-layers? params)
            doc (doc-model/create-public-doc!
                 (merge {:document/chat-bot (rand-nth chat-bot-model/chat-bots)}
                        (when cust-uuid {:document/creator cust-uuid})
                        (when read-only? {:document/privacy :document.privacy/read-only})
                        (when doc-name {:document/name doc-name})))]
        (when intro-layers?
          (doc-http/add-intro-layers doc))
        {:status 200 :body (pr-str {:document (doc-model/read-api doc)})})
      (if (and (:team req)
               (auth/logged-in? req)
               (auth/has-team-permission? (pcd/default-db) (:team req) (:auth req) :admin))
        (let [doc (doc-model/create-team-doc!
                   (:team req)
                   (merge {:document/chat-bot (rand-nth chat-bot-model/chat-bots)}
                          (when-let [cust-uuid (get-in req [:cust :cust/uuid])]
                            {:document/creator cust-uuid})
                          (when read-only?
                            {:document/privacy :document.privacy/read-only})
                          (when doc-name
                            {:document/name doc-name})))]
          {:status 200 :body (pr-str {:document (doc-model/read-api doc)})})
        {:status 400 :body (pr-str {:error :unauthorized-to-team
                                    :redirect-url (str (url/map->URL {:host (profile/hostname)
                                                                      :protocol (if (profile/force-ssl?)
                                                                                  "https"
                                                                                  (name (:scheme req)))
                                                                      :port (if (profile/force-ssl?)
                                                                              (profile/https-port)
                                                                              (profile/http-port))
                                                                      :path "/new"
                                                                      :query (:query-string req)}))
                                    :msg "You're unauthorized to make documents in this subdomain. Please request access."})}))))
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/eval [t/Any :-> t/Any]
cc/rand-nth (t/All [x] [(t/U (t/Indexed x) (t/SequentialSeqable x)) :-> x])
nubank/nodely
(ns nodely.engine.applicative-test
  (:require
   [clojure.core.async :as async]
   [clojure.test :refer :all]
   [clojure.test.check.clojure-test :refer [defspec]]
   [clojure.test.check.properties :as prop]
   [criterium.core :as criterium]
   [matcher-combinators.matchers :as matchers]
   [matcher-combinators.test :refer [match?]]
   [nodely.data :as data]
   [nodely.engine.applicative :as applicative]
   [nodely.engine.applicative.core-async :as core-async]
   [nodely.engine.applicative.promesa :as promesa]
   [nodely.engine.applicative.synchronous :as synchronous]
   [nodely.engine.core :as core]
   [nodely.engine.core-async.core :as nodely.async]
   [nodely.engine.schema :as schema]
   [nodely.fixtures :as fixtures]
   [nodely.syntax :as syntax :refer [>leaf >value]]
   [nodely.syntax.schema :refer [yielding-schema]]
   [promesa.core :as p]
   [schema.core :as s]))

(defspec does-not-blow-up-spec
  (prop/for-all [env (fixtures/env-gen {})]
                (applicative/eval-key env
                                      (rand-nth (keys env))
                                      {::applicative/context core-async/context})
                true))
nubank/nodely
(ns nodely.engine.core-async.lazy-scheduling-test
  (:refer-clojure :exclude [eval async])
  (:require
   [clojure.core.async :as async]
   [clojure.test :refer :all]
   [clojure.test.check.clojure-test :refer [defspec]]
   [clojure.test.check.properties :as prop]
   [criterium.core :refer [time-body]]
   [matcher-combinators.matchers :as matchers]
   [matcher-combinators.test :refer [match? thrown-match?]]
   [nodely.data :as data]
   [nodely.engine.core :as core]
   [nodely.engine.core-async.core :as nodely.async]
   [nodely.engine.core-async.lazy-scheduling :as nasync]
   [nodely.engine.lazy :as engine.lazy]
   [nodely.fixtures :as fixtures]
   [nodely.syntax :as syntax :refer [>cond >leaf >value blocking]]))

(defspec does-not-blow-up-spec
  (prop/for-all [env (fixtures/env-gen {})]
                (nasync/eval-key env (rand-nth (keys env)))
                true))