简体   繁体   English

ramda.js中带有模板字符串的Pointfree-Style

[英]Pointfree-Style with template-string in ramda.js

I've problems writing a pointfree-style function in ramda.js and wondered if anybody can help me with that. 我在ramda.js中编写无点样式的函数时遇到问题,想知道是否有人可以帮助我。 The getEnv function reads an env-variable and logs to console if it couldn't be found. getEnv函数读取一个env变量,如果找不到则记录到控制台。

Here is my code 这是我的代码

const env = name => R.path(['env', name], process);

const getEnv = name => R.pipe(
  env,
  R.when(R.isNil, () => log(`Missing env "${name}"`))
)(name);

console.log(getEnv('myenv'))

I'd want to drop the name parameter of the getEnv function (and if possible also on the env function) but don't know how to do this. 我想删除getEnv函数的name参数(如果可能的话,也可以放在env函数上),但是不知道该怎么做。

The function getEnv does more than it should. 函数getEnv作用超出其应有的范围。 It actualy returns the content of the path or logs a validation message . 实际上,它返回路径的内容 记录验证消息

Split it into two separate functions. 将其分为两个单独的功能。 In my example below, I call it findPath and validatePath , which works generically for all paths. 在下面的示例中,我将其findPathvalidatePath ,它们通常适用于所有路径。 I've wrapped validatePath into another function called validateEnvPath , which searches directly for "env" 我将validatePath包装到另一个名为validateEnvPath函数中,该函数直接搜索“ env”

To get rid of env you can do the following: R.flip (R.curry (R.path)) . 为了摆脱env你可以做到以下几点: R.flip (R.curry (R.path)) This will turn the function curry and then the arguments around, so you can tell the function where you want to query first 这将使函数咖喱然后变为参数,因此您可以告诉函数您要首先查询的位置

 const process = {env: {myenv: ':)'}} const path = R.flip(R.curry(R.path)) const findPathInProcess = R.pipe( path (process), R.ifElse( R.isNil, R.always(undefined), R.identity ) ) const validatePath = path => validationPathResponse (findPathInProcess( path )) (`can't find something under [${path}]`) const validateEnvPath = path => validatePath (buildPath (['env']) (path)) const buildPath = xs => x => xs.concat(x) const validationPathResponse = response => errorMessage => response ? response : errorMessage console.log(validatePath(['env', 'myenv'])) console.log(validateEnvPath('myenv')) console.log(validateEnvPath('yourenv')) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script> 

Consider using Either monad. 考虑使用Either monad。 Sanctuary has it already implemented and plays nice with its brother ramda : Sanctuary已经实施了,并且与它的兄弟ramda玩得很好:

const viewEnv = S.pipe ([
    S.flip (R.append) (['env']),
    R.lensPath,
    R.view,
    S.T (process),
    S.toEither ('Given variable could not be retrieved')
])

const log = R.tap (console.log)

const eitherSomeVar = viewEnv ('someVar')
const eitherWhatever = S.bimap (log) (doSomeOtherStuff)

In addition one could also write the following 此外,还可以编写以下内容

const path = R.flip(R.path) // already curried

const findPathInProcess = R.pipe(
  path(process),
  R.when(
    R.isNil,
    R.always(undefined)
  )
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM