Skip to content

Commit ce51d5c

Browse files
authored
Merge pull request #15 from purescript/f-t
Add Foldable and Traversable instances for V
2 parents f934666 + a71a7d6 commit ce51d5c

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/.*
22
!/.gitignore
33
!/.travis.yml
4+
package-lock.json
45
/bower_components/
56
/node_modules/
67
/output/

bower.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
],
1919
"dependencies": {
2020
"purescript-bifunctors": "^3.0.0",
21-
"purescript-monoid": "^3.0.0"
21+
"purescript-monoid": "^3.0.0",
22+
"purescript-foldable-traversable": "^3.6.1"
2223
}
2324
}

src/Data/Validation/Semigroup.purs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ module Data.Validation.Semigroup
1414
import Prelude
1515

1616
import Control.Apply (lift2)
17-
1817
import Data.Bifunctor (class Bifunctor)
18+
import Data.Foldable (class Foldable)
1919
import Data.Monoid (class Monoid, mempty)
20+
import Data.Traversable (class Traversable)
2021

2122
-- | The `V` functor, used for applicative validation
2223
-- |
@@ -79,3 +80,12 @@ instance semigroupV :: (Semigroup err, Semigroup a) => Semigroup (V err a) where
7980

8081
instance monoidV :: (Semigroup err, Monoid a) => Monoid (V err a) where
8182
mempty = pure mempty
83+
84+
instance foldableV :: Foldable (V err) where
85+
foldMap = unV (const mempty)
86+
foldr f b = unV (const b) (flip f b)
87+
foldl f b = unV (const b) (f b)
88+
89+
instance traversableV :: Traversable (V err) where
90+
sequence = unV (pure <<< Invalid) (map Valid)
91+
traverse f = unV (pure <<< Invalid) (map Valid <<< f)

src/Data/Validation/Semiring.purs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ module Data.Validation.Semiring
1414
import Prelude
1515

1616
import Control.Alt (class Alt)
17+
import Control.Alternative (class Alternative)
1718
import Control.Apply (lift2)
1819
import Control.Plus (class Plus)
19-
import Control.Alternative (class Alternative)
20-
2120
import Data.Bifunctor (class Bifunctor)
21+
import Data.Foldable (class Foldable)
2222
import Data.Monoid (class Monoid, mempty)
23+
import Data.Traversable (class Traversable)
2324

2425
-- | The `V` functor, used for alternative validation
2526
-- |
@@ -94,3 +95,12 @@ instance plusV :: (Semiring err) => Plus (V err) where
9495
empty = Invalid zero
9596

9697
instance alernativeV :: (Semiring err) => Alternative (V err)
98+
99+
instance foldableV :: Foldable (V err) where
100+
foldMap = unV (const mempty)
101+
foldr f b = unV (const b) (flip f b)
102+
foldl f b = unV (const b) (f b)
103+
104+
instance traversableV :: Traversable (V err) where
105+
sequence = unV (pure <<< Invalid) (map Valid)
106+
traverse f = unV (pure <<< Invalid) (map Valid <<< f)

0 commit comments

Comments
 (0)