gilbert / es-papp

A proposal for adding partial application support to JavaScript.
359 stars 12 forks source link

Split papp to pal and par (upd.: paL and paR) #2

Open Alexsey opened 8 years ago

Alexsey commented 8 years ago

Proposal is to have two functions for partial application: pal and par

pal will partially apply arguments to the left (will work like papp) par will partially apply arguments to the right

Why is it important? Because it makes possible even for functions that does not implement data last pattern (most of current JS APIs) to be used like they does. Example:

function styleText (text, style) { // this function have typical arguments order
   return style(text)
}

let lines = ['first line', 'second line', 'third line']
let bold = text => `<b>${text}</b>'

let styledLines = lines.map(styleText.par(bold))

Of course if styleText implements data last pattern and would have an argument order with data as the last argument - papp would be enough. But probably it would have data first. So with pal and par partial application would cover such cases whatever API you are using (or supporting. You can't just start to implement data last in existing API with data first tradition)

And also it's easier to pronounce par or pal than papp :)

References: A few sentences about data last pattern Most dependent on library for JS Lodash implements it for all their methods in fp build

gilbert commented 8 years ago

Thank for your input! I like the idea of a right-side partial application. My first thought is it might get tricky defining how it works with rest parameters. For example:

function example (first, ...rest) {
  return { first, rest }
}

var g = example.rPapp(10, 20)
g('x','y','z') //=> ??

Would this return { first: 'x', rest: [10, 20, 'y', 'z'] } ? I would think so myself. It's something worth noting.

(I write rPapp because par to me reads par-tial, which isn't immediately clear it's from the right. But I bikeshed...)

Alexsey commented 8 years ago

At this point we could just mimic existing implementations like Lodash's or Ramda's or any other. This two works well with rest and both output { first: 'x', rest: ['y', 'z', 10, 20] }. I'm also think that it's more consistent with how it would works without rest

Alexsey commented 8 years ago

I have think of it again and you are right - par is really reads like partial. On the other hand I do think that short name is critical for such method because of potential of usage of partial functions as arguments of another functions. What do you think about paR and paL? Also keep in mind that people will start usage of this feature from paL and 90% of time their time it would also be paL