Back

split (clj)

(source)

function

(split s re) (split s re limit)
Splits string on a regular expression. Optional argument limit is the maximum number of parts. Not lazy. Returns vector of the parts. Trailing empty strings are not returned - pass limit of -1 to return all.

Examples

clojure
(ns clojure.test-clojure.string
  (:require [clojure.string :as s])
  (:use clojure.test))

(deftest t-split
  (is (= ["a" "b"] (s/split "a-b" #"-")))
  (is (= ["a" "b-c"] (s/split "a-b-c" #"-" 2)))
  (is (vector? (s/split "abc" #"-"))))

(deftest nil-handling
  (are [f args] (thrown? NullPointerException (apply f args))
       s/reverse [nil]
       s/replace [nil #"foo" "bar"]
       s/replace-first [nil #"foo" "bar"]
       s/re-quote-replacement [nil]
       s/capitalize [nil]
       s/upper-case [nil]
       s/lower-case [nil]
       s/split [nil #"-"]
       s/split [nil #"-" 1]
       s/trim [nil]
       s/triml [nil]
       s/trimr [nil]
       s/trim-newline [nil]))

(deftest char-sequence-handling
  (are [result f args] (let [[^CharSequence s & more] args]
                         (= result (apply f (StringBuffer. s) more)))
       "paz" s/reverse ["zap"]
       "foo:bar" s/replace ["foo-bar" \- \:]
       "ABC" s/replace ["abc" #"\w" s/upper-case]
       "faa" s/replace ["foo" #"o" (StringBuffer. "a")]
       "baz::quux" s/replace-first ["baz--quux" #"--" "::"]
       "baz::quux" s/replace-first ["baz--quux" (StringBuffer. "--") (StringBuffer. "::")]
       "zim-zam" s/replace-first ["zim zam" #" " (StringBuffer. "-")]
       "\\\\ \\$" s/re-quote-replacement ["\\ $"]
       "Pow" s/capitalize ["POW"]
       "BOOM" s/upper-case ["boom"]
       "whimper" s/lower-case ["whimPER"]
       ["foo" "bar"] s/split ["foo-bar" #"-"]
       "calvino" s/trim ["  calvino  "]
       "calvino  " s/triml ["  calvino  "]
       "  calvino" s/trimr ["  calvino  "]
       "the end" s/trim-newline ["the end\r\n\r\r\n"]
       true s/blank? [" "]
       ["a" "b"] s/split-lines ["a\nb"]
       "fa la la" s/escape ["fo lo lo" {\o \a}]))

(deftest t-split-lines
  (let [result (s/split-lines "one\ntwo\r\nthree")]
    (is (= ["one" "two" "three"] result))
    (is (vector? result)))
  (is (= (list "foo") (s/split-lines "foo"))))
clojure
(ns clojure.test-clojure.repl
  (:use clojure.test
        clojure.repl
        [clojure.test-helper :only [platform-newlines]]
        clojure.test-clojure.repl.example)
  (:require [clojure.string :as str]))

(deftest test-doc
  (testing "with namespaces"
    (is (= "clojure.pprint"
           (second (str/split-lines (with-out-str (doc clojure.pprint)))))))
  (testing "with special cases"
    (is (= (with-out-str (doc catch)) (with-out-str (doc try))))))
clojure
  "(ns autodoc.build-html
  \"This is the namespace that builds the HTML pages themselves.
It is implemented with a number of custom enlive templates.\"
  {:skip-wiki true, :author \"Tom Faulhaber\"}
  (:refer-clojure :exclude [empty complement])
  (:import [java.util.jar JarFile]
           [java.io File FileWriter BufferedWriter StringReader
                    BufferedInputStream BufferedOutputStream
                    ByteArrayOutputStream FileReader FileInputStream]
           [java.util.regex Pattern])
  (:require [clojure.string :as str])
  (:use [net.cgrand.enlive-html :exclude (deftemplate)]
        [clojure.java.io :only (as-file file writer)]
        [clojure.java.shell :only (sh)]
        [clojure.pprint :only (pprint cl-format pprint-ident
                               pprint-logical-block set-pprint-dispatch
                               get-pretty-writer fresh-line)]
        [clojure.data.json :only (pprint-json)]
        [autodoc.collect-info :only (contrib-info)]
        [autodoc.params :only (params expand-classpath)])
  (:use clojure.set clojure.java.io clojure.data clojure.java.browse
        clojure.inspector clojure.zip clojure.stacktrace))")

(deftest test-pprint-calendar
  (let [calendar (doto (java.util.GregorianCalendar. 2014 3 29 14 0 0)
                   (.setTimeZone (java.util.TimeZone/getTimeZone "GMT")))
        calendar-str (with-out-str (pprint calendar))]
    (is (= (str/split-lines calendar-str)
           ["#inst \"2014-04-29T14:00:00.000+00:00\""])
        "calendar object pretty prints")))
status-im/status-mobile
(ns status-im.common.parallax.whitelist
  (:require
    [clojure.string :as string]
    [native-module.core :as native-module]))

(def whitelisted?
  (let [device-type (first (string/split (str device-id) ","))]
    (whitelist device-type)))
functional-koans/clojure-koans
(ns koans.02-strings
  (:require [koan-engine.core :refer :all]
            [clojure.string :as string]))

  "Maybe you want to separate out all your lines"
  (= [__ __ __] (string/split-lines "1\n2\n3"))
nextjournal/clerk
(ns viewer-classes
  {:nextjournal.clerk/visibility {:code :hide}
   :nextjournal.clerk/doc-css-class [:justify-center :bg-slate-200 :dark:bg-slate-900 :py-8 :min-h-screen]}
  (:require [babashka.fs :as fs]
            [nextjournal.clerk :as clerk]
            [clojure.string :as str]))

^{::clerk/viewer clerk/table
  ::clerk/css-class [:max-w-2xl :mx-auto :bg-white :p-4 :rounded-lg :shadow-lg :mt-4]}
(def dataset
  (->> (slurp (if (fs/exists? "/usr/share/dict/words")
                "/usr/share/dict/words"
                "https://gist.githubusercontent.com/wchargin/8927565/raw/d9783627c731268fb2935a731a618aa8e95cf465/words"))
       str/split-lines
       (group-by (comp keyword str/upper-case str first))
       (into (sorted-map))))
bhauman/devcards
(ns devdemos.defcard-api
    (:require
     [devcards.core]
     [om.core :as om :include-macros true]
     [om.dom :as dom :include-macros true]
     [reagent.core :as reagent]
     [clojure.string :as string]
     [sablono.core :as sab :include-macros true]
     [cljs.test :as t :include-macros true :refer-macros [testing is]])
    (:require-macros
     ;; Notice that I am not including the 'devcards.core namespace
     ;; but only the macros. This helps ensure that devcards will only
     ;; be created when the :devcards is set to true in the build config.
     [devcards.core :as dc :refer [defcard defcard-doc noframe-doc deftest dom-node]]))

  ```
  (defcard [\"This\" \"is\" \"a\" \"vector\"])
  ```"
  (string/split "This is a vector" #"\s" ))
mtgred/netrunner
(ns game.cards.hardware
  (:require
   [clojure.set :as set]
   [clojure.string :as str]
   [game.core.access :refer [access-bonus access-card breach-server
                             get-only-card-to-access]]
   [game.core.actions :refer [play-ability]]
   [game.core.board :refer [all-active all-active-installed all-installed]]
   [game.core.card :refer [active? corp? event? facedown? get-card get-counters get-title
                           get-zone hardware? has-subtype? ice? in-deck? in-discard?
                           in-hand? in-scored? installed? program? resource? rezzed?
                           runner? virus-program? faceup?]]
   [game.core.card-defs :refer [card-def]]
   [game.core.cost-fns :refer [all-stealth install-cost
                               rez-additional-cost-bonus rez-cost trash-cost]]
   [game.core.damage :refer [chosen-damage damage damage-prevent
                             enable-runner-damage-choice runner-can-choose-damage?]]
   [game.core.def-helpers :refer [breach-access-bonus defcard offer-jack-out
                                  reorder-choice trash-on-empty get-x-fn]]
   [game.core.drawing :refer [draw]]
   [game.core.effects :refer [register-lingering-effect
                              unregister-effects-for-card unregister-lingering-effects]]
   [game.core.eid :refer [effect-completed make-eid make-result]]
   [game.core.engine :refer [can-trigger? not-used-once? register-events
                             register-once register-suppress resolve-ability trigger-event
                             unregister-floating-events unregister-suppress-by-uuid]]
   [game.core.events :refer [event-count first-event? first-trash? no-event?
                             run-events]]
   [game.core.expose :refer [expose]]
   [game.core.finding :refer [find-card find-latest]]
   [game.core.flags :refer [can-trash? card-flag? in-corp-scored? register-run-flag!
                            zone-locked?]]
   [game.core.gaining :refer [gain-clicks gain-credits lose-clicks
                              lose-credits]]
   [game.core.hand-size :refer [hand-size runner-hand-size+]]
   [game.core.hosting :refer [host]]
   [game.core.ice :refer [all-subs-broken? any-subs-broken? break-sub pump
                          reset-all-ice update-all-ice update-all-icebreakers
                          update-breaker-strength]]
   [game.core.installing :refer [runner-can-install? runner-can-pay-and-install? runner-install]]
   [game.core.link :refer [get-link link+]]
   [game.core.memory :refer [caissa-mu+ mu+ update-mu virus-mu+]]
   [game.core.moving :refer [mill move swap-agendas trash trash-cards]]
   [game.core.optional :refer [get-autoresolve never? set-autoresolve]]
   [game.core.payment :refer [build-cost-string can-pay? cost-value]]
   [game.core.play-instants :refer [play-instant]]
   [game.core.prompts :refer [cancellable clear-wait-prompt]]
   [game.core.props :refer [add-counter add-icon remove-icon]]
   [game.core.revealing :refer [reveal]]
   [game.core.rezzing :refer [derez rez]]
   [game.core.runs :refer [bypass-ice end-run end-run-prevent
                           get-current-encounter jack-out make-run
                           successful-run-replace-breach total-cards-accessed]]
   [game.core.say :refer [system-msg]]
   [game.core.servers :refer [target-server is-central?]]
   [game.core.shuffling :refer [shuffle!]]
   [game.core.tags :refer [gain-tags lose-tags tag-prevent]]
   [game.core.to-string :refer [card-str]]
   [game.core.toasts :refer [toast]]
   [game.core.update :refer [update!]]
   [game.core.virus :refer [count-virus-programs number-of-virus-counters]]
   [game.macros :refer [continue-ability effect msg req wait-for]]
   [game.utils :refer :all]
   [jinteki.utils :refer :all]
   [game.core.set-aside :refer [set-aside get-set-aside]]
   [game.core.sabotage :refer [sabotage-ability]]
   [game.core.mark :refer [identify-mark-ability]]))

(defcard "Şifr"
  (letfn [(index-of [pred coll]
            (some (fn [[idx item]] (when (pred item) idx))
                  (map-indexed vector coll)))
          (gather-pre-sifr-effects [sifr state side eid target targets]
            ;; This is needed because of the stupid ass rulings about how Sifr modifies
            ;; ice strength: Sifr only lowers the ice to 0 at the point it's activated,
            ;; and then other abilities (Sandburg, etc) can raise it back after, which
            ;; is DUMB, so that means we have to on the fly calculate what the strength
            ;; of the ice is at the moment Sifr would affect it.
            (if (card-flag? target :cannot-lower-strength true)
              0
              (->> (:effects @state)
                   (filter #(= :ice-strength (:type %)))
                   (filter #(if-not (:req %)
                              true
                              ((:req %) state side eid (get-card state (:card %)) (cons target targets))))
                   (#(split-at (index-of (fn [item] (same-card? sifr (:card item))) %) %))
                   (first)
                   (mapv #(if-not (fn? (:value %))
                            (:value %)
                            ((:value %) state side eid (get-card state (:card %)) (cons target targets))))
                   (reduce +)
                   (abs))))]
    {:static-abilities [(mu+ 2)]
     :events [{:event :encounter-ice
               :interactive (req true)
               :optional
               {:prompt "Lower your maximum hand size by 1 to reduce the strength of encountered ice to 0?"
                :once :per-turn
                :yes-ability
                {:msg (msg "lower their maximum hand size by 1 and reduce the strength of " (:title current-ice) " to 0")
                 :effect (effect
                           (register-lingering-effect
                             card
                             {:type :hand-size
                              :duration :until-runner-turn-begins
                              :req (req (= :runner side))
                              :value -1})
                           (register-lingering-effect
                             :runner card
                             (let [ice current-ice]
                               {:type :ice-strength
                                :duration :end-of-encounter
                                :req (req (same-card? target ice))
                                :value (req (- (+ (:strength target)
                                                  (gather-pre-sifr-effects card state side eid target (rest targets)))))})))}}}]}))