apply for arrays?

For the Compleat Fan
Locked
hartrock
Posts: 136
Joined: Wed Aug 07, 2013 9:37 pm

apply for arrays?

Post by hartrock »

[update] -> 10.5.6.

Code: Select all

(setq a (array 10 (sequence 1 10)))

;; currently we have:
(setq a_applied (apply + (array-list a)))

;; nice would be:
(setq a_applied (apply-array + a))
;; or:
(setq a_applied (apply + a))
Func apply-array could work at source array directly (without indirection by conversions to/from lists).
Because
  • it shouldn't be more expensive than its list counterpart,
  • the result type depends on to be applied function and not on source to be applied to (array);
it probably could be named the same (and internally just do the right thing).
Last edited by hartrock on Mon Dec 02, 2013 8:54 pm, edited 1 time in total.

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: apply for arrays?

Post by Lutz »

Both map and apply also take arrays in the next version, but will do array -> list conversion internally to keep code changes / additions to a minimum leveraging existing code.

rickyboy
Posts: 607
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

Re: apply for arrays?

Post by rickyboy »

If the input "sequence" is an array, what would the output "sequence" be -- an array or list?
(λx. x x) (λx. x x)

hartrock
Posts: 136
Joined: Wed Aug 07, 2013 9:37 pm

Re: apply for arrays?

Post by hartrock »

rickyboy wrote:If the input "sequence" is an array, what would the output "sequence" be -- an array or list?
For map the output naturally would be an array (for apply applied func decides).

A multi-dimensional array can be seen as an array of arrays: then map would just map first dimension.
This seems to be the semantics of array; at least it's possible to replace elements of first dim by both lists and arrays, without changing the type of the array:

Code: Select all

> (setq a (array 2 5 (sequence 0 9)))
((0 1 2 3 4) (5 6 7 8 9))
> (setq (a 0) (sequence 10 14))
(10 11 12 13 14)
> (setq (a 1) (array 5 (sequence 15 19)))
(15 16 17 18 19)
> (array? (a 0))
nil
> (array? (a 1))
true
>> (array? a)
true
>
In case of mapping all lowest-level elements - e.g. pixels in an image - map had to be nested.

hartrock
Posts: 136
Joined: Wed Aug 07, 2013 9:37 pm

Re: apply for arrays?

Post by hartrock »

Lutz wrote:Both map and apply also take arrays in the next version, but will do array -> list conversion internally to keep code changes / additions to a minimum leveraging existing code.
This sounds good!

There are some functions for which there are different performance characteristics for lists and arrays: e.g. an assoc for arrays should be slower for large arrays compared to lists. In such cases there is a trade-off, because there are two possibilities for making such a function applicable for the other data type:
  • Use the same name: comfortable to use (unification), but may create performance surprises, if list arg gets an array or vice-versa;
  • use another name: less comfortable to use, but you have it explicit, that a list or an array is expected, and exclude the other data type.
My point has been - under the assumption, that arrays are specially made just for performance reasons -, that this kind of trade-off needn't be applied for apply, if it will be implemented accordingly. So far I had seen array-list as a result of the will to make computation effort visible (you explicitely have to convert), and not as something, which has to be there.

Locked