reselect icon indicating copy to clipboard operation
reselect copied to clipboard

add curried version of createSelector?

Open mrtnbroder opened this issue 10 years ago • 9 comments

I've just ran into this problem. I wanted to cleanup some selectors and refactor them to be point-free.

Now, as the very last function of createSelector receives the return values of all the functions before that, I have to write one function that takes all x arguments at once and handle them. this makes it kinda hard to write in point-free style.

Given this selector for example:

const favoritePetsOfUser = (user, pets) => pets.filter((pet) => pet.id === user.favorites.favoritePetId)
const petsOfUserSelector = createSelector(
  userSelector,
  petsSelector,
  favoritePetsOfUser
)

to get rid of (user, pets) I could write (using ramda)

const pathToFav = R.pathEq(['pets', 'favoritePetId'])

// and use it like this:
const favoritePetsOfUser = R.compose(R.filter, pathToFav)

and use that as the last function within my selector.

BUT, and here's the thing: Only when the function would apply each argument one by one like this: favoritePetsOfUser(user)(pets)

so first call to favoritePetsOfUser would setup the path to the user and the second one would then call the actual filter method.

But actually it will be called like this favoritePetsOfUser(user, pets).

To make it work now I'd have to uncurry my functions first, which would be a lot of boilerplate if this happens a lot.

This sucks, so I'd love to have a curried version of createSelector, like createSelectorN or something like that, that applies each argument to the last function one by one.

I hope this makes sense to you. It's 2 am here almost and I'm pretty tired 😎 👍

Thanks!

mrtnbroder avatar Aug 09 '16 23:08 mrtnbroder

Hey Martin,

Not sure if this is what you meant by a lot of boilerplate, but would this do it?

const uncurry = curriedFn => (...args) => (
    args.reduce((left, right) => left(right), curriedFn)
);

const createSelectorN = (...selectors, curriedFn) => (
    createSelector(...selectors, uncurry(curriedFn))
);

MattSPalmer avatar Aug 09 '16 23:08 MattSPalmer

Hey @MattSPalmer,

Thanks! That is indeed exactly what I wanted... Ugh. Maybe I should have slept about it first before creating an issue.

I will write a helper file and import my createSelectorN from there.

Feel free to close this :)

mrtnbroder avatar Aug 10 '16 07:08 mrtnbroder

Actually, what if we could change the API of createSelector a bit to accept the selector functions first as an array of functions and let the second argument be the actual transforming function, where the second argument could also be applied at a later point in time, as the whole thing would be curried.

// create a new selector immediatly
const userAndPetsSelector = createSelector([selectUser, selectPets], transformingFunction)

// apply transforming function later
const userAndPets = createSelector([selectUser, selectPets])
const userAndPetsSelector = userAndPets(transformingFunction)

We would be more explicit with the functions we fed into our actual transforming function and could do a lot more stuff like, using concat etc. to push more selecting functions into the first argument of createSelector

mrtnbroder avatar Aug 10 '16 09:08 mrtnbroder

It's an interesting proposal, but at this point in time the API for createSelector is stable and won't change. We could maybe add createCurriedSelector as a new function though.

@MattSPalmer Nice solution, in case anyone else is wanting the same thing I'm going to link to it from the FAQ. Cheers!

ellbee avatar Aug 11 '16 20:08 ellbee

@ellbee Is it still something needed to release 3.0.0 ? Should we keep this open ?

alex3165 avatar Mar 10 '17 14:03 alex3165

Not needed for 3.0.0, but lets keep it open.

ellbee avatar Mar 10 '17 19:03 ellbee

Can anyone provide an example how-to use createSelectorN? And will be a createCurriedSelector included to release?

Thanks.

koutsenko avatar Mar 25 '17 22:03 koutsenko

A minor aside, and also incompatible: it might be useful to use partial application with createSelectorN itself, or, since the arguments are of an arbitrary arity, go full tilt and break it up instead, e.g. changing from this:

const createSelectorN = (...selectors, curriedFn) => (
    createSelector(...selectors, uncurry(curriedFn))
)

to

const createSelectorN = curriedFn => (...selectors) => (
    createSelector(...selectors, uncurry(curriedFn))
)

so a user may call

createSelectorN(func)(selector1, ..., selectorN)

which of course is just a lift operator on the space of selectors (it lifts the ordinary function func to work with selector inputs and yield a selector as a result).

This can be useful when the same lifted func can be plugged in eg.

const liftedFunc = createSelectorN(func)

const selA = liftedFunc(sel1, sel2)
const selB = liftedFunc(sel3, sel4)
const selC = liftedFunc(sel5, sel6)

so it's just a minor DRY application but chimes well with currying the ordinary function

monfera avatar May 27 '17 12:05 monfera

Closing as this seems to be both stale and adequately answered.

markerikson avatar Feb 07 '21 18:02 markerikson