[Functional Programming Monad] Apply Stateful Computations T
When building our stateful computations,there will come a time when we’ll need to combine two or more state transactions at the same time to come up with a new result. Usually this occurs when want to use plain ol’ JavaScript functions with two or more a arguments as part of our stateful computations. We first look at how this can be accomplished by using? ? For example,we have a function: const namefiy = firstName => lastName => `${lastName},${firstName}`;
It should receive two params to return a string. ? one way is using .ap(),it takes the same input,run with the given functions and return its value,then combine those: var _getFullName = State.of(namefiy) .ap(getFirstName) .ap(getLastName) ? Or we can use .liftA2,it lift the function into State automaticlly: var getFullName = liftA2( namefiy,getFirstName,getLastName ) ? ---- ? const { liftA2,composeK,Unit,curry,objOf,compose,State,mapProps,prop,option } = require("crocks"); const { put,get,modify } = State; const namefiy = firstName => lastName => `${lastName},${firstName}`; const getWord = number => name => name.split(‘ ‘)[number]; const getFirstName = get(getWord(0)); const getLastName = get(getWord(1)); var _getFullName = State.of(namefiy) .ap(getFirstName) .ap(getLastName) var getFullName = liftA2( namefiy,getLastName ) console.log( getFullName .evalWith("John Green") ) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |