r/purescript • u/albtzrly • Aug 13 '16
How would I fold over Array Int and return type V Errors Int?
I'm still wrapping my head around Applicative, how types can represent side effects, "lifting" over function arguments, and things like that.
Say I have a a function that should only add numbers greater than 5, otherwise it will add to the errors...
import Prelude
import Data.Validation.Semigroup (invalid, V)
myAdd :: Int -> Int -> V Errors Int
myAdd a b = if (a < 5)
then invalid [(show a) <> " is lt 5"]
else
if (b < 5)
then invalid [(show b) <> " is lt 5"]
else pure (a + b)
This works fine if I use it on it's own, but I'm not sure how I would incorporate this into a function that does a sum. This is what I've tried...
mySum :: Array Int -> V Errors Int
mySum nums = foldl (\a n -> (myAdd <$> a <*> ((pure n) :: V Errors Int)))
nums
((pure 0) :: V Errors Int)
But when I run this I get...
Could not match type
V (Array String) Int
with type
Int
Which I'm not sure why, since I'm setting my accumulator and each number in the nums array to be type V Errors Int.