Skip to content

Commit 45afaf6

Browse files
committed
Use a Tuple in splitAt
Fixes #69
1 parent 94d6526 commit 45afaf6

File tree

4 files changed

+17
-11
lines changed

4 files changed

+17
-11
lines changed

bower.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
],
1919
"dependencies": {
2020
"purescript-either": "^2.0.0",
21-
"purescript-maybe": "^2.0.0"
21+
"purescript-maybe": "^2.0.0",
22+
"purescript-tuples": "^3.0.0"
2223
},
2324
"devDependencies": {
2425
"purescript-assert": "^2.0.0",

src/Data/String.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,12 @@ exports.split = function (sep) {
146146

147147
exports._splitAt = function (just) {
148148
return function (nothing) {
149-
return function (i) {
150-
return function (s) {
151-
return i >= 0 && i < s.length ?
152-
just([s.substring(0, i), s.substring(i)]) : nothing;
149+
return function (tuple) {
150+
return function (i) {
151+
return function (s) {
152+
return i >= 0 && i < s.length ?
153+
just(tuple(s.substring(0, i))(s.substring(i))) : nothing;
154+
};
153155
};
154156
};
155157
};

src/Data/String.purs

+5-3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import Prelude
4141
import Data.Maybe (Maybe(..), isJust)
4242
import Data.Newtype (class Newtype)
4343
import Data.String.Unsafe as U
44+
import Data.Tuple (Tuple(..))
4445

4546
-- | A newtype used in cases where there is a string to be matched.
4647
newtype Pattern = Pattern String
@@ -232,14 +233,15 @@ foreign import count :: (Char -> Boolean) -> String -> Int
232233
foreign import split :: Pattern -> String -> Array String
233234

234235
-- | Returns the substrings of split at the given index, if the index is within bounds.
235-
splitAt :: Int -> String -> Maybe (Array String)
236-
splitAt = _splitAt Just Nothing
236+
splitAt :: Int -> String -> Maybe (Tuple String String)
237+
splitAt = _splitAt Just Nothing Tuple
237238

238239
foreign import _splitAt :: (forall a. a -> Maybe a)
239240
-> (forall a. Maybe a)
241+
-> (forall a b. a -> b -> Tuple a b)
240242
-> Int
241243
-> String
242-
-> Maybe (Array String)
244+
-> Maybe (Tuple String String)
243245

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

test/Test/Data/String.purs

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Control.Monad.Eff (Eff)
66
import Control.Monad.Eff.Console (CONSOLE, log)
77

88
import Data.Maybe (Maybe(..), isNothing)
9+
import Data.Tuple (Tuple(..))
910
import Data.String
1011

1112
import Test.Assert (ASSERT, assert)
@@ -161,9 +162,9 @@ testString = do
161162

162163
log "splitAt"
163164
assert $ splitAt 1 "" == Nothing
164-
assert $ splitAt 0 "a" == Just ["", "a"]
165-
assert $ splitAt 1 "ab" == Just ["a", "b"]
166-
assert $ splitAt 3 "aabcc" == Just ["aab", "cc"]
165+
assert $ splitAt 0 "a" == Just (Tuple "" "a")
166+
assert $ splitAt 1 "ab" == Just (Tuple "a" "b")
167+
assert $ splitAt 3 "aabcc" == Just (Tuple "aab" "cc")
167168
assert $ splitAt (-1) "abc" == Nothing
168169

169170
log "toCharArray"

0 commit comments

Comments
 (0)