library(R6)
Fibonnacci <- R6Class("Fibonnacci",
                       public = list(
                         
                         # objects
                         init = c(0, 1),
                         last = 1,
                         current = 1,

                         # functions
                         fib_next = function(){ # a method
                           
                           x <- self$last
                           y <- self$current
                           
                           self$last <- y
                           self$current <- x + y
                           invisible(self)          #returns
                           
                                     },
                         
                         fib_init = function(x1, x2){
                           
                           self$init <- c(x1, x2)
                           self$last <- x2
                           self$current <- x1 + x2
                           invisible(self)            #returns
                           
                         },
                         print = function() {  # print method; default is to print everything
                           
                           cat(self$current)
                           
                         }
                       )
                      
                  
)
library(magrittr)
my_fib <- Fibonnacci$new()
my_fib %>% class()
## [1] "Fibonnacci" "R6"
my_fib %>% str()
## Classes 'Fibonnacci', 'R6' <Fibonnacci>
##   Public:
##     clone: function (deep = FALSE) 
##     current: 1
##     fib_init: function (x1, x2) 
##     fib_next: function () 
##     init: 0 1
##     last: 1
##     print: function ()
my_fib$fib_next()
my_fib$fib_next() 
my_fib$fib_next() 
my_fib$fib_next() 
my_fib$fib_next() 
my_fib$fib_next() 
my_fib$fib_next() 
my_fib$fib_init(5,4) 
my_fib$fib_next() 
my_fib$fib_next() 
my_fib$fib_next() 
my_fib$fib_next() 

my_fib$current
## [1] 57
## wrap and pipe
fibi_init <- function(x1 = 0, x2 = 1){
  
  my_fib <- Fibonnacci$new()
  
  my_fib$fib_init(x1,x2)
  
  my_fib
  
}

fibi_advance <- function(my_fib){
  
  my_fib <- my_fib
  
  my_fib$fib_next()
  
  my_fib
  
}
fibi_init() %>% 
  fibi_advance()  %>% 
  fibi_advance()  %>% 
  fibi_advance()  %>% 
  fibi_advance()  %>% 
  fibi_advance()  %>% 
  fibi_advance()
## 21