Returning a list of functions, is this considered an OOP?












6















I learned this way from the Johns Hopkins MOOC R Programming a long time ago on Coursera. The idea was to return a list of functions that were defined in a father function's scope. For example:



newString <- function(s) {
l <- nchar(s)
return(list(
get = function() return(s),
len = function() return(l),
concate = function(cat) {
s <<- paste0(s, cat)
l <<- nchar(s)
},
find = function(pattern) return(grepl(pattern, s)),
substitute = function(pattern, sub) {
s <<- gsub(pattern, sub, s)
l <<- nchar(s)
}
))
}


This function returns a list of functions/methods which can manipulate the item "s". I can "new" this "object" calling the father function:



my <- newString("hellow")


And using the "methods" with $ just looks like OOP.



my$get()
# [1] "hellow"
my$len()
# [1] 6
my$substitute("w$", "")
my$get()
# [1] "hello"
my$len()
# [1] 5
my$concate(", world")
my$get()
# [1] "hello, world"
my$find("world$")
# [1] TRUE


To print the "object" directly, we can see it is a list of functions. And all these functions located in the same environment 0x103ca6e08, where the item s was also in.



my
# $get
# function ()
# return(s)
# <bytecode: 0x1099ac1e0>
# <environment: 0x103ca6e08>
#
# $len
# function ()
# return(l)
# <bytecode: 0x109a58058>
# <environment: 0x103ca6e08>
#
# $concate
# function (cat)
# {
# s <<- paste0(s, cat)
# l <<- nchar(s)
# }
# <bytecode: 0x1074fd4e8>
# <environment: 0x103ca6e08>
#
# $find
# function (pattern)
# return(grepl(pattern, s))
# <bytecode: 0x1076c8470>
# <environment: 0x103ca6e08>
#
# $substitute
# function (pattern, sub)
# {
# s <<- gsub(pattern, sub, s)
# l <<- nchar(s)
# }
# <bytecode: 0x1077ad270>
# <environment: 0x103ca6e08>


So is this style of programming (?) considered OOP or OOP-like? What is the difference of this from S3/S4?





Thanks to @G.Grothendieck, @r2evans and @Jozef. The demo documentation for scoping in R says "functions can encapsulate state information", because of the scoping rules in R. And the RC system "uses environment", so I think what I did was similar to a primitive RC system.







"An object is data with functions. A closure is a function with data." -- John D. Cook




Closures get their name because they enclose the environment of the parent function and can access all its variables.




In http://adv-r.had.co.nz/Functional-programming.html#closures I found the most proper name for this is a closure.










share|improve this question




















  • 2





    what you did resembles Functional Programming than anything else. Especially read the section on First-class and higher-order functions

    – Vivek Kalyanarangan
    Dec 31 '18 at 10:15






  • 1





    Try demo("scoping")

    – G. Grothendieck
    Dec 31 '18 at 12:10






  • 1





    adv-r.had.co.nz/OO-essentials.html contains a decent comparison of S3, S4, and RC.

    – r2evans
    Dec 31 '18 at 16:36











  • @G.Grothendieck The demo shows exactly the case! Because "functions can encapsulate state information" in R, this style of programming is not actually considered "functional programming", right?

    – Lytze
    Jan 1 at 12:59











  • OO and functional programming don't have a single meaning but newString can be regarded as a class which stamps out objects such as my. newString does pass around functions and that can be regarded as functional programming; on the other hand, functional programming usually is regarded as side-effect free and the my object can be modified which is more OO than functional.

    – G. Grothendieck
    Jan 1 at 13:48
















6















I learned this way from the Johns Hopkins MOOC R Programming a long time ago on Coursera. The idea was to return a list of functions that were defined in a father function's scope. For example:



newString <- function(s) {
l <- nchar(s)
return(list(
get = function() return(s),
len = function() return(l),
concate = function(cat) {
s <<- paste0(s, cat)
l <<- nchar(s)
},
find = function(pattern) return(grepl(pattern, s)),
substitute = function(pattern, sub) {
s <<- gsub(pattern, sub, s)
l <<- nchar(s)
}
))
}


This function returns a list of functions/methods which can manipulate the item "s". I can "new" this "object" calling the father function:



my <- newString("hellow")


And using the "methods" with $ just looks like OOP.



