pbeshai / tidy

Tidy up your data with JavaScript, inspired by dplyr and the tidyverse
https://pbeshai.github.io/tidy
MIT License
727 stars 22 forks source link

Thoughts on slice() #9

Closed geotheory closed 3 years ago

geotheory commented 3 years ago

In my view slice feels very javascripty. dplyr's implementation allows for multiple methods:

Am I right slice in tidy.js is basically same as javascript?

x = [1,2,3,4,5,6,7,8,9,10];
tidy(x, Tidy.slice(2,5))
(3) [3, 4, 5]
tidy(x, Tidy.slice([2,5]))   // ?
(10) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
tidy(x, Tidy.slice(-5))
(5) [6, 7, 8, 9, 10]

I feel javascript is crying out for an elegant implementation like dplyr's. Wonder if there might be appetite for a slice2 function to plug the gap?

pbeshai commented 3 years ago

You're right, it is just calling slice currently!

export function slice<T extends object>(
  start: number,
  end?: number
): TidyFn<T> {
  const _slice: TidyFn<T> = (items: T[]): T[] => items.slice(start, end);

  return _slice;
}

I actually have never used slice/needed to use it, so I'm sure it can be improved. Can you explain a bit what the additional options do? e.g. slice(1, 51, 101) -- does this just take items 1, 51, and 101? e.g. filter((d, i) => [1,51,101].includes(i)) ? And for negative indices, it removes them?

My main concern is since js has a slice, if the verb slice is substantially different from the js slice that could be confusing. It seems like it's just shorthand for filter, is that right?

geotheory commented 3 years ago

Yes dplyr's slice(x, 1, 3, 5) and slice(x, c(1,3,5)) are both equivalent to javascript's x.filter((v, i) => [2,4,6].includes(i)). For negation dplyr's slice(x,-1,-3,-5)) is javascript x.filter((v, i) => ![1,4,6].includes(i)). Perhaps this is elegant enough already.

I suppose this comes down to a question of whether you want tidy.js to really be as much like dplyr as possible or rather to provide a tidy-like toolkit for tasks that are more fiddly to achieve in javascript. Perhaps the latter is preferable.

pbeshai commented 3 years ago

Ah gotcha, yes I think I prefer the latter as some things in js are a lot easier to accomplish and we can embrace them. To me (a js person) learning a new slice syntax vs a simple no-magic filter() call, it's preferable to do the filter(). But I am happy to be swayed by the community if there ends up being strong support for a fancier slice(), we can make some improvements.

R has cool support for ranges/indices that probably make using slice more natural to do than it is in js, where filter may be more readable and more common. Thanks for bringing it up, I think we'll stick with what we have for now, but am happy to revisit again in the future