Skip to content

Commit ad0bf43

Browse files
Make splitAt total
1 parent 43d8699 commit ad0bf43

File tree

3 files changed

+18
-31
lines changed

3 files changed

+18
-31
lines changed

src/Data/String.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,9 @@ exports.split = function (sep) {
145145
};
146146
};
147147

148-
exports._splitAt = function (just) {
149-
return function (nothing) {
150-
return function (i) {
151-
return function (s) {
152-
return i >= 0 && i < s.length ?
153-
just({ before: s.substring(0, i), after: s.substring(i) }) :
154-
nothing;
155-
};
156-
};
148+
exports.splitAt = function (i) {
149+
return function (s) {
150+
return { before: s.substring(0, i), after: s.substring(i) };
157151
};
158152
};
159153

src/Data/String.purs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -231,15 +231,10 @@ foreign import count :: (Char -> Boolean) -> String -> Int
231231
-- | * `split (Pattern " ") "hello world" == ["hello", "world"]`
232232
foreign import split :: Pattern -> String -> Array String
233233

234-
-- | Returns the substrings of split at the given index, if the index is within bounds.
235-
splitAt :: Int -> String -> Maybe { before :: String, after :: String }
236-
splitAt = _splitAt Just Nothing
237-
238-
foreign import _splitAt :: (forall a. a -> Maybe a)
239-
-> (forall a. Maybe a)
240-
-> Int
241-
-> String
242-
-> Maybe { before :: String, after :: String }
234+
-- | Returns a string split into two substrings at the given index, where
235+
-- | `before` includes all of the characters up to the given index, and `after`
236+
-- | is the rest of the string, from the given index on.
237+
foreign import splitAt :: Int -> String -> { before :: String, after :: String }
243238

244239
-- | Converts the string into an array of characters.
245240
foreign import toCharArray :: String -> Array Char

test/Test/Data/String.purs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Prelude (Unit, Ordering(..), (==), ($), discard, negate, not, (/=), (&&))
55
import Control.Monad.Eff (Eff)
66
import Control.Monad.Eff.Console (CONSOLE, log)
77

8-
import Data.Maybe (Maybe(..), isNothing, maybe)
8+
import Data.Maybe (Maybe(..), isNothing)
99
import Data.String
1010

1111
import Test.Assert (ASSERT, assert)
@@ -160,19 +160,17 @@ testString = do
160160
assert $ split (Pattern "d") "abc" == ["abc"]
161161

162162
log "splitAt"
163-
let testSplitAt i str res =
163+
let testSplitAt i str r =
164164
assert $ case splitAt i str of
165-
Nothing ->
166-
isNothing res
167-
Just { before, after } ->
168-
maybe false (\r ->
169-
r.before == before && r.after == after) res
170-
171-
testSplitAt 1 "" Nothing
172-
testSplitAt 0 "a" $ Just {before: "", after: "a"}
173-
testSplitAt 1 "ab" $ Just {before: "a", after: "b"}
174-
testSplitAt 3 "aabcc" $ Just {before: "aab", after: "cc"}
175-
testSplitAt (-1) "abc" $ Nothing
165+
{ before, after } ->
166+
r.before == before && r.after == after
167+
168+
testSplitAt 1 "" { before: "", after: "" }
169+
testSplitAt 0 "a" { before: "", after: "a" }
170+
testSplitAt 1 "a" { before: "a", after: "" }
171+
testSplitAt 1 "ab" { before: "a", after: "b" }
172+
testSplitAt 3 "aabcc" { before: "aab", after: "cc" }
173+
testSplitAt (-1) "abc" { before: "", after: "abc" }
176174

177175
log "toCharArray"
178176
assert $ toCharArray "" == []

0 commit comments

Comments
 (0)