my$get()
# [1] "hellow"
my$len()
# [1] 6
my$substitute("w$", "")
my$get()
# [1] "hello"
my$len()
# [1] 5
my$concate(", world")
my$get()
# [1] "hello, world"
my$find("world$")
# [1] TRUE


To print the "object" directly, we can see it is a list of functions. And all these functions located in the same environment 0x103ca6e08, where the item s was also in.



my
# $get
# function ()
# return(s)
# <bytecode: 0x1099ac1e0>
# <environment: 0x103ca6e08>
#
# $len
# function ()
# return(l)
# <bytecode: 0x109a58058>
# <environment: 0x103ca6e08>
#
# $concate
# function (cat)
# {
# s <<- paste0(s, cat)
# l <<- nchar(s)
# }
# <bytecode: 0x1074fd4e8>
# <environment: 0x103ca6e08>
#
# $find
# function (pattern)
# return(grepl(pattern, s))
# <bytecode: 0x1076c8470>
# <environment: 0x103ca6e08>
#
# $substitute
# function (pattern, sub)
# {
# s <<- gsub(pattern, sub, s)
# l <<- nchar(s)
# }
# <bytecode: 0x1077ad270>
# <environment: 0x103ca6e08>


So is this style of programming (?) considered OOP or OOP-like? What is the difference of this from S3/S4?





Thanks to @G.Grothendieck, @r2evans and @Jozef. The demo documentation for scoping in R says "functions can encapsulate state information", because of the scoping rules in R. And the RC system "uses environment", so I think what I did was similar to a primitive RC system.







"An object is data with functions. A closure is a function with data." -- John D. Cook




Closures get their name because they enclose the environment of the parent function and can access all its variables.




In http://adv-r.had.co.nz/Functional-programming.html#closures I found the most proper name for this is a closure.










share|improve this question




















  • 2





    what you did resembles Functional Programming than anything else. Especially read the section on First-class and higher-order functions

    – Vivek Kalyanarangan
    Dec 31 '18 at 10:15






  • 1





    Try demo("scoping")

    – G. Grothendieck
    Dec 31 '18 at 12:10






  • 1





    adv-r.had.co.nz/OO-essentials.html contains a decent comparison of S3, S4, and RC.

    – r2evans
    Dec 31 '18 at 16:36











  • @G.Grothendieck The demo shows exactly the case! Because "functions can encapsulate state information" in R, this style of programming is not actually considered "functional programming", right?

    – Lytze
    Jan 1 at 12:59











  • OO and functional programming don't have a single meaning but newString can be regarded as a class which stamps out objects such as my. newString does pass around functions and that can be regarded as functional programming; on the other hand, functional programming usually is regarded as side-effect free and the my object can be modified which is more OO than functional.

    – G. Grothendieck
    Jan 1 at 13:48














6












6








6








I learned this way from the Johns Hopkins MOOC R Programming a long time ago on Coursera. The idea was to return a list of functions that were defined in a father function's scope. For example:



newString <- function(s) {
l <- nchar(s)
return(list(
get = function() return(s),
len = function() return(l),
concate = function(cat) {
s <<- paste0(s, cat)
l <<- nchar(s)
},
find = function(pattern) return(grepl(pattern, s)),
substitute = function(pattern, sub) {
s <<- gsub(pattern, sub, s)
l <<- nchar(s)
}
))
}


This function returns a list of functions/methods which can manipulate the item "s". I can "new" this "object" calling the father function:



my <- newString("hellow")


And using the "methods" with $ just looks like OOP.



my$get()
# [1] "hellow"
my$len()
# [1] 6
my$substitute("w$", "")
my$get()
# [1] "hello"
my$len()
# [1] 5
my$concate(", world")
my$get()
# [1] "hello, world"
my$find("world$")
# [1] TRUE


To print the "object" directly, we can see it is a list of functions. And all these functions located in the same environment 0x103ca6e08, where the item s was also in.



