如何在函数式编程中访问两个参数

我有一个这样的管子


pipe(

    getUserData,

    getLocationData,

    someFunctionThatNeedBothUserAndLocationData

)(input)

我需要一个功能来访问这样的用户和位置数据


function someFunctionThatNeedBothUserAndLocationData(user, location){

    // do something

}

我怎样才能在函数式编程中做这样的事情?


30秒到达战场
浏览 126回答 2
2回答

慕尼黑的夜晚无繁华

用 不太可能pipe,因为后续函数是一元的(它们将前一个函数的结果作为参数)。一个汇聚功能有助于在这里我想!const log = (user, location) => {&nbsp; console.log(`${user.name} is in ${location}`);};const getUserData = () => ({ name: 'Giuseppe' });const getUserLocation = () => 'Bologna';R.converge(log, [&nbsp; getUserData,&nbsp; getUserLocation,])();<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js" integrity="sha256-xB25ljGZ7K2VXnq087unEnoVhvTosWWtqXB4tAtZmHU=" crossorigin="anonymous"></script>

繁华开满天机

我希望我不会太晚。const pipedFunction = pipe(&nbsp; &nbsp; getUserData,&nbsp; &nbsp; getLocationData,&nbsp; &nbsp; someFunctionThatNeedBothUserAndLocationData)(input)const getLocationData = (userData) => [user, location]const someFunctionThatNeedBothUserAndLocationData = (arg) => {&nbsp; const [user, location] = arg;}或者const pipedFunction = pipe(&nbsp; &nbsp; getUserData,&nbsp; &nbsp; getLocationData,&nbsp; &nbsp; someFunctionThatNeedBothUserAndLocationData)(input)const getLocationData = (userData) => {user, location}const someFunctionThatNeedBothUserAndLocationData = (arg) => {&nbsp; const {user, location} = arg;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript