This page is no longer maintained — Please continue to the home page at www.scala-lang.org

a non-sequential slice

8 replies
ethul
Joined: 2009-09-23,
User offline. Last seen 2 years 21 weeks ago.
hi All,
i am a scala newbie, and was trying to figure out a way in scala to take a non-sequential slice of elements from a scala List.
for example:
   let x be a list of ten elements
   let y be a list of indices where y is a subset of x.indices; so y = List(0,5,7,3) for example
is there a simple way to do something like
   z = x.nonSeqentialSlice(y)
which would create a new List z containing the elements at index 0, 5, 7, and 3 from x
i hope what i am asking is clear, thank you in advance!
 - Eric

    
dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: a non-sequential slice
Easy way: z = y map (x(_))   Assuming y is ordered, a, perhaps, faster way: z = x.projection.zipWithIndex filter (y contains _._2) map (_._1) toList
On Wed, Sep 23, 2009 at 10:56 AM, Eric Thul <eric [dot] thul [at] gmail [dot] com> wrote:
hi All,
i am a scala newbie, and was trying to figure out a way in scala to take a non-sequential slice of elements from a scala List.
for example:
   let x be a list of ten elements
   let y be a list of indices where y is a subset of x.indices; so y = List(0,5,7,3) for example
is there a simple way to do something like
   z = x.nonSeqentialSlice(y)
which would create a new List z containing the elements at index 0, 5, 7, and 3 from x
i hope what i am asking is clear, thank you in advance!
 - Eric

    



--
Daniel C. Sobral

Something I learned in academia: there are three kinds of academic reviews: review by name, review by reference and review by value.
ethul
Joined: 2009-09-23,
User offline. Last seen 2 years 21 weeks ago.
Re: a non-sequential slice
thanks Daniel!

On Wed, Sep 23, 2009 at 10:08 AM, Daniel Sobral <dcsobral [at] gmail [dot] com> wrote:
Easy way: z = y map (x(_))   Assuming y is ordered, a, perhaps, faster way: z = x.projection.zipWithIndex filter (y contains _._2) map (_._1) toList
On Wed, Sep 23, 2009 at 10:56 AM, Eric Thul <eric [dot] thul [at] gmail [dot] com> wrote:
hi All,
i am a scala newbie, and was trying to figure out a way in scala to take a non-sequential slice of elements from a scala List.
for example:
   let x be a list of ten elements
   let y be a list of indices where y is a subset of x.indices; so y = List(0,5,7,3) for example
is there a simple way to do something like
   z = x.nonSeqentialSlice(y)
which would create a new List z containing the elements at index 0, 5, 7, and 3 from x
i hope what i am asking is clear, thank you in advance!
 - Eric

    



--
Daniel C. Sobral

Something I learned in academia: there are three kinds of academic reviews: review by name, review by reference and review by value.

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: a non-sequential slice
In fact, since List extends Function1, you can short the first one to:   val z = y map x

On Wed, Sep 23, 2009 at 11:20 AM, Eric Thul <eric [dot] thul [at] gmail [dot] com> wrote:
thanks Daniel!

On Wed, Sep 23, 2009 at 10:08 AM, Daniel Sobral <dcsobral [at] gmail [dot] com> wrote:
Easy way: z = y map (x(_))   Assuming y is ordered, a, perhaps, faster way: z = x.projection.zipWithIndex filter (y contains _._2) map (_._1) toList
On Wed, Sep 23, 2009 at 10:56 AM, Eric Thul <eric [dot] thul [at] gmail [dot] com> wrote:
hi All,
i am a scala newbie, and was trying to figure out a way in scala to take a non-sequential slice of elements from a scala List.
for example:
   let x be a list of ten elements
   let y be a list of indices where y is a subset of x.indices; so y = List(0,5,7,3) for example
is there a simple way to do something like
   z = x.nonSeqentialSlice(y)
