0

I've problems writing a pointfree-style function in ramda.js and wondered if anybody can help me with that. The getEnv function reads an env-variable and logs to console if it couldn't be found.

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.

Vispercept
  • 105
  • 1
  • 9
  • 1
    Can you elaborate on your problems? What were you expecting, and what actually happened? If you got an exception/error, post the line it occurred on and the exception/error details. Please [edit] these details in or we may not be able to help. – Blue Aug 08 '18 at 07:19
  • Thanks @FrankerZ . Updated my question recently. – Vispercept Aug 08 '18 at 07:31

3 Answers3

1

The function getEnv does more than it should. 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 andvalidatePath, which works generically for all paths. I've wrapped validatePath into another function calledvalidateEnvPath, which searches directly for "env"

To get rid of env you can do the following: 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>
Roman
  • 4,922
  • 3
  • 22
  • 31
1

Consider using Either monad. Sanctuary has it already implemented and plays nice with its brother 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)
Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
0

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)
  )
)
Vispercept
  • 105
  • 1
  • 9