Back
index-of (clj)
(source)function
(index-of s value)
(index-of s value from-index)
Return index of value (string or char) in s, optionally searching
forward from from-index. Return nil if value not found.
Examples
clojure
(ns clojure.test-clojure.string
(:require [clojure.string :as s])
(:use clojure.test))
(deftest t-index-of
(let [sb (StringBuffer. "tacos")]
(is (= 2 (s/index-of sb "c")))
(is (= 2 (s/index-of sb \c)))
(is (= 1 (s/index-of sb "ac")))
(is (= 3 (s/index-of sb "o" 2)))
(is (= 3 (s/index-of sb \o 2)))
(is (= 3 (s/index-of sb "o" -100)))
(is (= nil (s/index-of sb "z")))
(is (= nil (s/index-of sb \z)))
(is (= nil (s/index-of sb "z" 2)))
(is (= nil (s/index-of sb \z 2)))
(is (= nil (s/index-of sb "z" 100))
(is (= nil (s/index-of sb "z" -10))))))
(deftest t-last-index-of
(let [sb (StringBuffer. "banana")]
(is (= 4 (s/last-index-of sb "n")))
(is (= 4 (s/last-index-of sb \n)))
(is (= 3 (s/last-index-of sb "an")))
(is (= 4 (s/last-index-of sb "n" )))
(is (= 4 (s/last-index-of sb "n" 5)))
(is (= 4 (s/last-index-of sb \n 5)))
(is (= 4 (s/last-index-of sb "n" 500)))
(is (= nil (s/last-index-of sb "z")))
(is (= nil (s/last-index-of sb "z" 1)))
(is (= nil (s/last-index-of sb \z 1)))
(is (= nil (s/last-index-of sb "z" 100))
(is (= nil (s/last-index-of sb "z" -10))))))
functional-koans/clojure-koans
(ns koans.02-strings
(:require [koan-engine.core :refer :all]
[clojure.string :as string]))
"Maybe you want to find the index of the first occurrence of a substring"
(= 0 (string/index-of "hello world" __))
"Or maybe the last index of the same substring"
(= __ (string/last-index-of "hello world, hello" "hello"))
"But when something doesn't exist, nothing is found"
(= __ (string/index-of "hello world" "bob"))
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)))))})))}}}]}))
lspector/Clojush
(ns clojush.instructions.string
(:use [clojush pushstate globals]
[clojure.string :only [split trim]]
clojush.instructions.vectors)
(:require [clojure.string :as string]))
(define-registered
string_indexof
^{:stack-types [:string :integer]}
(fn [state]
(if (empty? (rest (:string state)))
state
(let [stri (stack-ref :string 1 state)
substr (stack-ref :string 0 state)]
(if (= (string/index-of stri substr) nil)
state
(->> (pop-item :string state)
(pop-item :string)
(push-item (string/index-of stri substr) :integer)))))))
Cirru/calcit-editor
(ns app.comp.picker-notice
(:require [respo.core :refer [defcomp list-> >> <> span div a pre]]
[clojure.string :as string]
[respo-ui.core :as ui]
[hsl.core :refer [hsl]]
[respo.comp.space :refer [=<]]))
(defcomp
comp-picker-notice
(choices target-node)
(let [imported-names (:imported choices)
defined-names (:defined choices)
render-code (fn [x]
(span
{:inner-text x,
:style style-name,
:on-click (fn [e d!] (d! :writer/pick-node x))}))
hint (if (some? target-node) (:text target-node) nil)
hint-fn (fn [x] (if (string/blank? hint) false (string/includes? x hint)))]
(div
{:style style-container}
(div
{:style {:font-family ui/font-fancy,
:font-size 16,
:font-weight 300,
:color (hsl 0 0 80),
:cursor :pointer},
:on-click (fn [e d!] (d! :writer/picker-mode nil))}
(<> "Picker mode: pick a target..."))
(let [possible-names (->> (concat imported-names defined-names)
distinct
(filter hint-fn))]
(if-not (empty? possible-names)
(div
{}
(list->
{}
(->> possible-names
(sort-by (fn [x] (string/index-of x hint)))
(map (fn [x] [x (render-code x)]))))
(=< nil 8))))
(let [filtered-names (->> imported-names (remove hint-fn))]
(if-not (empty? filtered-names)
(div
{}
(list-> {} (->> filtered-names sort (map (fn [x] [x (render-code x)]))))
(=< nil 8))))
(list-> {} (->> defined-names (remove hint-fn) sort (map (fn [x] [x (render-code x)])))))))
gigasquid/libpython-clj-examples
(ns gigasquid.psutil.core
(:require [libpython-clj.require
:refer [require-python]]
[libpython-clj.python
:as py
:refer [py.
py..
py.-
att-type-map
->python
->jvm]]
[clojure.java.shell :as sh]
[clojure.string :as str]
[clojure.pprint :refer [pprint]])
(:import [java.io File]))
(filter (fn [x] (str/index-of (str/lower-case x) "sys"))
process-names)