File tree 3 files changed +29
-0
lines changed
3 files changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -139,6 +139,17 @@ exports.split = function (sep) {
139
139
} ;
140
140
} ;
141
141
142
+ exports . _splitAt = function ( just ) {
143
+ return function ( nothing ) {
144
+ return function ( i ) {
145
+ return function ( s ) {
146
+ return i >= 0 && i < s . length ?
147
+ just ( [ s . substring ( 0 , i ) , s . substring ( i ) ] ) : nothing ;
148
+ } ;
149
+ } ;
150
+ } ;
151
+ } ;
152
+
142
153
exports . toCharArray = function ( s ) {
143
154
return s . split ( "" ) ;
144
155
} ;
Original file line number Diff line number Diff line change @@ -25,6 +25,7 @@ module Data.String
25
25
, stripSuffix
26
26
, count
27
27
, split
28
+ , splitAt
28
29
, toCharArray
29
30
, toLower
30
31
, toUpper
@@ -197,6 +198,16 @@ foreign import count :: (Char -> Boolean) -> String -> Int
197
198
-- | * `split " " "hello world" == ["hello", "world"]`
198
199
foreign import split :: String -> String -> Array String
199
200
201
+ -- | Returns the substrings of split at the given index, if the index is within bounds.
202
+ splitAt :: Int -> String -> Maybe (Array String )
203
+ splitAt = _splitAt Just Nothing
204
+
205
+ foreign import _splitAt :: (forall a . a -> Maybe a )
206
+ -> (forall a . Maybe a )
207
+ -> Int
208
+ -> String
209
+ -> Maybe (Array String )
210
+
200
211
-- | Converts the string into an array of characters.
201
212
foreign import toCharArray :: String -> Array Char
202
213
Original file line number Diff line number Diff line change @@ -155,6 +155,13 @@ testString = do
155
155
assert $ split " b" " aabcc" == [" aa" , " cc" ]
156
156
assert $ split " d" " abc" == [" abc" ]
157
157
158
+ log " splitAt"
159
+ assert $ splitAt 1 " " == Nothing
160
+ assert $ splitAt 0 " a" == Just [" " , " a" ]
161
+ assert $ splitAt 1 " ab" == Just [" a" , " b" ]
162
+ assert $ splitAt 3 " aabcc" == Just [" aab" , " cc" ]
163
+ assert $ splitAt (-1 ) " abc" == Nothing
164
+
158
165
log " toCharArray"
159
166
assert $ toCharArray " " == []
160
167
assert $ toCharArray " a" == [' a' ]
You can’t perform that action at this time.
0 commit comments