I've tried to create a reprex to the best of my ability. I have to read in a .txt file and split it using different regular expressions a bunch and would like to save some code. I'm wondering if it is possible to solve this problem using compose() and partial() or if I should create a function, the way I'm trying to do it I keep getting an error. For example:
library(tidyverse)
textfile <- "https://byuistats.github.io/M335/data/randomletters.txt"
read_and_split <- compose(partial(str_split, simplify = T),
read_lines)
# I would use it in the following ways
read_and_split(textfile, pattern = "")
#> Error in last(...): unused argument (pattern = "")
read_and_split(textfile, pattern = "[^0-9]+")
#> Error in last(...): unused argument (pattern = "[^0-9]+")
Am I going about this the wrong way (trying to spice up my purrr chops) or should I refactor this into a function? I'm pretty sure the technical term regarding this is 'currying' but haven't found many helpful resources explaining how we do it in R (besides the purrr mission statement saying (...) is a replacement for it).
@technocrat is correct.
Composing usually means that output of one function is exactly the input of the second function. This also means that input of first function should be the input to only first function. Your hunch about currying is correct, but in R we usually do it with partial as you did in your example. However, if I understood you correctly, you want to have multiple functions with possibly multiple regexes going over the same text file. You can achieve this with partial by creating multiple functions that all take only one argument, so one implementation can be something like the following:
This is very interesting but I'm having trouble understanding the nature of reg_fun(). Is it applying the regex to the file and then applying the second regex or is it applying both regex separately? Also unfortunatley, I'm using different textfiles so ideally my solution would also take a path to a file too.
I tried running the reg_fun() without the reduce(compose) and it looks like it's returning two separate functions.
So, to answer your question, it will first apply first regex to string and then apply second str_split with a regex to the result of first transformation.
But let's step back a bit. What is the result you want to achieve? I tried running your example by hand and I'm not getting anything meaningful.
My use case is broader than just the split. But essentially I have several text files that I need to find the "hidden-message" in using regex and some other stuff. I'm trying to get better at my functional programming chops and while I can refactor read_and_split() into a function, I am trying to develop a better understanding of purrr's toolset and functional programming. Below is a more complete example of what I am doing:
I should add, this is an assignment that I've already completed, but there is a challenge to minimize your code to as few lines as possible so I'm trying to do that for the challenge.