which would create a new List z containing the elements at index 0, 5, 7, and 3 from x
i hope what i am asking is clear, thank you in advance!
 - Eric

    



--
Daniel C. Sobral

Something I learned in academia: there are three kinds of academic reviews: review by name, review by reference and review by value.




--
Daniel C. Sobral

Something I learned in academia: there are three kinds of academic reviews: review by name, review by reference and review by value.
David Copeland
Joined: 2009-06-16,
User offline. Last seen 42 years 45 weeks ago.
Re: a non-sequential slice

This is good example of a question I have, which is why doesn't TupleN
have an apply method, so we can do

z = x.projection.zipWithIndex filter (y contains _(1)) map (_(0)) toList

To me, having two underscores so close to each other and having wildly
different meanings requires some mental parsing (plus, the _N methods
are just really ugly to me)

Dave

---
My Blog: http://www.naildrivin5.com/daveblog5000
Scala Tour for Java Developers: http://www.naildrivin5.com/scalatour
Fork me on Github: http://davetron5000.github.com

On Wed, Sep 23, 2009 at 10:08 AM, Daniel Sobral wrote:
> Easy way:
> z = y map (x(_))
>
> Assuming y is ordered, a, perhaps, faster way:
> z = x.projection.zipWithIndex filter (y contains _._2) map (_._1) toList
> On Wed, Sep 23, 2009 at 10:56 AM, Eric Thul wrote:
>>
>> hi All,
>> i am a scala newbie, and was trying to figure out a way in scala to take a
>> non-sequential slice of elements from a scala List.
>> for example:
>>    let x be a list of ten elements
>>    let y be a list of indices where y is a subset of x.indices; so y =
>> List(0,5,7,3) for example
>> is there a simple way to do something like
>>    z = x.nonSeqentialSlice(y)
>> which would create a new List z containing the elements at index 0, 5, 7,
>> and 3 from x
>> i hope what i am asking is clear, thank you in advance!
>>  - Eric
>>
>>
>
>
> --
> Daniel C. Sobral
>
> Something I learned in academia: there are three kinds of academic reviews:
> review by name, review by reference and review by value.
>

Sciss
Joined: 2008-12-17,
User offline. Last seen 28 weeks 5 days ago.
Re: a non-sequential slice

i also think that _1, _2 etc. is extremely ugly. what was the
reasoning for choosing these method names for tuples?

Am 23.09.2009 um 17:09 schrieb David Copeland:

> This is good example of a question I have, which is why doesn't TupleN
> have an apply method, so we can do
>
> z = x.projection.zipWithIndex filter (y contains _(1)) map (_(0))
> toList
>
> To me, having two underscores so close to each other and having wildly
> different meanings requires some mental parsing (plus, the _N methods
> are just really ugly to me)
>
> Dave
>
> ---
> My Blog: http://www.naildrivin5.com/daveblog5000
> Scala Tour for Java Developers: http://www.naildrivin5.com/scalatour
> Fork me on Github: http://davetron5000.github.com
>
>
>
>
> On Wed, Sep 23, 2009 at 10:08 AM, Daniel Sobral
> wrote:
>> Easy way:
>> z = y map (x(_))
>>
>> Assuming y is ordered, a, perhaps, faster way:
>> z = x.projection.zipWithIndex filter (y contains _._2) map (_._1)
>> toList
>> On Wed, Sep 23, 2009 at 10:56 AM, Eric Thul
>> wrote:
>>>
>>> hi All,
>>> i am a scala newbie, and was trying to figure out a way in scala
>>> to take a
>>> non-sequential slice of elements from a scala List.
>>> for example:
>>> let x be a list of ten elements
>>> let y be a list of indices where y is a subset of x.indices;
>>> so y =
>>> List(0,5,7,3) for example
>>> is there a simple way to do something like
>>> z = x.nonSeqentialSlice(y)
>>> which would create a new List z containing the elements at index
>>> 0, 5, 7,
>>> and 3 from x
>>> i hope what i am asking is clear, thank you in advance!
>>> - Eric
>>>
>>>
>>
>>
>> --
>> Daniel C. Sobral
>>
>> Something I learned in academia: there are three kinds of academic
>> reviews:
>> review by name, review by reference and review by value.
>>

Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: a non-sequential slice


