You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Backlinking to #212 for more context. The question here is whether to add sliding, a function that returns windows of the array, and what its type signature should be. A variation of sliding is slidingSizeStep which allows one to control the size of the window and how far to step before creating a new window. We'll cover this function later in this issue.
Starting with sliding, the core question is what the return type should be. Below, I'll refer to various ideas for implementations using _N where N refers to the possible way forward.
-- as proposed in #212_1::foralla. Arraya->Array (Tupleaa)
_2::foralla. Arraya->Array{previous::Maybea, current::a, next::Maybea}_3::forallab. Arraya-> (Tupleaa->b) ->Arrayb
_1 is what is proposed in #212. _2 is what Harry proposed in his comment, functioning more like a Zipper. _3 is something I am proposing for unifying the other two. For example, if we implemented _1 and _3 in this library, _2 could still easily be implemented via (Tuple l r) -> {previous: Nothing, current: l, next: Just r}.
Since _2's zipper-like type might want additional features (e.g. a Functor instance) that is largely outside the scope of this project, I propose not implementing it as end-users can implement it and define their own Window type. Moreover, _3 enables more potential use cases we cannot imagine right now (e.g. slidingF ((Tuple l r) -> l + r) [0, 1, 2, 3] would produce [1, 3, 5]), simulating a scanl like feature.
Let's move on to slidingSizeStep. If we ignore the size and step arguments in the function, the question arises again as to what the return type should be. Here's two factors that come to mind:
whether the returned value is Array _ or Maybe (NonEmptyArray _)
whether the returned value is the complete subcollection or a zipper-like value
-- as proposed in #212_11::foralla. Arraya->Array (NonEmptyArraya)
_12::foralla. Arraya->Maybe (NonEmptyArray (NonEmptyArraya))
_13::foralla. Arraya->Array{before::Lista, current::a, after::Lista}_14::forallab. Arraya-> (NonEmptyArraya->b) ->Arrayb
I believe _11 is to be preferred over _12 because one can still get Foldable1 and friends without having to pay for the Just constructor unwrapping in _12 by doing something like this:
caseNEA.fromArray $ slidingSizeStep [0, 1, 2, 3] ofNothing->-- returned array is emptyJust nea -> fold1 nea -- returned array is non-empty
_13 provides the same zipper-like idea. However, it cannot be implemented because arrays does not depend on lists. Working similar to sliding, _4 provides an escape hatch to this problem. One could use _4 in combination with my own arrays-zipper to get an Array-based zipper or use pointed-list to get a List-based zipper.
In conclusion, I propose we implement these four functions:
While the comment I made above discusses the design of the API, it does not ask whether the functionality should be added here. Does such sliding functionality appear in other language's standard libraries? If it does, I think that's a strong argument for supporting it here.
Thanks for the writeup, @JordanMartinez. I come from scala and it has a very powerful collection library that's part of the standard-library. I missed this function for my side-project so I decided to write it in purescript ;-) see here.
It returns an Iterator[List[A]] which is a lazy datastructure. Oh, and they don't have the version with the tuple.
Backlinking to #212 for more context. The question here is whether to add
sliding
, a function that returns windows of the array, and what its type signature should be. A variation ofsliding
isslidingSizeStep
which allows one to control the size of the window and how far to step before creating a new window. We'll cover this function later in this issue.Starting with
sliding
, the core question is what the return type should be. Below, I'll refer to various ideas for implementations using_N
whereN
refers to the possible way forward._1 is what is proposed in #212. _2 is what Harry proposed in his comment, functioning more like a Zipper. _3 is something I am proposing for unifying the other two. For example, if we implemented _1 and _3 in this library, _2 could still easily be implemented via
(Tuple l r) -> {previous: Nothing, current: l, next: Just r}
.Since _2's zipper-like type might want additional features (e.g. a
Functor
instance) that is largely outside the scope of this project, I propose not implementing it as end-users can implement it and define their ownWindow
type. Moreover, _3 enables more potential use cases we cannot imagine right now (e.g.slidingF ((Tuple l r) -> l + r) [0, 1, 2, 3]
would produce[1, 3, 5]
), simulating ascanl
like feature.Let's move on to
slidingSizeStep
. If we ignore the size and step arguments in the function, the question arises again as to what the return type should be. Here's two factors that come to mind:Array _
orMaybe (NonEmptyArray _)
I believe _11 is to be preferred over _12 because one can still get
Foldable1
and friends without having to pay for theJust
constructor unwrapping in _12 by doing something like this:_13 provides the same zipper-like idea. However, it cannot be implemented because
arrays
does not depend onlists
. Working similar tosliding
, _4 provides an escape hatch to this problem. One could use _4 in combination with my own arrays-zipper to get an Array-based zipper or use pointed-list to get a List-based zipper.In conclusion, I propose we implement these four functions:
I'm not yet considering
rangeWithStep
that was also proposed in #212 because I want to first cover these two functions and their variations.The text was updated successfully, but these errors were encountered: