Prompted by a comments conversation here, here's an interesting (and often performance enhancing) use of Extract
:
target = Join[Range[100], {{1, 2, 3, Range[20]}}];
Rest@Extract[target, {{{}}, {2 ;; 10 ;; 2}, {-1, -1, 1 ;; -1 ;; 2}}]
(* {{2, 4, 6, 8, 10}, {1, 3, 5, 7, 9, 11, 13, 15, 17, 19}} *)
Prepending the {{}}
to the extraction list appears to suppress the error message one would otherwise get attempting to use Span
, and it appears that most any valid position/span construct can be used in the extraction list.
The extracted list has an empty list as first element (hence the use of Rest
). Prepending just {}
makes the first element the original list.
I've often found this useful instead of mapping over a list of spans (and faster).
This seems similar to the undocumented extension to MapAt
As noted by Mr. Wizard, this does not appear to work in V7, I'm curious what versions it works in.
Comments
Post a Comment