Closed geotheory closed 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?
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.
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
In my view slice feels very javascripty. dplyr's implementation allows for multiple methods:
slice(iris, c(1, 51, 101))
// single argumentslice(iris, 1, 51, 101)
// multiple argumentsslice(iris, -1, -51, -101)
// negationAm I right slice in tidy.js is basically same as javascript?
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?