my
# $get
# function ()
# return(s)
# <bytecode: 0x1099ac1e0>
# <environment: 0x103ca6e08>
#
# $len
# function ()
# return(l)
# <bytecode: 0x109a58058>
# <environment: 0x103ca6e08>
#
# $concate
# function (cat)
# {
# s <<- paste0(s, cat)
# l <<- nchar(s)
# }
# <bytecode: 0x1074fd4e8>
# <environment: 0x103ca6e08>
#
# $find
# function (pattern)
# return(grepl(pattern, s))
# <bytecode: 0x1076c8470>
# <environment: 0x103ca6e08>
#
# $substitute
# function (pattern, sub)
# {
# s <<- gsub(pattern, sub, s)
# l <<- nchar(s)
# }
# <bytecode: 0x1077ad270>
# <environment: 0x103ca6e08>


So is this style of programming (?) considered OOP or OOP-like? What is the difference of this from S3/S4?





Thanks to @G.Grothendieck, @r2evans and @Jozef. The demo documentation for scoping in R says "functions can encapsulate state information", because of the scoping rules in R. And the RC system "uses environment", so I think what I did was similar to a primitive RC system.







"An object is data with functions. A closure is a function with data." -- John D. Cook




Closures get their name because they enclose the environment of the parent function and can access all its variables.




In http://adv-r.had.co.nz/Functional-programming.html#closures I found the most proper name for this is a closure.










share|improve this question
















I learned this way from the Johns Hopkins MOOC R Programming a long time ago on Coursera. The idea was to return a list of functions that were defined in a father function's scope. For example:



newString <- function(s) {
l <- nchar(s)
return(list(
get = function() return(s),
len = function() return(l),
concate = function(cat) {
s <<- paste0(s, cat)
l <<- nchar(s)
},
find = function(pattern) return(grepl(pattern, s)),
substitute = function(pattern, sub) {
s <<- gsub(pattern, sub, s)
l <<- nchar(s)
}
))
}


This function returns a list of functions/methods which can manipulate the item "s". I can "new" this "object" calling the father function:



my <- newString("hellow")


And using the "methods" with $ just looks like OOP.



my$get()
# [1] "hellow"
my$len()
# [1] 6
my$substitute("w$", "")
my$get()
# [1] "hello"
my$len()
# [1] 5
my$concate(", world")
my$get()
# [1] "hello, world"
my$find("world$")
# [1] TRUE


To print the "object" directly, we can see it is a list of functions. And all these functions located in the same environment 0x103ca6e08, where the item s was also in.



my
# $get
# function ()
# return(s)
# <bytecode: 0x1099ac1e0>
# <environment: 0x103ca6e08>
#
# $len
# function ()
# return(l)
# <bytecode: 0x109a58058>
# <environment: 0x103ca6e08>
#
# $concate
# function (cat)
# {
# s <<- paste0(s, cat)
# l <<- nchar(s)
# }
# <bytecode: 0x1074fd4e8>
# <environment: 0x103ca6e08>
#
# $find
# function (pattern)
# return(grepl(pattern, s))
# <bytecode: 0x1076c8470>
# <environment: 0x103ca6e08>
#
# $substitute
# function (pattern, sub)
# {
# s <<- gsub(pattern, sub, s)
# l <<- nchar(s)
# }
# <bytecode: 0x1077ad270>
# <environment: 0x103ca6e08>


So is this style of programming (?) considered OOP or OOP-like? What is the difference of this from S3/S4?





Thanks to @G.Grothendieck, @r2evans and @Jozef. The demo documentation for scoping in R says "functions can encapsulate state information", because of the scoping rules in R. And the RC system "uses environment", so I think what I did was similar to a primitive RC system.







"An object is data with functions. A closure is a function with data." -- John D. Cook




Closures get their name because they enclose the environment of the parent function and can access all its variables.




In http://adv-r.had.co.nz/Functional-programming.html#closures I found the most proper name for this is a closure.







r






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 10:56







Lytze

















asked Dec 31 '18 at 9:34









LytzeLytze

372310