On Wed, Sep 23, 2009 at 5:09 PM, David Copeland <davec [at] naildrivin5 [dot] com> wrote:
This is good example of a question I have, which is why doesn't TupleN
have an apply method, so we can do

Why are they not Iterables?
 

z = x.projection.zipWithIndex filter (y contains _(1)) map (_(0)) toList

To me, having two underscores so close to each other and having wildly
different meanings requires some mental parsing (plus, the _N methods
are just really ugly to me)

Dave

---
My Blog: http://www.naildrivin5.com/daveblog5000
Scala Tour for Java Developers: http://www.naildrivin5.com/scalatour
Fork me on Github: http://davetron5000.github.com




On Wed, Sep 23, 2009 at 10:08 AM, Daniel Sobral <dcsobral [at] gmail [dot] com> wrote:
> Easy way:
> z = y map (x(_))
>
> Assuming y is ordered, a, perhaps, faster way:
> z = x.projection.zipWithIndex filter (y contains _._2) map (_._1) toList
> On Wed, Sep 23, 2009 at 10:56 AM, Eric Thul <eric [dot] thul [at] gmail [dot] com> wrote:
>>
>> hi All,
>> i am a scala newbie, and was trying to figure out a way in scala to take a
>> non-sequential slice of elements from a scala List.
>> for example:
>>    let x be a list of ten elements
>>    let y be a list of indices where y is a subset of x.indices; so y =
>> List(0,5,7,3) for example
>> is there a simple way to do something like
>>    z = x.nonSeqentialSlice(y)
>> which would create a new List z containing the elements at index 0, 5, 7,
>> and 3 from x
>> i hope what i am asking is clear, thank you in advance!
>>  - Eric
>>
>>
>
>
> --
> Daniel C. Sobral
>
> Something I learned in academia: there are three kinds of academic reviews:
> review by name, review by reference and review by value.
>



--
Viktor Klang

Blog: klangism.blogspot.com
Twttr: viktorklang

Lift Committer - liftweb.com
AKKA Committer - akkasource.org
Cassidy - github.com/viktorklang/Cassidy.git
SoftPub founder: http://groups.google.com/group/softpub
Seth Tisue
Joined: 2008-12-16,
User offline. Last seen 34 weeks 3 days ago.
Re: a non-sequential slice

>>>>> "David" == David Copeland writes:

David> This is good example of a question I have, which is why doesn't
David> TupleN have an apply method

What would its type be?

David Copeland
Joined: 2009-06-16,
User offline. Last seen 42 years 45 weeks ago.
Re: a non-sequential slice

That is a good point; I suppose the methods to access each element
need to be typed, so there's no type, other than Any, that would work
(Product defines productElement that returns an Any; if TupleN defined
apply as an alias for this, would it help?)

Still, the underscores and the 1-based-ed-ness of it is kinda weird to
me. I think almost ANY other symbol than underscore would make it
nicer.

t.:2
t.|2
t.$2 <-- kinda like this
t.>2
t.#2 <-- not bad either

Dave

---
My Blog: http://www.naildrivin5.com/daveblog5000
Scala Tour for Java Developers: http://www.naildrivin5.com/scalatour
Fork me on Github: http://davetron5000.github.com

On Wed, Sep 23, 2009 at 11:17 AM, Seth Tisue wrote:
>>>>>> "David" == David Copeland writes:
>
>  David> This is good example of a question I have, which is why doesn't
>  David> TupleN have an apply method
>
> What would its type be?
>
> --
> Seth Tisue @ Northwestern University / http://tisue.net
> lead developer, NetLogo: http://ccl.northwestern.edu/netlogo/
>

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland