Comments (10)
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.
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 Nothing
s or Just(undefined)
s.
from sanctuary.
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.
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.
Thanks for investigating, @svozza. Let's add S.slice
before deciding whether any string-specific functions are necessary.
from sanctuary.
Sounds good, I'll pick up this PR of you want.
from sanctuary.
Go for it! :)
from sanctuary.
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.
Yep, sounds good. I can work on this later on today.
from sanctuary.
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)
- Possible example bug HOT 3
- How to create a function in 'sanctuary'? HOT 2
- State and Reader ADTs
- ES5 => ES6 HOT 1
- Bundled version contains refetences to undefined self HOT 4
- Add "picks" to include certain key/value pairs from an object HOT 3
- Deno version HOT 2
- Add a function to split an Array into equally sized chunks
- Idea: Only treat objects without prototypes as StrMaps HOT 6
- Be more explicit about S.get(s) being an escape hatch from type safety HOT 1
- Question about S.concat HOT 4
- Support logging S.Nothing in deno
- feat: Make types accessible by name in `S` HOT 1
- Remove the need for require ('util') in browser environment HOT 4
- Check for empty structures? HOT 1
- Searchbar on website https://sanctuary.js.org/ HOT 1
- Implement Types for all new EcmaScript objects.
- match has changed from version 3.1.0 HOT 2
- Refactor documentation to be more independent and catchy
- ECMAScript modules HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sanctuary.