Master functions and discover how to write functional programs in R. In this book, updated for R 4, you'll learn to make your functions pure by avoiding side effects, write functions that manipulate other functions, and construct complex functions using simpler functions as building blocks.
In Functional Programming in R 4, you’ll see how to replace loops, which can have side-effects, with recursive functions that can more easily avoid them. In addition, the book covers why you shouldn't use recursion when loops are more efficient and how you can get the best of both worlds.
Functional programming is a style of programming, like object-oriented programming, but one that focuses on data transformations and calculations rather than objects and state. Where in object-oriented programming you model your programs by describing which states an object can be in and how methods will reveal or modify that state, in functional programming you model programs by describing how functions translate input data to output data. Functions themselves are considered to be data you can manipulate and much of the strength of functional programming comes from manipulating functions; that is, building more complex functions by combining simpler functions.
What You'll Learn:
Write functions in R 4, including infix operators and replacement functions
Create higher order functions
Pass functions to other functions and start using functions as data you can manipulate
Use Filer, Map and Reduce functions to express the intent behind code clearly and safely
Build new functions from existing functions without necessarily writing any new functions, using point-free programming
Create functions that carry data along with them
Who This Book Is For:
Those with at least some experience with programming in R.
Author(s): Thomas Mailund
Edition: 2
Publisher: Apress
Year: 2023
Language: English
Pages: 166
logo
Hello, Welcome to EPUB Reader
Click button to select your book
Open EPUB book
This Online Web App is made by Neo Reader for experimental purpose, it is a very simple EPUB Reader. We recommend you try our Neo Reader for better experience.
Take a look now
neat reader pc
AD
Ultimate EPUB Reader
Totally free to try
Support multiple file types, such as EPUB, MOBI, AZW3, AZW, PDF and TXT.
Learn more about Neo Reader
General Ebook Solution
Table of Contents
About the Author
About the Technical Reviewer
Acknowledgments
Chapter 1: Introduction
Chapter 2: Functions in R
Writing Functions in R
Named Parameters and Default Parameters
The “Gobble Up Everything Else” Parameter: “...”
Lazy Evaluation
Functions Don’t Have Names
Vectorized Functions
Infix Operators
Replacement Functions
Untitled
Chapter 3: Pure Functional Programming
Writing Pure Functions
Recursion As Loops
The Structure of a Recursive Function
Tail-Recursion
Runtime Considerations
Chapter 4: Scope and Closures
Environments and Functions
Environment Chains, Scope, and Function Calls
Scopes, Lazy Evaluation, and Default Parameters
Nested Functions and Scopes
Closures
Reaching Outside Your Innermost Scope
Lexical and Dynamic Scope
Chapter 5: Higher-Order Functions
Currying
A Parameter Binding Function
Continuation-Passing Style
Thunks and Trampolines
Chapter 6: Filter, Map, and Reduce
The General Sequence Object in R Is a List
Filtering Sequences
Mapping over Sequences
Reducing Sequences
Bringing the Functions Together
The Apply Family of Functions
sapply, vapply, and lapply
The apply Function
The tapply Function
Functional Programming in purrr
Filter-like Functions
Map-like Functions
Reduce-like Functions
Chapter 7: Point-Free Programming
Function Composition
Pipelines
Chapter 8: Conclusions
Index