Which function can I use to find its index of an element in an Array?
For example, I want to find the index of 'x' in an Array (Data.Array)
lowerCase = listArray ((0,0),(1,12)) ['a'..]
fst <$> find ((== 'a') . snd) $ assocs lowerCase
array have its own find or in this case elemIndex / elemIndices?find because Array has a Foldable instance. I don't know why there is not a elemIndex for Array.To get all the indices a certain element appears in your Data.Array the following list comprehension can be used:
results = [fst x | x <- (assocs lowerCase), snd x == 'a']
assocs has the following prototype:
assocs :: Ix i => Array i e -> [(i, e)]
It basically flattens a Data.Array in a List containing (i, e) pairs.
For:
a = listArray ((0,0),(2,2)) ['a'..]
assocs a will output
[((0,0),'a'),((0,1),'b'),((0,2),'c'),((1,0),'d'),((1,1),'e'),((1,2),'f'),((2,0),'g'),((2,1),'h'),((2,2),'i')]
Now, in our list comprehension, we have x <- (assocs a), so x is generated by the list assocs a.
The list outputed by the list comprehension will contain only fst x where
snd x == theElementWeAreLookingFor
Every x generated by assocs a is checked and if the condition snd x == 'a' is met then fst a (the index) will be inserted in the output list.
Once the list is generated, it can be checked whether there are none, one or more outputs.
getElementIndex :: Array (Int, Int) Char -> Char -> Maybe (Int, Int)
getElementIndex a e
| null results = Nothing
| othwerwise = Just $ head results
where results = [fst x | x <- (assocs a), snd x == e]
A imperative pseduocode could look like:
results = []
for_each x in a.toList():
if x.second == 'a':
results.append(x.first)
xin the array ? Basically, arrays are for accessing by index and if you need to find element's index then array is not the right data structure.