Skip to content

Commit a6befc6

Browse files
committed
Generalise LHS of (!)
1 parent 116a663 commit a6befc6

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

examples/Nested.purs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Control.Monad.Eff.Console (CONSOLE, logShow)
77
import Control.Monad.Except (runExcept)
88

99
import Data.Foreign (F, Foreign, readNumber, readString)
10-
import Data.Foreign.Index ((!), (!!))
10+
import Data.Foreign.Index ((!))
1111

1212
import Example.Util.Value (foreignValue)
1313

@@ -28,8 +28,8 @@ instance showBaz :: Show Baz where
2828

2929
readFoo :: Foreign -> F Foo
3030
readFoo value = do
31-
s <- value ! "foo" !! "bar" >>= readString
32-
n <- value ! "foo" !! "baz" >>= readNumber
31+
s <- value ! "foo" ! "bar" >>= readString
32+
n <- value ! "foo" ! "baz" >>= readNumber
3333
pure $ Foo (Bar s) (Baz n)
3434

3535
main :: Eff (console :: CONSOLE) Unit

src/Data/Foreign/Index.purs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,38 @@
33

44
module Data.Foreign.Index
55
( class Index
6+
, class Indexable
67
, readProp
78
, readIndex
89
, ix, (!)
9-
, ixKleisli, (!!)
10+
, index
1011
, hasProperty
1112
, hasOwnProperty
1213
, errorAt
1314
) where
1415

1516
import Prelude
1617

18+
import Control.Monad.Except.Trans (ExceptT)
19+
1720
import Data.Foreign (Foreign, F, ForeignError(..), typeOf, isUndefined, isNull, fail)
1821
import Data.Function.Uncurried (Fn2, runFn2, Fn4, runFn4)
22+
import Data.Identity (Identity)
23+
import Data.List.NonEmpty (NonEmptyList)
1924

2025
-- | This type class identifies types that act like _property indices_.
2126
-- |
2227
-- | The canonical instances are for `String`s and `Int`s.
2328
class Index i where
24-
ix :: Foreign -> i -> F Foreign
29+
index :: Foreign -> i -> F Foreign
2530
hasProperty :: i -> Foreign -> Boolean
2631
hasOwnProperty :: i -> Foreign -> Boolean
2732
errorAt :: i -> ForeignError -> ForeignError
2833

29-
infixl 9 ix as !
30-
31-
ixKleisli :: forall i. Index i => F Foreign -> i -> F Foreign
32-
ixKleisli f i = f >>= (_ ! i)
34+
class Indexable a where
35+
ix :: forall i. Index i => a -> i -> F Foreign
3336

34-
infixl 9 ixKleisli as !!
37+
infixl 9 ix as !
3538

3639
foreign import unsafeReadPropImpl :: forall r k. Fn4 r (Foreign -> r) k Foreign r
3740

@@ -64,13 +67,19 @@ hasPropertyImpl p value | typeOf value == "object" || typeOf value == "function"
6467
hasPropertyImpl _ value = false
6568

6669
instance indexString :: Index String where
67-
ix = flip readProp
70+
index = flip readProp
6871
hasProperty = hasPropertyImpl
6972
hasOwnProperty = hasOwnPropertyImpl
7073
errorAt = ErrorAtProperty
7174

7275
instance indexInt :: Index Int where
73-
ix = flip readIndex
76+
index = flip readIndex
7477
hasProperty = hasPropertyImpl
7578
hasOwnProperty = hasOwnPropertyImpl
7679
errorAt = ErrorAtIndex
80+
81+
instance indexableForeign :: Indexable Foreign where
82+
ix = index
83+
84+
instance indexableExceptT :: Indexable (ExceptT (NonEmptyList ForeignError) Identity Foreign) where
85+
ix f i = flip index i =<< f

0 commit comments

Comments
 (0)