0

I have a pipe like this

pipe(
    getUserData,
    getLocationData,
    someFunctionThatNeedBothUserAndLocationData
)(input)

I need to have a function that access to both user and location data like this

function someFunctionThatNeedBothUserAndLocationData(user, location){
    // do something
}

How can I do something like that in functional programming?

Héctor
  • 24,444
  • 35
  • 132
  • 243
mohsen saremi
  • 685
  • 8
  • 22
  • Just don't use `pipe`. It's still functional programming when calling functions directly… – Bergi Nov 11 '19 at 07:55
  • 1
    "functional" doesn't necessarily mean "point-free", see https://stackoverflow.com/a/58203072/989121 – georg Nov 11 '19 at 08:25
  • This seems to be applicative lifting in the function context: `const liftA2 = f => g => h => x => f(g(x)) (h(x))`. –  Nov 11 '19 at 09:23
  • Please notice that piping is just upside down function composition, which in turn is a functorial computation, i.e. an applied functor of the function type. FP consists of much more than just the functor instance of pure functions. –  Nov 11 '19 at 12:13

2 Answers2

2

not really possible with pipe, since subsequent functions are unary (they take as argument the result of a previous function). A converge function helps here I think!

const log = (user, location) => {
  console.log(`${user.name} is in ${location}`);
};


const getUserData = () => ({ name: 'Giuseppe' });
const getUserLocation = () => 'Bologna';

R.converge(log, [
  getUserData,
  getUserLocation,
])();
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js" integrity="sha256-xB25ljGZ7K2VXnq087unEnoVhvTosWWtqXB4tAtZmHU=" crossorigin="anonymous"></script>
Hitmands
  • 13,491
  • 4
  • 34
  • 69
0

I hope I'm not too late.

const pipedFunction = pipe(
    getUserData,
    getLocationData,
    someFunctionThatNeedBothUserAndLocationData
)(input)

const getLocationData = (userData) => [user, location]

const someFunctionThatNeedBothUserAndLocationData = (arg) => {
  const [user, location] = arg;
}

or

const pipedFunction = pipe(
    getUserData,
    getLocationData,
    someFunctionThatNeedBothUserAndLocationData
)(input)

const getLocationData = (userData) => {user, location}

const someFunctionThatNeedBothUserAndLocationData = (arg) => {
  const {user, location} = arg;
}
Daniel Duong
  • 1,084
  • 4
  • 11