-1

I'm wondering if it's considered valid, from a functional programming perspective, to include declarations and ternaries while currying in Javascript, like so:

const one = (a) => {
  return (b) => {
    return (c) => {
       const new = c + b;
       return new > 10 ? new : a;
    }
  }
}

Is this valid?

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
  • 4
    `const new` definitely isn't valid - `new` is a reserved keyword – CertainPerformance Jun 30 '19 at 23:39
  • I believe that if you come up with an actual use case, rather than some contrived thing, you'll find that you will almost always have operations do to inside pure functions. Whether they are curried or not. – Randy Casburn Jun 30 '19 at 23:42
  • The conditional operator (which happens to be a ternary one) is totally valid in FP, because it is an expression. Usually you want to work with expressions rahter than statements in FP but sometimes you need intermediate values or it just makes the code more readable. Then just use a statements. There is nothing wrong about it. –  Jul 01 '19 at 07:20

1 Answers1

0

I believe so, as long as you do it in one line - you can also optimize your code slightly (and change new or you'll get an error):

const one = a => {
  return b => {
    return c => c + b > 10 ? c + b : a;
  }
};

You can also make the whole thing one line, using the implicit return of arrow functions:

const one = a => b => c => c + b > 10 ? c + b : a;
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79