public
Last active — forked from nathany/gist:190489

  • Download Gist
gistfile1.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// Returns the index of the first character contained in searchChars
// From Apache Commons Lang, http://commons.apache.org/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
gistfile2.clj
Clojure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
;; 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)
gistfile3.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
# 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
try:
print( index_filter("ab", None) )
except TypeError:
pass
 
# 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] )
gistfile4.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
# 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
end
 
# 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) }
end
 
# 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)
collection.each_with_index.map do |item, index|
index if predicate.include?(item)
end.compact
end
 
# For completeness
def indexed(collection)
collection.each_with_index
end
 
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]

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.