I'm a functional programming beginner. I'm working on a React Native app using Ramda. The app lets users maintain their houses.
I have written function called asyncPipe
which lets me pipe promises and normal functions. I use it for the loginFlow
which currently has a http request (getHouseList
) as its last function.
const asyncPipe = (...fns) => x => fns.reduce(async (y, f) => f(await y), x);
const loginFlow = asyncPipe(
// ... someFunctions
getHouseList
);
// used later like this in LoginForm.js's handleSubmit():
const list = await loginFlow(credentials);
So, after logging in, the app loads the user's houses. Now depending on whether he has only one or multiple houses I would like to send the user either to list view to choose a house or a detail view if he only has one house. Additionally, I would like to dispatch a Redux action to save the list in my reducer and another action to pick the house if there is only one.
Currently I do it like this:
const list = await loginFlow(credentials);
dispatch(addHouses(list));
if (list.length > 1) {
navigate('ListScreen')
} else {
dispatch(pickHouse(list[0]);
navigate('DetailScreen') ;
}
But as you can see that is super imperative. It seems like I have to 'fork' the list and use it twice in the pipe (because Redux' dispatch
does not have a return value).
My main question is:
How to do this more functional / declaratively (if there is a way)?
A little sub question I have would be, whether its okay to be imperative here / if doing it functional is a good idea.
You could probably extend your async pipeline, using something like
tap
:Whether this is worth doing will depend upon your application. If the pipeline is already a longish one, then it would probably be cleaner to add things to the end this way, even if they're not particularly functional sections. But for a short pipeline, this might not make much sense.
You also might want to look at the now-deprecated, pipeP or its replacement,
pipeWith
(then
).But you asked in the title about forking a parameter. Ramda's
converge
does exactly that:This allows you to pass more than two functions as well, and to pass more than one parameter to the resulting function:
Given that we can use
R.then
andR.otherwise
, then anasyncPipe
is not really needed. One of the principle of functional programming is actually delegating orchestration...Finally, of course you can be more declarative, and a good way to start is trying to avoid imperative control flows.
R.ifElse
will definitely help you here :)If your code has side effects, then use
R.tap
in your pipes :)