ComenzarEmpieza gratis

Primitive Generic Functions

Some core functionality of R is defined using primitive functions, which use a special technique for accessing C-code, for performance reasons. Examples of primitive functions include language elements, like if and for, operators like + and $, and mathematical functions like exp and sin. Primitive functions include S3 generics; the complete list of S3 primitive generics can be found using .S3PrimitiveGenerics (docs).

When an S3 generic is primitive, its lookup process to find methods works slightly differently. R will look for methods using the class (docs), as normal, but if nothing is found, the internal C-code function will be called. (Compare this to regular generics, where an error is thrown if no method is found.) This means that if you override the class of an object, fundamental behavior like calculating the length will not be broken.

Este ejercicio forma parte del curso

Object-Oriented Programming with S3 and R6 in R

Ver curso

Instrucciones del ejercicio

A list object, with the class overridden to be "hairstylist" has been assigned in your workspace to the variable hair.

  • View the structure of hair with str() (docs).
  • List all the S3 primitive generics that R knows about.
  • Use the exists() (docs) function to check that no function named length.hairstylist exists. Note that this function takes a character input.
  • Calculate the length() (docs) of the hair variable.

Ejercicio interactivo práctico

Prueba este ejercicio completando el código de muestra.

# View the structure of hair
___

# What primitive generics are available?
___

# Does length.hairstylist exist?
___

# What is the length of hair?
___
Editar y ejecutar código