372310








  • 2





    what you did resembles Functional Programming than anything else. Especially read the section on First-class and higher-order functions

    – Vivek Kalyanarangan
    Dec 31 '18 at 10:15






  • 1





    Try demo("scoping")

    – G. Grothendieck
    Dec 31 '18 at 12:10






  • 1





    adv-r.had.co.nz/OO-essentials.html contains a decent comparison of S3, S4, and RC.

    – r2evans
    Dec 31 '18 at 16:36











  • @G.Grothendieck The demo shows exactly the case! Because "functions can encapsulate state information" in R, this style of programming is not actually considered "functional programming", right?

    – Lytze
    Jan 1 at 12:59











  • OO and functional programming don't have a single meaning but newString can be regarded as a class which stamps out objects such as my. newString does pass around functions and that can be regarded as functional programming; on the other hand, functional programming usually is regarded as side-effect free and the my object can be modified which is more OO than functional.

    – G. Grothendieck
    Jan 1 at 13:48














  • 2





    what you did resembles Functional Programming than anything else. Especially read the section on First-class and higher-order functions

    – Vivek Kalyanarangan
    Dec 31 '18 at 10:15






  • 1





    Try demo("scoping")

    – G. Grothendieck
    Dec 31 '18 at 12:10






  • 1





    adv-r.had.co.nz/OO-essentials.html contains a decent comparison of S3, S4, and RC.

    – r2evans
    Dec 31 '18 at 16:36











  • @G.Grothendieck The demo shows exactly the case! Because "functions can encapsulate state information" in R, this style of programming is not actually considered "functional programming", right?

    – Lytze
    Jan 1 at 12:59











  • OO and functional programming don't have a single meaning but newString can be regarded as a class which stamps out objects such as my. newString does pass around functions and that can be regarded as functional programming; on the other hand, functional programming usually is regarded as side-effect free and the my object can be modified which is more OO than functional.

    – G. Grothendieck
    Jan 1 at 13:48








2




2





what you did resembles Functional Programming than anything else. Especially read the section on First-class and higher-order functions

– Vivek Kalyanarangan
Dec 31 '18 at 10:15





what you did resembles Functional Programming than anything else. Especially read the section on First-class and higher-order functions

– Vivek Kalyanarangan
Dec 31 '18 at 10:15




1




1





Try demo("scoping")

– G. Grothendieck
Dec 31 '18 at 12:10





Try demo("scoping")

– G. Grothendieck
Dec 31 '18 at 12:10




1




1





adv-r.had.co.nz/OO-essentials.html contains a decent comparison of S3, S4, and RC.

– r2evans
Dec 31 '18 at 16:36





adv-r.had.co.nz/OO-essentials.html contains a decent comparison of S3, S4, and RC.

– r2evans
Dec 31 '18 at 16:36













@G.Grothendieck The demo shows exactly the case! Because "functions can encapsulate state information" in R, this style of programming is not actually considered "functional programming", right?

– Lytze
Jan 1 at 12:59





@G.Grothendieck The demo shows exactly the case! Because "functions can encapsulate state information" in R, this style of programming is not actually considered "functional programming", right?

– Lytze
Jan 1 at 12:59













OO and functional programming don't have a single meaning but newString can be regarded as a class which stamps out objects such as my. newString does pass around functions and that can be regarded as functional programming; on the other hand, functional programming usually is regarded as side-effect free and the my object can be modified which is more OO than functional.

– G. Grothendieck
Jan 1 at 13:48





OO and functional programming don't have a single meaning but newString can be regarded as a class which stamps out objects such as my. newString does pass around functions and that can be regarded as functional programming; on the other hand, functional programming usually is regarded as side-effect free and the my object can be modified which is more OO than functional.

– G. Grothendieck
Jan 1 at 13:48












1 Answer
1






active

oldest

votes


















4














What you are trying to do here reminds me of reference classes or an alternative implementation of the concept by the R6 package - essentially trying to make a OO system similar to other "classic" OOP languages (e.g. Java):



Reference classes:




  • https://www.rdocumentation.org/packages/methods/versions/3.5.1/topics/ReferenceClasses


R6




  • https://github.com/r-lib/R6/blob/master/README.md


For example, you could define an R6 class like so:



library(R6)

Person <- R6Class("Person",
public = list(
name = NULL,
hair = NULL,
initialize = function(name = NA, hair = NA) {
self$name <- name
self$hair <- hair
self$greet()
},
set_hair = function(val) {
self$hair <- val
},
greet = function() {
cat(paste0("Hello, my name is ", self$name, ".n"))
}
)
)


And then one can create an instance from that class:



ann <- Person$new("Ann", "black")


