11 Nov 2012 00:01

## [repa] beginner questions

```Playing around with repa arrays and got some questions.

1) How I can get list of indexes of array that suffice some predicate?

> a1
AUnboxed (Z :. 3) (fromList [False,False,True])
it :: Array U (Z :. Int) Bool

Indexes of element that satisfying specific predicate could be obtained
like that:

> (\a p → Data.List.map (subtract 1 . snd) \$ filter (p . fst) \$ zip
(toList a) [1..]) a1 (== False)
[0,1]

Looks ugly. How REPA users used to do filtering like that without
converting to list?

2) How can I apply some function `f' to each row of 2D array `a' and
collect results in single value?

f ∷ (Shape sh, Source r Bool) ⇒ Array r sh Bool → Bool
f a = (== toList a) \$
foldl1 (Prelude.zipWith (||)) \$
Prelude.map toList \$
foldl (\l k -> filter (\x -> x ! (Z :. k) == False) l)
[b1,b2,b3,b4] \$
findWhich (== False) a

and ∷ [Bool] → Bool
```

18 Nov 2012 22:21

### Re: [repa] beginner questions

```Dmitry Malikov <malikov.d.y <at> gmail.com> writes:

>
> Playing around with repa arrays and got some questions.
>
> 1) How I can get list of indexes of array that suffice some predicate?
>
>      > a1
>      AUnboxed (Z :. 3) (fromList [False,False,True])
>      it :: Array U (Z :. Int) Bool
>
> Indexes of element that satisfying specific predicate could be obtained
> like that:
>
>      > (\a p → Data.List.map (subtract 1 . snd) \$ filter (p . fst) \$ zip
> (toList a) [1..]) a1 (== False)
>      [0,1]
>
> Looks ugly. How REPA users used to do filtering like that without
> converting to list?
>

I hope someone will correct me if I am wrong and furthermore I was not
entirely clear what you were trying to do but it seems to me that if you
want to filter out an unknown number of elements from a collection
then repa is the wrong abstraction to use.

You can however filter out a known number of elements e.g.

xs = Repa.fromListUnboxed (Z :. 3) [1, 2, 3]
```

19 Nov 2012 21:47

### Re: [repa] beginner questions

```On 11/19/2012 01:21 AM, Dominic Steinitz wrote:
> Dmitry Malikov <malikov.d.y <at> gmail.com> writes:
>
>> Playing around with repa arrays and got some questions.
>>
>> 1) How I can get list of indexes of array that suffice some predicate?
>>
>>       > a1
>>       AUnboxed (Z :. 3) (fromList [False,False,True])
>>       it :: Array U (Z :. Int) Bool
>>
>> Indexes of element that satisfying specific predicate could be obtained
>> like that:
>>
>>       > (\a p → Data.List.map (subtract 1 . snd) \$ filter (p . fst) \$ zip
>> (toList a) [1..]) a1 (== False)
>>       [0,1]
>>
>> Looks ugly. How REPA users used to do filtering like that without
>> converting to list?
>>
> I hope someone will correct me if I am wrong and furthermore I was not
> entirely clear what you were trying to do but it seems to me that if you
> want to filter out an unknown number of elements from a collection
> then repa is the wrong abstraction to use.
>
> You can however filter out a known number of elements e.g.
>
> xs = Repa.fromListUnboxed (Z :. 3) [1, 2, 3]
>
```