Singleton pattern

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Singleton pattern

deivit
Hi all,

I know it may not have much sense thinking about a Singleton Pattern in an
R application which doesn't use any OOP facilities, however I'm curious to
know if anybody faced the same issue. I've been googling but using
"singleton pattern" as a key word leads to typical OOP languages like Java
or C++ among others.

So my problem is that I'd like to ensure some very big objects aren't
copied again and again in some other variables. In the worst case I'll
check all code by myself to ensure it but in this case the application
won't force programmers to take it in consideration which is what I am
really looking for.

Any advice will be highly appreciated :P

Thanks!
--
*David Cassany Viladomat
Software Developer
Transmural Biote**ch S.L*

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Singleton pattern

jverzaniNWBKZ
David Cassany <david.cassany <at> transmuralbiotech.com> writes:

>
> Hi all,
>
> I know it may not have much sense thinking about a Singleton Pattern in an
> R application which doesn't use any OOP facilities, however I'm curious to
> know if anybody faced the same issue. I've been googling but using
> "singleton pattern" as a key word leads to typical OOP languages like Java
> or C++ among others.
>

While it isn't too hard to implement the Singleton pattern using reference
classes, I would think for what you want to do the memoise package can
be used. Create a wrapper function to return the objects and the cached
value will be returned each time.


> So my problem is that I'd like to ensure some very big objects aren't
> copied again and again in some other variables. In the worst case I'll
> check all code by myself to ensure it but in this case the application
> won't force programmers to take it in consideration which is what I am
> really looking for.
>
> Any advice will be highly appreciated :P
>
> Thanks!

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Singleton pattern

Bryan Hanson
In reply to this post by deivit
Since no one else has "bit", I'll take a stab.  I'm an experienced R person, but I've recently been teaching myself objective-c and I've been using singletons quite a bit (and mis-using them quite a bit!).  Not a computer scientist at all.  You've been warned.

I don't think there is a comparable concept in R.  You do have a choice of S3 or S4 classes for your object orientation in R.  S3 is very loose in that you can add to S3 objects readily and abuse them a lot.  There really is no checking of them unless you implement it manually.  S4 objects are much "tighter" and they are less readily modified and are self-checking (I know some will complain about this characterization but  it's approximately correct).  So perhaps you want an S4 object so it's less likely to get mangled, but I doubt there is a way to prevent users from copying it, which would be more along the lines of a singleton.

You can google the archives for some great discussions of S3 vs S4 if that sounds interesting.

Bryan

***********
Bryan Hanson
Professor of Chemistry & Biochemistry
DePauw University

On Mar 16, 2012, at 7:47 AM, David Cassany wrote:

> Hi all,
>
> I know it may not have much sense thinking about a Singleton Pattern in an
> R application which doesn't use any OOP facilities, however I'm curious to
> know if anybody faced the same issue. I've been googling but using
> "singleton pattern" as a key word leads to typical OOP languages like Java
> or C++ among others.
>
> So my problem is that I'd like to ensure some very big objects aren't
> copied again and again in some other variables. In the worst case I'll
> check all code by myself to ensure it but in this case the application
> won't force programmers to take it in consideration which is what I am
> really looking for.
>
> Any advice will be highly appreciated :P
>
> Thanks!
> --
> *David Cassany Viladomat
> Software Developer
> Transmural Biote**ch S.L*
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Singleton pattern

Jan Kim
Using the singleton pattern in R has never occurred to me so far, as
I think it applies to languages that support multiple references to
one instance. R doesn't do that, at least not in ways that would be
required for applying the singleton pattern as described in the GoF book,
anyway. One would have to use closures and / or environments to
approximate references, I suppose.

When passed around as parameters, R objects don't get copied unless
the called function starts modifying them, so if the primary concern
is to prevent unnecessary / costly copying of bulky objects, creating
the thing once and then passing it around as necessary, taking care
that called functions don't change it, is perhaps good enough.

Best regards, Jan

On Fri, Mar 16, 2012 at 12:15:27PM -0400, Bryan Hanson wrote:

