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

(Idea) Customizable Iterable.mkString

11 replies
Drew
Joined: 2011-12-16,
User offline. Last seen 42 years 45 weeks ago.

Hi Everyone,

I was thinking that it would be useful to be able to pass a custom
function to turn elements into String to Itrable.mkString. Something
to replace

items.foldLeft("")((str, item) => {
val toAppend = if (str.isEmpty()) {
item.toCustomString
} else {
"," + item.toCustomString
}
str + toAppend
}

so instead will be able to write

items.mkString(",", _.toCustomString)

What does everybody think about this? Should I open an enhancement
JIRA for this?

Jason Zaugg
Joined: 2009-05-18,
User offline. Last seen 38 weeks 5 days ago.
Re: (Idea) Customizable Iterable.mkString
On Sat, Dec 17, 2011 at 6:17 AM, Drew <drew [at] venarc [dot] com> wrote:
Hi Everyone,

I was thinking that it would be useful to be able to pass a custom
function to turn elements into String to Itrable.mkString. Something
to replace

items.foldLeft("")((str, item) => {
                       val toAppend = if (str.isEmpty()) {
                               item.toCustomString
                       } else {
                               "," + item.toCustomString
                       }
                       str + toAppend
               }

so instead will be able to write

items.mkString(",", _.toCustomString)

I think that this is sufficient:
  items.view.map(_.toCustomString).mkString(",") 
-jason
Drew
Joined: 2011-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: (Idea) Customizable Iterable.mkString

Oh wow, that looks nice. I'm not too familiar with views. Would that
create an extra collection?

Joshua.Suereth
Joined: 2008-09-02,
User offline. Last seen 32 weeks 5 days ago.
Re: Re: (Idea) Customizable Iterable.mkString
No. the beauty of views is that they point at an existing collection.

On Fri, Dec 16, 2011 at 4:10 PM, Drew <drew [at] venarc [dot] com> wrote:
Oh wow, that looks nice. I'm not too familiar with views. Would that
create an extra collection?

Drew
Joined: 2011-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: (Idea) Customizable Iterable.mkString

Looking into views a bit more, seems like they don't apply to
Iterators, only Iterables

On Dec 16, 1:10 pm, Drew wrote:
> Oh wow, that looks nice. I'm not too familiar with views. Would that
> create an extra collection?
>

Drew
Joined: 2011-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: (Idea) Customizable Iterable.mkString

Thanks Josh,

The problem that I'm trying to solve is that I'm reading the lines
from a file, parse it line by line and generate objects "Item" using
map() see

Source.fromURL("some file", "UTF-8").getLines().slice(start,
end).map((line) => { parse line and create items})
That returns an Iterator. Now I want to create JSON from the
collection by using your method, but Iterator doesn't have view. Any
advice?

On Dec 16, 1:15 pm, Drew wrote:
> Looking into views a bit more, seems like they don't apply to
> Iterators, only Iterables
>
> On Dec 16, 1:10 pm, Drew wrote:
>
>
>
>
>
>
>
> > Oh wow, that looks nice. I'm not too familiar with views. Would that
> > create an extra collection?
>
> > -- Drew
>
> > On Dec 16, 12:20 pm, Jason Zaugg wrote:
>
> > > On Sat, Dec 17, 2011 at 6:17 AM, Drew wrote:
> > > > Hi Everyone,
>
> > > > I was thinking that it would be useful to be able to pass a custom
> > > > function to turn elements into String to Itrable.mkString. Something
> > > > to replace
>
> > > > items.foldLeft("")((str, item) => {
> > > >                        val toAppend = if (str.isEmpty()) {
> > > >                                item.toCustomString
> > > >                        } else {
> > > >                                "," + item.toCustomString
> > > >                        }
> > > >                        str + toAppend
> > > >                }
>
> > > > so instead will be able to write
>
> > > > items.mkString(",", _.toCustomString)
>
> > > I think that this is sufficient:
>
> > >   items.view.map(_.toCustomString).mkString(",")
>
> > > -jason

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: Re: (Idea) Customizable Iterable.mkString

On Fri, Dec 16, 2011 at 19:15, Drew wrote:
> Looking into views a bit more, seems like they don't apply to
> Iterators, only Iterables

That's because Iterators don't need views, since no new collection is
created when you call "map" on an Iterator.

>
> On Dec 16, 1:10 pm, Drew wrote:
>> Oh wow, that looks nice. I'm not too familiar with views. Would that
>> create an extra collection?
>>

Drew
Joined: 2011-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: (Idea) Customizable Iterable.mkString

Thanks guys. What's a good reading to get to know the inner workings
of the Scala Collection's classes. I'm pretty new to Scala and I've
heard that it's very easy to shoot yourself in the food (memory leaks,
etc) using Scala collections if you don't know what you're doing. For
example, I didn't know the semantics that you guys talked about such
as "no new collection is created when you call "map" on an Iterator. "
or

On Dec 16, 1:34 pm, Daniel Sobral wrote:
> On Fri, Dec 16, 2011 at 19:15, Drew wrote:
> > Looking into views a bit more, seems like they don't apply to
> > Iterators, only Iterables
>
> That's because Iterators don't need views, since no new collection is
> created when you call "map" on an Iterator.
>
>
>
>
>
>
>
>
>
>
>
> > On Dec 16, 1:10 pm, Drew wrote:
> >> Oh wow, that looks nice. I'm not too familiar with views. Would that
> >> create an extra collection?
>
> >> -- Drew
>
> >> On Dec 16, 12:20 pm, Jason Zaugg wrote:
>
> >> > On Sat, Dec 17, 2011 at 6:17 AM, Drew wrote:
> >> > > Hi Everyone,
>
> >> > > I was thinking that it would be useful to be able to pass a custom
> >> > > function to turn elements into String to Itrable.mkString. Something
> >> > > to replace
>
> >> > > items.foldLeft("")((str, item) => {
> >> > >                        val toAppend = if (str.isEmpty()) {
> >> > >                                item.toCustomString
> >> > >                        } else {
> >> > >                                "," + item.toCustomString
> >> > >                        }
> >> > >                        str + toAppend
> >> > >                }
>
> >> > > so instead will be able to write
>
> >> > > items.mkString(",", _.toCustomString)
>
> >> > I think that this is sufficient:
>
> >> >   items.view.map(_.toCustomString).mkString(",")
>
> >> > -jason
>
> --
> Daniel C. Sobral
>
> I travel to the future all the time.

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: Re: (Idea) Customizable Iterable.mkString

On Fri, Dec 16, 2011 at 19:51, Drew wrote:
> Thanks guys. What's a good reading to get to know the inner workings
> of the Scala Collection's classes. I'm pretty new to Scala and I've
> heard that it's very easy to shoot yourself in the food (memory leaks,
> etc) using Scala collections if you don't know what you're doing. For
> example, I didn't know the semantics that you guys talked about such
> as "no new collection is created when you call "map" on an Iterator. "
> or

Well, start here: http://docs.scala-lang.org/. It contains plenty of
resources about Scala all around.

There's a bare-bones overview of collections here:
http://docs.scala-lang.org/tutorials/FAQ/collections.html

There's a walk-through (by Odersky) here:
http://docs.scala-lang.org/overviews/collections/introduction.html

And the inner workings are explained here:
http://docs.scala-lang.org/overviews/core/architecture-of-scala-collecti...

>
> On Dec 16, 1:34 pm, Daniel Sobral wrote:
>> On Fri, Dec 16, 2011 at 19:15, Drew wrote:
>> > Looking into views a bit more, seems like they don't apply to
>> > Iterators, only Iterables
>>
>> That's because Iterators don't need views, since no new collection is
>> created when you call "map" on an Iterator.
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> > On Dec 16, 1:10 pm, Drew wrote:
>> >> Oh wow, that looks nice. I'm not too familiar with views. Would that
>> >> create an extra collection?
>>
>> >> -- Drew
>>
>> >> On Dec 16, 12:20 pm, Jason Zaugg wrote:
>>
>> >> > On Sat, Dec 17, 2011 at 6:17 AM, Drew wrote:
>> >> > > Hi Everyone,
>>
>> >> > > I was thinking that it would be useful to be able to pass a custom
>> >> > > function to turn elements into String to Itrable.mkString. Something
>> >> > > to replace
>>
>> >> > > items.foldLeft("")((str, item) => {
>> >> > >                        val toAppend = if (str.isEmpty()) {
>> >> > >                                item.toCustomString
>> >> > >                        } else {
>> >> > >                                "," + item.toCustomString
>> >> > >                        }
>> >> > >                        str + toAppend
>> >> > >                }
>>
>> >> > > so instead will be able to write
>>
>> >> > > items.mkString(",", _.toCustomString)
>>
>> >> > I think that this is sufficient:
>>
>> >> >   items.view.map(_.toCustomString).mkString(",")
>>
>> >> > -jason
>>
>> --
>> Daniel C. Sobral
>>
>> I travel to the future all the time.

Drew
Joined: 2011-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: (Idea) Customizable Iterable.mkString

Thanks Daniel

On Dec 16, 2011, at 2:25 PM, Daniel Sobral wrote:

> On Fri, Dec 16, 2011 at 19:51, Drew wrote:
>> Thanks guys. What's a good reading to get to know the inner workings
>> of the Scala Collection's classes. I'm pretty new to Scala and I've
>> heard that it's very easy to shoot yourself in the food (memory leaks,
>> etc) using Scala collections if you don't know what you're doing. For
>> example, I didn't know the semantics that you guys talked about such
>> as "no new collection is created when you call "map" on an Iterator. "
>> or
>
> Well, start here: http://docs.scala-lang.org/. It contains plenty of
> resources about Scala all around.
>
> There's a bare-bones overview of collections here:
> http://docs.scala-lang.org/tutorials/FAQ/collections.html
>
> There's a walk-through (by Odersky) here:
> http://docs.scala-lang.org/overviews/collections/introduction.html
>
> And the inner workings are explained here:
> http://docs.scala-lang.org/overviews/core/architecture-of-scala-collecti...
>
>>
>> On Dec 16, 1:34 pm, Daniel Sobral wrote:
>>> On Fri, Dec 16, 2011 at 19:15, Drew wrote:
>>>> Looking into views a bit more, seems like they don't apply to
>>>> Iterators, only Iterables
>>>
>>> That's because Iterators don't need views, since no new collection is
>>> created when you call "map" on an Iterator.
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>> On Dec 16, 1:10 pm, Drew wrote:
>>>>> Oh wow, that looks nice. I'm not too familiar with views. Would that
>>>>> create an extra collection?
>>>
>>>>> -- Drew
>>>
>>>>> On Dec 16, 12:20 pm, Jason Zaugg wrote:
>>>
>>>>>> On Sat, Dec 17, 2011 at 6:17 AM, Drew wrote:
>>>>>>> Hi Everyone,
>>>
>>>>>>> I was thinking that it would be useful to be able to pass a custom
>>>>>>> function to turn elements into String to Itrable.mkString. Something
>>>>>>> to replace
>>>
>>>>>>> items.foldLeft("")((str, item) => {
>>>>>>> val toAppend = if (str.isEmpty()) {
>>>>>>> item.toCustomString
>>>>>>> } else {
>>>>>>> "," + item.toCustomString
>>>>>>> }
>>>>>>> str + toAppend
>>>>>>> }
>>>
>>>>>>> so instead will be able to write
>>>
>>>>>>> items.mkString(",", _.toCustomString)
>>>
>>>>>> I think that this is sufficient:
>>>
>>>>>> items.view.map(_.toCustomString).mkString(",")
>>>
>>>>>> -jason
>>>
>>> --
>>> Daniel C. Sobral
>>>
>>> I travel to the future all the time.
>
>
>

Joshua.Suereth
Joined: 2008-09-02,
User offline. Last seen 32 weeks 5 days ago.
Re: Re: (Idea) Customizable Iterable.mkString

Just a note to be careful with Source.   It does not clean up resources.   I tend to use a traversable subclass to safely read lines of a file.

On Dec 16, 2011 4:30 PM, "Drew" <drew [at] venarc [dot] com> wrote:
Thanks Josh,

The problem that I'm trying to solve is that I'm reading the lines
from a file, parse it line by line and generate objects "Item" using
map() see

Source.fromURL("some file", "UTF-8").getLines().slice(start,
end).map((line) => {    parse line and create items})
That returns an Iterator. Now I want to create JSON from the
collection by using your method, but Iterator doesn't have view. Any
advice?



On Dec 16, 1:15 pm, Drew <d [dot] [dot] [dot] [at] venarc [dot] com> wrote:
> Looking into views a bit more, seems like they don't apply to
> Iterators, only Iterables
>
> On Dec 16, 1:10 pm, Drew <d [dot] [dot] [dot] [at] venarc [dot] com> wrote:
>
>
>
>
>
>
>
> > Oh wow, that looks nice. I'm not too familiar with views. Would that
> > create an extra collection?
>
> > -- Drew
>
> > On Dec 16, 12:20 pm, Jason Zaugg <jza [dot] [dot] [dot] [at] gmail [dot] com> wrote:
>
> > > On Sat, Dec 17, 2011 at 6:17 AM, Drew <d [dot] [dot] [dot] [at] venarc [dot] com> wrote:
> > > > Hi Everyone,
>
> > > > I was thinking that it would be useful to be able to pass a custom
> > > > function to turn elements into String to Itrable.mkString. Something
> > > > to replace
>
> > > > items.foldLeft("")((str, item) => {
> > > >                        val toAppend = if (str.isEmpty()) {
> > > >                                item.toCustomString
> > > >                        } else {
> > > >                                "," + item.toCustomString
> > > >                        }
> > > >                        str + toAppend
> > > >                }
>
> > > > so instead will be able to write
>
> > > > items.mkString(",", _.toCustomString)
>
> > > I think that this is sufficient:
>
> > >   items.view.map(_.toCustomString).mkString(",")
>
> > > -jason
Drew
Joined: 2011-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: (Idea) Customizable Iterable.mkString
Thanks for the tip
On Dec 16, 2011, at 3:48 PM, Josh Suereth wrote:

Just a note to be careful with Source.   It does not clean up resources.   I tend to use a traversable subclass to safely read lines of a file.

On Dec 16, 2011 4:30 PM, "Drew" <drew [at] venarc [dot] com> wrote:
Thanks Josh,

The problem that I'm trying to solve is that I'm reading the lines
from a file, parse it line by line and generate objects "Item" using
map() see

Source.fromURL("some file", "UTF-8").getLines().slice(start,
end).map((line) => {    parse line and create items})
That returns an Iterator. Now I want to create JSON from the
collection by using your method, but Iterator doesn't have view. Any
advice?



On Dec 16, 1:15 pm, Drew <d [dot] [dot] [dot] [at] venarc [dot] com> wrote:
> Looking into views a bit more, seems like they don't apply to
> Iterators, only Iterables
>
> On Dec 16, 1:10 pm, Drew <d [dot] [dot] [dot] [at] venarc [dot] com> wrote:
>
>
>
>
>
>
>
> > Oh wow, that looks nice. I'm not too familiar with views. Would that
> > create an extra collection?
>
> > -- Drew
>
> > On Dec 16, 12:20 pm, Jason Zaugg <jza [dot] [dot] [dot] [at] gmail [dot] com> wrote:
>
> > > On Sat, Dec 17, 2011 at 6:17 AM, Drew <d [dot] [dot] [dot] [at] venarc [dot] com> wrote:
> > > > Hi Everyone,
>
> > > > I was thinking that it would be useful to be able to pass a custom
> > > > function to turn elements into String to Itrable.mkString. Something
> > > > to replace
>
> > > > items.foldLeft("")((str, item) => {
> > > >                        val toAppend = if (str.isEmpty()) {
> > > >                                item.toCustomString
> > > >                        } else {
> > > >                                "," + item.toCustomString
> > > >                        }
> > > >                        str + toAppend
> > > >                }
>
> > > > so instead will be able to write
>
> > > > items.mkString(",", _.toCustomString)
>
> > > I think that this is sufficient:
>
> > >   items.view.map(_.toCustomString).mkString(",")
>
> > > -jason

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