0

I'm stuck on currying a function with many parameters. What I want to do is take the result of return function and pass it to the next functions.

What I want is similar to this

return this.function(
   this.function(
      this.function(this.data, this.filterCondition1),
      this.filterCondition2),
   this.filterCondition3
);

What it basically does is it takes some data, filters it by condition and then feeds it to the next filter that also takes in a second different condition

What I've tried is:

curryFunction(data, filter, filterFunction) =>{
   const filteredData = filterFunction(data)
   return helper( curryFilter, curryFunction) =>{
     return curryFunction(filteredData, curryFilter, curryFunction)
   }
}

However, this function returns a function at the end instead of a value. Basically, it's an infinite loop. How exactly would I go about solving this problem? Thanks

AzureWorld
  • 311
  • 2
  • 10
  • Please have a look at https://stackoverflow.com/questions/36314/what-is-currying?rq=1, think again about it – MoxxiManagarm Aug 29 '19 at 11:17
  • you want to provide arguments infinitely ? – Aviso Aug 29 '19 at 12:30
  • Thanks Moxxi, I was doing this function really late last night so I didn't read about the most important part about currying which is to reduce it down to one function. – AzureWorld Aug 29 '19 at 22:10

1 Answers1

0

Based on your comment of

take the result of the previous function and pass it to the next, similar to add([1]).add(2).add(3), which will result in 6

I think it is might be better to implement via class function.

class NumberResult {
  value = 0;
  
  constructor (value: number) {
    this.value = value;
  }

  function add(a: number) {
    this.value = this.value + a;
    return this;
  }
  
  function add(a: number[]) {
    // overload to handle array
  }
}

const result = new NumberResult(1);
result.add(1).add(2).add(3);
console.log(result.value);
Icycool
  • 7,099
  • 1
  • 25
  • 33
  • The reason why i was using a curry is beause I wanted to do it in a functional way, without having to reassign var. – AzureWorld Aug 29 '19 at 22:40
  • @AzureWorld don't really understand, can you provide a more concrete example of your use case? – Icycool Aug 30 '19 at 02:25
  • What I want to do is basically chain a function: take the result of the previous function and pass it to the next, similar to add([1]).add(2).add(3), which will result in 6. I was able to come up with a solution while reading more about currying: `function curry(value, filter, func){ let totalValue = func(value,filter); return function helper(filter){ if(filter === undefined) return totalValue; else{ totalValue += func(value, filter); return helper; } . }` – AzureWorld Aug 31 '19 at 07:36
  • Updated answer based on comment. The idea is returning the object for every function so next function can use it. But `rxjs` is moving from this semantics to the `.pipe(multiple functions)`, so this approach might have some limitations? – Icycool Sep 02 '19 at 01:16