MathGroup Archive 2012

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: case of inconsistent API between Drop and Part?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg127131] Re: case of inconsistent API between Drop and Part?
  • From: Bob Hanlon <hanlonr357 at gmail.com>
  • Date: Mon, 2 Jul 2012 05:23:14 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <201206300917.FAA01642@smc.vnet.net>

To drop elements 2 and 4 in one command:

lis = {a, b, c, d, e};

Drop[lis, {2, 4, 2}]

{a, c, e}


Bob Hanlon


On Sat, Jun 30, 2012 at 5:17 AM, Nasser M. Abbasi <nma at 12000.org> wrote:
> One thing I always liked about Mathematica's functions is
> that the API's of its functions all seem to be consistent
> (for the most part).
>
> Here is a case where it is not consistent though to share it.
>
> (I am trying to make a list of all such cases in my cheat sheet)
>
> It is between Drop and Part.
>
> Drop[list,{m,n}]  gives list with elements m THROUGH n dropped.
> Drop[list,m;;n]   gives list with elements m THROUGH n dropped.
>
> but
>
> Part[list,{m,n}]   gives the list of elements m AND n
> Part[list,m;;n]    gives the list of elements m THROUGH n
>
> Example using Drop:
> -----------------
> Clear[a,b,c,d,e];
> lis={a,b,c,d,e};
> Drop[lis,2;;4]
>
> Out[161]= {a,e}
>
> Drop[lis,{2,4}]
>
> Out[162]= {a,e}
> ----------------------
>
> Example using Part:
> ------------------
> Part[lis,2;;4]
>
> Out[163]= {b,c,d}
>
> Part[lis,{2,4}]
>
> Out[164]= {b,d}
> --------------------
>
> What the above also mean, is that using the Drop function,
> there is no direct way to tell it to Drop element 2 AND
> element 4 in one command.  (there are easy ways to do this,
> but not using one function call of Drop as is). (one easy
> solution is to complement the elements to drop, and use Part
> instead, and other ways)
>
> I do not know if the experts here agree that this is an
> inconsistency in the API between Drop and Part, but for me,
> (and I am no expert) it seems so.
>
> http://reference.wolfram.com/mathematica/ref/Part.html
> http://reference.wolfram.com/mathematica/ref/Drop.html
> --Nasser
>



-- 
Bob Hanlon



  • Prev by Date: Re: How to export to Excel numbers as text fields?
  • Next by Date: Re: multiplying, element wise, a row by each column of a matrix.
  • Previous by thread: Re: case of inconsistent API between Drop and Part?
  • Next by thread: Re: case of inconsistent API between Drop and Part?