> Since no one else has "bit", I'll take a stab.  I'm an experienced R person, but I've recently been teaching myself objective-c and I've been using singletons quite a bit (and mis-using them quite a bit!).  Not a computer scientist at all.  You've been warned.
>
> I don't think there is a comparable concept in R.  You do have a choice of S3 or S4 classes for your object orientation in R.  S3 is very loose in that you can add to S3 objects readily and abuse them a lot.  There really is no checking of them unless you implement it manually.  S4 objects are much "tighter" and they are less readily modified and are self-checking (I know some will complain about this characterization but  it's approximately correct).  So perhaps you want an S4 object so it's less likely to get mangled, but I doubt there is a way to prevent users from copying it, which would be more along the lines of a singleton.
>
> You can google the archives for some great discussions of S3 vs S4 if that sounds interesting.
>
> Bryan
>
> ***********
> Bryan Hanson
> Professor of Chemistry & Biochemistry
> DePauw University
>
> On Mar 16, 2012, at 7:47 AM, David Cassany wrote:
>
> > Hi all,
> >
> > I know it may not have much sense thinking about a Singleton Pattern in an
> > R application which doesn't use any OOP facilities, however I'm curious to
> > know if anybody faced the same issue. I've been googling but using
> > "singleton pattern" as a key word leads to typical OOP languages like Java
> > or C++ among others.
> >
> > So my problem is that I'd like to ensure some very big objects aren't
> > copied again and again in some other variables. In the worst case I'll
> > check all code by myself to ensure it but in this case the application
> > won't force programmers to take it in consideration which is what I am
> > really looking for.
> >
> > Any advice will be highly appreciated :P
> >
> > Thanks!
> > --
> > *David Cassany Viladomat
> > Software Developer
> > Transmural Biote**ch S.L*
> >
> > [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

--
 +- Jan T. Kim -------------------------------------------------------+
 |             email: [hidden email]                                |
 |             WWW:   http://www.jtkim.dreamhosters.com/              |
 *-----=<  hierarchical systems are for files, not for humans  >=-----*

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Singleton pattern

deivit
Thanks all of your answers and advices! They brought me some light!

I'll have a look to memois package and to tracemem function in order to
check if they can help me somehow, at least to understand and trace in
detail how memory gets consumed.

Thank you all!
David

2012/3/16 Jan T. Kim <[hidden email]>

> Using the singleton pattern in R has never occurred to me so far, as
> I think it applies to languages that support multiple references to
> one instance. R doesn't do that, at least not in ways that would be
> required for applying the singleton pattern as described in the GoF book,
> anyway. One would have to use closures and / or environments to
> approximate references, I suppose.
>
> When passed around as parameters, R objects don't get copied unless
> the called function starts modifying them, so if the primary concern
> is to prevent unnecessary / costly copying of bulky objects, creating
> the thing once and then passing it around as necessary, taking care
> that called functions don't change it, is perhaps good enough.
>
> Best regards, Jan
>
> On Fri, Mar 16, 2012 at 12:15:27PM -0400, Bryan Hanson wrote:
> > Since no one else has "bit", I'll take a stab.  I'm an experienced R
> person, but I've recently been teaching myself objective-c and I've been
> using singletons quite a bit (and mis-using them quite a bit!).  Not a
> computer scientist at all.  You've been warned.
> >
> > I don't think there is a comparable concept in R.  You do have a choice
> of S3 or S4 classes for your object orientation in R.  S3 is very loose in
> that you can add to S3 objects readily and abuse them a lot.  There really
> is no checking of them unless you implement it manually.  S4 objects are
> much "tighter" and they are less readily modified and are self-checking (I
> know some will complain about this characterization but  it's approximately
> correct).  So perhaps you want an S4 object so it's less likely to get
> mangled, but I doubt there is a way to prevent users from copying it, which
> would be more along the lines of a singleton.
> >
> > You can google the archives for some great discussions of S3 vs S4 if
> that sounds interesting.
> >
> > Bryan
> >
> > ***********
> > Bryan Hanson
> > Professor of Chemistry & Biochemistry
> > DePauw University
> >
> > On Mar 16, 2012, at 7:47 AM, David Cassany wrote:
> >
> > > Hi all,
> > >
> > > I know it may not have much sense thinking about a Singleton Pattern
> in an
> > > R application which doesn't use any OOP facilities, however I'm
> curious to
> > > know if anybody faced the same issue. I've been googling but using
> > > "singleton pattern" as a key word leads to typical OOP languages like
> Java
> > > or C++ among others.
> > >
> > > So my problem is that I'd like to ensure some very big objects aren't
> > > copied again and again in some other variables. In the worst case I'll
> > > check all code by myself to ensure it but in this case the application
> > > won't force programmers to take it in consideration which is what I am
> > > really looking for.
> > >
> > > Any advice will be highly appreciated :P
> > >
> > > Thanks!
> > > --
> > > *David Cassany Viladomat
> > > Software Developer
> > > Transmural Biote**ch S.L*
> > >
> > >     [[alternative HTML version deleted]]
> > >
> > > ______________________________________________
> > > [hidden email] mailing list
> > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> > > and provide commented, minimal, self-contained, reproducible code.
> >
> > ______________________________________________
> > [hidden email] mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>
> --
>  +- Jan T. Kim -------------------------------------------------------+
>  |             email: [hidden email]                                |
>  |             WWW:   http://www.jtkim.dreamhosters.com/              |
>  *-----=<  hierarchical systems are for files, not for humans  >=-----*
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



--
*David Cassany Viladomat
Software Developer
Transmural Biote**ch S.L*
**

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.