For a quick introduction: https://r6.r-lib.org/articles/Introduction.html






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53985852%2freturning-a-list-of-functions-is-this-considered-an-oop%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














    What you are trying to do here reminds me of reference classes or an alternative implementation of the concept by the R6 package - essentially trying to make a OO system similar to other "classic" OOP languages (e.g. Java):



    Reference classes:




    • https://www.rdocumentation.org/packages/methods/versions/3.5.1/topics/ReferenceClasses


    R6




    • https://github.com/r-lib/R6/blob/master/README.md


    For example, you could define an R6 class like so:



    library(R6)

    Person <- R6Class("Person",
    public = list(
    name = NULL,
    hair = NULL,
    initialize = function(name = NA, hair = NA) {
    self$name <- name
    self$hair <- hair
    self$greet()
    },
    set_hair = function(val) {
    self$hair <- val
    },
    greet = function() {
    cat(paste0("Hello, my name is ", self$name, ".n"))
    }
    )
    )


    And then one can create an instance from that class:



    ann <- Person$new("Ann", "black")


    For a quick introduction: https://r6.r-lib.org/articles/Introduction.html






    share|improve this answer






























      4














      What you are trying to do here reminds me of reference classes or an alternative implementation of the concept by the R6 package - essentially trying to make a OO system similar to other "classic" OOP languages (e.g. Java):



      Reference classes:




      • https://www.rdocumentation.org/packages/methods/versions/3.5.1/topics/ReferenceClasses


      R6




      • https://github.com/r-lib/R6/blob/master/README.md


      For example, you could define an R6 class like so:



      library(R6)

      Person <- R6Class("Person",
      public = list(
      name = NULL,
      hair = NULL,
      initialize = function(name = NA, hair = NA) {
      self$name <- name
      self$hair <- hair
      self$greet()
      },
      set_hair = function(val) {
      self$hair <- val
      },
      greet = function() {
      cat(paste0("Hello, my name is ", self$name, ".n"))
      }
      )
      )


      And then one can create an instance from that class:



      ann <- Person$new("Ann", "black")


      For a quick introduction: https://r6.r-lib.org/articles/Introduction.html






      share|improve this answer




























        4












        4








        4







        What you are trying to do here reminds me of reference classes or an alternative implementation of the concept by the R6 package - essentially trying to make a OO system similar to other "classic" OOP languages (e.g. Java):



        Reference classes:




        • https://www.rdocumentation.org/packages/methods/versions/3.5.1/topics/ReferenceClasses


        R6




        • https://github.com/r-lib/R6/blob/master/README.md


        For example, you could define an R6 class like so:



        library(R6)

        Person <- R6Class("Person",
        public = list(
        name = NULL,
        hair = NULL,
        initialize = function(name = NA, hair = NA) {
        self$name <- name
        self$hair <- hair
        self$greet()
        },
        set_hair = function(val) {
        self$hair <- val
        },
        greet = function() {
        cat(paste0("Hello, my name is ", self$name, ".n"))
        }
        )
        )


        And then one can create an instance from that class:



        ann <- Person$new("Ann", "black")


        For a quick introduction: https://r6.r-lib.org/articles/Introduction.html






        share|improve this answer















        What you are trying to do here reminds me of reference classes or an alternative implementation of the concept by the R6 package - essentially trying to make a OO system similar to other "classic" OOP languages (e.g. Java):



        Reference classes:




        • https://www.rdocumentation.org/packages/methods/versions/3.5.1/topics/ReferenceClasses


        R6




        • https://github.com/r-lib/R6/blob/master/README.md


        For example, you could define an R6 class like so:



        library(R6)

        Person <- R6Class("Person",
        public = list(
        name = NULL,
        hair = NULL,
        initialize = function(name = NA, hair = NA) {
        self$name <- name
        self$hair <- hair
        self$greet()
        },
        set_hair = function(val) {
        self$hair <- val
        },
        greet = function() {
        cat(paste0("Hello, my name is ", self$name, ".n"))
        }
        )
        )


        And then one can create an instance from that class:



        ann <- Person$new("Ann", "black")


        For a quick introduction: https://r6.r-lib.org/articles/Introduction.html







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 3 at 8:08

























        answered Dec 31 '18 at 11:25









        JozefJozef

        1,031510




        1,031510






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53985852%2freturning-a-list-of-functions-is-this-considered-an-oop%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas