Giter Site home page Giter Site logo

Comments (10)

davidchambers avatar davidchambers commented on July 17, 2024

Before considering string slice functions, let's decide whether a sanctified version of R.slice is a good idea. We could implement S.tail and S.init in terms of S.slice:

S.tail = slice(1, Infinity);
S.init = slice(0, -1);
> S.slice(2, 4, [])
Nothing()
> S.slice(2, 4, ['a', 'b', 'c', 'd', 'e'])
Just(['c', 'd'])

I'm not sure how S.slice(2, 102, ['a', 'b', 'c', 'd', 'e']) would evaluate.

from sanctuary.

svozza avatar svozza commented on July 17, 2024

I guess there's not much difference between slice when toIndex > arr.length and tail so I think it's justified in only returning the remaining elements of the array. I don't think it would be that useful to return what amounts to a sparse array with 96 Nothings or Just(undefined)s.

from sanctuary.

davidchambers avatar davidchambers commented on July 17, 2024

I'm wondering how the following translates to the hypothetical S.slice:

> S.tail([1, 2, 3])
Just([2, 3])
> S.tail([1, 2])
Just([2])
> S.tail([1])
Just([])
> S.tail([])
Nothing()

It seems S.slice should only return a Just if the array contains the entire range of indexes (otherwise it isn't well differentiated from R.slice):

> S.slice(1, 1, ['a', 'b', 'c'])
Just([])
> S.slice(1, 2, ['a', 'b', 'c'])
Just(['b'])
> S.slice(1, 3, ['a', 'b', 'c'])
Just(['b', 'c'])
> S.slice(1, 4, ['a', 'b', 'c'])
Nothing()

This would guarantee that if startIndex >= 0 and endIndex >= 0 and startIndex <= endIndex, the resulting list will contain endIndex - startIndex elements.

from sanctuary.

svozza avatar svozza commented on July 17, 2024

Ah okay, I see where you're coming from. Seems like that's the implementation favoured here:

https://wiki.haskell.org/99_questions/Solutions/18

And here:

https://hackage.haskell.org/package/repa-array-4.1.0.1/docs/src/Data-Repa-Array-Auto-Operator.html

slice   :: Elem a => Int -> Int -> Array a -> Maybe (Array a)
slice from len arr
        | from >= 0, len >= 0
        , len  <= G.length arr - from
        = Just $ A.window from len arr

        | otherwise
        = Nothing

from sanctuary.

davidchambers avatar davidchambers commented on July 17, 2024

Thanks for investigating, @svozza. Let's add S.slice before deciding whether any string-specific functions are necessary.

from sanctuary.

svozza avatar svozza commented on July 17, 2024

Sounds good, I'll pick up this PR of you want.

from sanctuary.

davidchambers avatar davidchambers commented on July 17, 2024

Go for it! :)

from sanctuary.

davidchambers avatar davidchambers commented on July 17, 2024

We could define S.take and S.drop:

> S.take(2, ['a', 'b', 'c'])
Just(['a', 'b'])
> S.take(3, ['a', 'b', 'c'])
Just(['a', 'b', 'c'])
> S.take(4, ['a', 'b', 'c'])
Nothing()
> S.drop(2, ['a', 'b', 'c'])
Just(['c'])
> S.drop(3, ['a', 'b', 'c'])
Just([])
> S.drop(4, ['a', 'b', 'c'])
Nothing()

ramda/ramda#1159 deprecates R.substringTo and R.substringFrom. We could define S.take and S.drop in terms of S.slice so they would also work with strings.

from sanctuary.

svozza avatar svozza commented on July 17, 2024

Yep, sounds good. I can work on this later on today.

from sanctuary.

davidchambers avatar davidchambers commented on July 17, 2024

We now have S.slice, S.take, and S.drop, all of which can operate on strings (in addition to S.indexOf and S.lastIndexOf which are similarly polymorphic).

from sanctuary.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.