Day 11

Author

Julia Romanowska

Data

Monkeys 🐒 are having fun with my stuff!!!

Functions and objects

I will need to create classes, I think.

🐒 Monkey

new_monkey <- function(id, things, operation, test){
  structure(id, things, operation, test)
}

🎁 Thing

A class with attributes worry_level and monkey (which monkey holds it).

new_thing <- function(id, worry_level, monkey){
  stopifnot(is.numeric(worry_level))
  structure(id, worry_level, monkey = monkey, class = "thing")
}
print.thing <- function(x, ...){
  cat("This thing (", id, ") is associated with current worry level of ",
      x$worry_level, " and is being hold by a monkey no.", x$monkey, "\n")
}

change_worry_level(thing, operation){
  old <- thing$worry_level
  new <- NA
  eval(parse_expr(operation))
  thing$worry_level <- new
  return(thing)
}
change_monkey_holding(thing, old_monkey, new_monkey){
  remove_thing(old_monkey, thing$id)
  add_thing(new_monkey, thing$id)
  thing$monkey <- new_monkey$id
  return(thing)
}