2
$\begingroup$

This answer says that Listable just makes the function automatically apply MapThread to its arguments.

Does anyone have a simple example of when explicitly using MapThread is better (or simply more convenient) then just giving a function the Listable attribute?

My guess is that if I have a function that works on lists (concatenating, etc), this would be such a case. Or perhaps theres an easy example using Plot (since a list of plot may not be desirable).

$\endgroup$
2
  • 1
    $\begingroup$ Explicit is better than implicit? $\endgroup$
    – Alan
    May 29, 2021 at 17:29
  • 1
    $\begingroup$ If it is a built in function, it's probably not a good idea to change its Listable status, else you might break something internally. $\endgroup$
    – bill s
    May 29, 2021 at 19:38

1 Answer 1

2
$\begingroup$

Any function whose arguments are not "homogeneous" would be a candidate. Or any function whose arguments won't "align" properly during a threading. Or any function that expects some of its arguments to be lists with particular semantics in terms of size, position, etc. For example.

myF[a_String, {pre_String, post_String}] := pre <> a <> post

It works like this:

myF["hello", {"a", "b"}]

"ahellob"

But now,

SetAttributes[myF, Listable];
myF["hello", {"a", "b"}]

{myF["hello", "a"], myF["hello", "b"]}

We might want it to be Listable just on the first argument, so we could do this:

myF[{"hello", "goodbye"}, {"a", "b"}]

but that doesn't do what we want now: {myF["hello", "a"], myF["goodbye", "b"]}

If we want to MapThread, we could have done it explicitly:

ClearAttributes[myF, Listable];
MapThread[myF, {{"hello", "goodbye"}, {{"a", "b"}, {"c", "d"}}}]

{"ahellob", "cgoodbyed"}

Or, if we want to thread just on the first argument, we can provide that definition:

myF[as : {___String}, pad : {_String, _String}] := 
  Activate[Thread[Inactive[myF][as, pad], List, 1]];
myF[{"hello", "goodbye"}, {"a", "b"}]

{"ahellob", "agoodbyeb"}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.