Skip to content

Commit 36c0431

Browse files
authored
Merge pull request #83 from safareli/index
add Indexl and indexr
2 parents 642ae7d + 96e8f95 commit 36c0431

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
},
88
"devDependencies": {
99
"eslint": "^3.17.1",
10-
"pulp": "^10.0.4",
11-
"purescript-psa": "^0.5.0-rc.1",
10+
"pulp": "^11.0.0",
11+
"purescript-psa": "^0.5.1",
1212
"rimraf": "^2.6.1"
1313
}
1414
}

src/Data/Foldable.purs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ module Data.Foldable
1919
, product
2020
, elem
2121
, notElem
22+
, indexl
23+
, indexr
2224
, find
2325
, findMap
2426
, maximum
@@ -325,6 +327,30 @@ elem = any <<< (==)
325327
notElem :: forall a f. Foldable f => Eq a => a -> f a -> Boolean
326328
notElem x = not <<< elem x
327329

330+
-- | Try to get nth element from the left in a data structure
331+
indexl :: forall a f. Foldable f => Int -> f a -> Maybe a
332+
indexl idx = _.elem <<< foldl go { elem: Nothing, pos: 0 }
333+
where
334+
go cursor elem =
335+
case cursor.elem of
336+
Just _ -> cursor
337+
_ ->
338+
if cursor.pos == idx
339+
then { elem: Just elem, pos: cursor.pos }
340+
else { pos: cursor.pos + 1, elem: cursor.elem }
341+
342+
-- | Try to get nth element from the right in a data structure
343+
indexr :: forall a f. Foldable f => Int -> f a -> Maybe a
344+
indexr idx = _.elem <<< foldr go { elem: Nothing, pos: 0 }
345+
where
346+
go elem cursor =
347+
case cursor.elem of
348+
Just _ -> cursor
349+
_ ->
350+
if cursor.pos == idx
351+
then { elem: Just elem, pos: cursor.pos }
352+
else { pos: cursor.pos + 1, elem: cursor.elem }
353+
328354
-- | Try to find an element in a data structure which satisfies a predicate.
329355
find :: forall a f. Foldable f => (a -> Boolean) -> f a -> Maybe a
330356
find p = foldl go Nothing

test/Main.purs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Control.Monad.Eff.Console (CONSOLE, log)
77
import Data.Bifoldable (class Bifoldable, bifoldl, bifoldr, bifoldMap, bifoldrDefault, bifoldlDefault, bifoldMapDefaultR, bifoldMapDefaultL)
88
import Data.Bifunctor (class Bifunctor, bimap)
99
import Data.Bitraversable (class Bitraversable, bisequenceDefault, bitraverse, bisequence, bitraverseDefault)
10-
import Data.Foldable (class Foldable, find, findMap, fold, foldMap, foldMapDefaultL, foldMapDefaultR, foldl, foldlDefault, foldr, foldrDefault, length, maximum, maximumBy, minimum, minimumBy, null, surroundMap)
10+
import Data.Foldable (class Foldable, find, findMap, fold, indexl, indexr, foldMap, foldMapDefaultL, foldMapDefaultR, foldl, foldlDefault, foldr, foldrDefault, length, maximum, maximumBy, minimum, minimumBy, null, surroundMap)
1111
import Data.FoldableWithIndex (class FoldableWithIndex, findWithIndex, foldMapWithIndex, foldMapWithIndexDefaultL, foldMapWithIndexDefaultR, foldlWithIndex, foldlWithIndexDefault, foldrWithIndex, foldrWithIndexDefault, surroundMapWithIndex)
1212
import Data.Function (on)
1313
import Data.FunctorWithIndex (class FunctorWithIndex, mapWithIndex)
@@ -95,6 +95,16 @@ main = do
9595
log "Test bisequenceDefault"
9696
testBitraversableIOrWith BSD
9797

98+
log "Test indexl"
99+
assert $ indexl 2 [1, 5, 10] == Just 10
100+
assert $ indexl 0 [1, 5, 10] == Just 1
101+
assert $ indexl 9 [1, 5, 10] == Nothing
102+
103+
log "Test indexr"
104+
assert $ indexr 2 [1, 5, 10] == Just 1
105+
assert $ indexr 0 [1, 5, 10] == Just 10
106+
assert $ indexr 9 [1, 5, 10] == Nothing
107+
98108
log "Test find"
99109
assert $ find (_ == 10) [1, 5, 10] == Just 10
100110
assert $ find (\x -> x `mod` 2 == 0) [1, 4, 10] == Just 4

0 commit comments

Comments
 (0)