Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
// Returns the index of the first character contained in searchChars
// From Apache Commons Lang,
public static int indexOfAny(String str, char[] searchChars) {
if (isEmpty(str) || ArrayUtils.isEmpty(searchChars)) {
return -1;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
for (int j = 0; j < searchChars.length; j++) {
if (searchChars[j] == ch) {
return i;
return -1;
// LOC 14, Branches 4, Exits Method 3, Local Variables 3
;; map collection to vectors with [incrementing number, collection element]
(defn indexed [coll]
(map vector (iterate inc 0) coll))
;; sequence comprehension returns idx when element is in the predicate sequence
;; sets are functions that test membership in the set (pred elt)
(defn index-filter [pred coll]
(when pred
(for [[idx elt] (indexed coll) :when (pred elt)] idx)))
(defn index-of-any [pred coll] (first (index-filter pred coll)))
;; LOC 6, Branches 1, Exits/Method 1, Local Variables 0
;; try it out
(indexed "abcde")
(index-filter #{\a \b} "abcdbbb")
(index-of-any #{\a \b} "zzabyycdxx")
;; generalized, find the index of the 3rd "heads" coin toss
(nth (index-filter #{:h} [:t :t :h :t :h :t :t :t :h :h]) 2)
# Python has indexed() built in, it's called enumerate().
# A list comprehension and slice could do it in one line,
# so long as dealing with Iterable inputs.
def index_filter(predicate, collection): # throws a TypeError if not iterable (i.e. NoneType, int)
return [idx for (idx,elt) in enumerate(collection) if elt in predicate]
def index_of_any(predicate, collection):
return index_filter(predicate, collection)[0] # Warning: NoneType is unsubscriptable
print( index_filter("ab", "sabcdbbb") )
print( index_of_any("ab", "zzabyycdxx") )
# can't iterate over int, so insufficient to only check that not None:
# if predicate != None and collection != None:
# could catch and discard the error
print( index_filter("ab", None) )
except TypeError:
# no :keywords, but still generalized... set {1} works in Python 3 but not 2.x, tuple works
print( "3rd heads:", index_filter((1,), [0, 0, 1, 0, 1, 0, 0, 0, 1, 1])[2] )
# String is a "collection" (i.e. Enumerable) in 1.8, but it's a collection of its lines.
# In 1.9, it's not a collection. For the purposes of this Script, let's make it a
# collection over its characters:
class String
# In 1.8, this is a noop
include Enumerable
# In 1.8, replace the builtin #each
alias each each_char
# Another way to do index_of_any without using a builtin
# Note that we're leveraging non-local-return and Enumerators here
def index_of_any(predicate, collection)
collection.each_with_index.find {|item, i| return i if predicate.include?(item) }
# Again, we're leveraging Enumerators to avoid needing a separate #indexed
# method. collection.each_with_index returns an Enumerator that looks a lot
# like the concrete Array that #indexed returned
def index_filter(predicate, collection) do |item, index|
index if predicate.include?(item)
# For completeness
def indexed(collection)
puts index_of_any %w{a b}, "zzabyycdxx"
p indexed("abcde")
p index_filter %w{a b}, "sabcdbbb"
# then index_of_any just becomes a call to first() or a slice like Python
puts index_filter(%w{a b}, "zzabyycdxx").first
# yup, generic, except the whole chars() thing
print "3rd heads:"
puts index_filter([:h], [:t, :t, :h, :t, :h, :t, :t, :t, :h, :h])[2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment