[racket] Is there a functional "array-set" for math/array?
There's no `array-set` yet. I didn't want to do it until I had a good
internal representation that supported efficient functional updates.
Alexander, your version of `array-set` will do fine in a pinch. Here are
a few things you might want to keep in mind.
1. If you're using strict arrays (the default), each `array-set` call
will copy the entire input array.
2. If you're using nonstrict arrays (i.e. `array-strictness` is #f),
each `array-set` call will run in constant time. However, accessing the
result will have additional constant overhead because each access will
call your lambda.
You can get nonstrict arrays in a dynamic environment that uses strict
arrays by wrapping the `array-set` function body like this:
(parameterize ([array-strictness #f])
((inst build-array a)
(array-shape arr)
(lambda ...)))
3. If updates happen in batches, consider writing specialized update
functions for each update pattern (for example, a row update).
4. If the updates happen within only one function, consider using
`array->mutable-array` to copy the input array, mutating the result, and
returning it. If the function's result type is (Array a) and the
returned mutable array never passes into untyped Racket code, it can't
be mutated any further.
Neil ⊥
On 07/15/2014 09:16 AM, Andrew Dudash wrote:
> If it already exists, I couldn't find it. There isn't a functional
> 'vector-set' either, and when I asked about it on IRC I was told that it
> wasn't an easy fix to add an efficient implementation.
>
>
> On Mon, Jul 14, 2014 at 7:19 PM, Alexander D. Knauth
> <alexander at knauth.org <mailto:alexander at knauth.org>> wrote:
>
> Is there a functional "array-set" for math/array?
>
> And if there isn’t, then would this be a good definition for it?:
> (: array-set : (All (a) [(Array a) In-Indexes a -> (Array a)]))
> (define (array-set arr update-pos val)
> ((inst build-array a)
> (array-shape arr)
> (lambda ([pos : Indexes])
> (cond [(equal? pos update-pos) val]
> [else (array-ref arr pos)]))))
>
>
>
> ____________________
> Racket Users list:
> http://lists.racket-lang.org/users
>
>
>
>
> ____________________
> Racket Users list:
> http://lists.racket-lang.org/users
>