Skip to content

Commit 29c7d5d

Browse files
committed
Add dropRight / takeRight
1 parent 81cfa1a commit 29c7d5d

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/Data/String.purs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ module Data.String
2121
, replace
2222
, replaceAll
2323
, take
24+
, takeRight
2425
, takeWhile
2526
, drop
27+
, dropRight
2628
, dropWhile
2729
, stripPrefix
2830
, stripSuffix
@@ -274,8 +276,8 @@ foreign import _lastIndexOf
274276
-> Maybe Int
275277

276278
-- | Returns the index of the last occurrence of the pattern in the
277-
-- | given string, starting at the specified index
278-
-- | and searching backwards towards the beginning of the string.
279+
-- | given string, starting at the specified index
280+
-- | and searching backwards towards the beginning of the string.
279281
-- | Returns `Nothing` if there is no match.
280282
-- |
281283
-- | ```purescript
@@ -347,6 +349,15 @@ foreign import replaceAll :: Pattern -> Replacement -> String -> String
347349
-- |
348350
foreign import take :: Int -> String -> String
349351

352+
-- | Returns the last `n` characters of the string.
353+
-- |
354+
-- | ```purescript
355+
-- | take 5 "Hello World" == "World"
356+
-- | ```
357+
-- |
358+
takeRight :: Int -> String -> String
359+
takeRight i s = drop (length s - i) s
360+
350361
-- | Returns the string without the first `n` characters.
351362
-- |
352363
-- | ```purescript
@@ -355,6 +366,15 @@ foreign import take :: Int -> String -> String
355366
-- |
356367
foreign import drop :: Int -> String -> String
357368

369+
-- | Returns the string without the last `n` characters.
370+
-- |
371+
-- | ```purescript
372+
-- | dropRight 6 "Hello World" == "Hello"
373+
-- | ```
374+
-- |
375+
dropRight :: Int -> String -> String
376+
dropRight i s = take (length s - i) s
377+
358378
-- | Returns the number of contiguous characters at the beginning
359379
-- | of the string for which the predicate holds.
360380
-- |

test/Test/Data/String.purs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,27 @@ testString = do
139139
assert $ take 3 "ab" == "ab"
140140
assert $ take (-1) "ab" == ""
141141

142+
log "takeRight"
143+
assert $ takeRight 0 "ab" == ""
144+
assert $ takeRight 1 "ab" == "b"
145+
assert $ takeRight 2 "ab" == "ab"
146+
assert $ takeRight 3 "ab" == "ab"
147+
assert $ takeRight (-1) "ab" == ""
148+
142149
log "drop"
143150
assert $ drop 0 "ab" == "ab"
144151
assert $ drop 1 "ab" == "b"
145152
assert $ drop 2 "ab" == ""
146153
assert $ drop 3 "ab" == ""
147154
assert $ drop (-1) "ab" == "ab"
148155

156+
log "dropRight"
157+
assert $ dropRight 0 "ab" == "ab"
158+
assert $ dropRight 1 "ab" == "a"
159+
assert $ dropRight 2 "ab" == ""
160+
assert $ dropRight 3 "ab" == ""
161+
assert $ dropRight (-1) "ab" == "ab"
162+
149163
log "count"
150164
assert $ count (_ == 'a') "" == 0
151165
assert $ count (_ == 'a') "ab" == 1

0 commit comments

Comments
 (0)