Skip to content

Commit 427071f

Browse files
committed
cleaned up data constructors for JCursor
1 parent a3470b7 commit 427071f

File tree

2 files changed

+25
-25
lines changed

2 files changed

+25
-25
lines changed

src/Data/Argonaut/JCursor.purs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module Data.Argonaut.JCursor
1919
import qualified Data.Array as A
2020
import qualified Data.StrMap as M
2121

22-
data JCursor = JCursorTop | JField JCursor String | JIndex JCursor Number
22+
data JCursor = JCursorTop | JField String JCursor | JIndex Number JCursor
2323

2424
newtype JsonPrim = JsonPrim (forall a. (JNull -> a) -> (JBoolean -> a) -> (JNumber -> a) -> (JString -> a) -> a)
2525

@@ -45,26 +45,26 @@ module Data.Argonaut.JCursor
4545

4646
insideOut :: JCursor -> JCursor
4747
insideOut JCursorTop = JCursorTop
48-
insideOut (JField c i) = downField i (insideOut c)
49-
insideOut (JIndex c i) = downIndex i (insideOut c)
48+
insideOut (JField i c) = downField i (insideOut c)
49+
insideOut (JIndex i c) = downIndex i (insideOut c)
5050

5151
downField :: String -> JCursor -> JCursor
5252
downField i = downField' where
53-
downField' JCursorTop = JField JCursorTop i
54-
downField' (JField c i') = JField (downField' c) i'
55-
downField' (JIndex c i') = JIndex (downField' c) i'
53+
downField' JCursorTop = JField i JCursorTop
54+
downField' (JField i' c) = JField i' (downField' c)
55+
downField' (JIndex i' c) = JIndex i' (downField' c)
5656

5757
downIndex :: Number -> JCursor -> JCursor
5858
downIndex i = downIndex' where
59-
downIndex' JCursorTop = JIndex JCursorTop i
60-
downIndex' (JField c i') = JField (downIndex' c) i'
61-
downIndex' (JIndex c i') = JIndex (downIndex' c) i'
59+
downIndex' JCursorTop = JIndex i JCursorTop
60+
downIndex' (JField i' c) = JField i' (downIndex' c)
61+
downIndex' (JIndex i' c) = JIndex i' (downIndex' c)
6262

6363
cursorGet :: JCursor -> Json -> Maybe Json
6464
cursorGet JCursorTop = Just
65-
cursorGet (JField c i) = foldJsonObject Nothing g where
65+
cursorGet (JField i c) = foldJsonObject Nothing g where
6666
g m = M.lookup i m >>= cursorGet c
67-
cursorGet (JIndex c i) = foldJsonArray Nothing g where
67+
cursorGet (JIndex i c) = foldJsonArray Nothing g where
6868
g a = a A.!! i >>= cursorGet c
6969

7070
inferEmpty :: JCursor -> Json
@@ -74,10 +74,10 @@ module Data.Argonaut.JCursor
7474

7575
cursorSet :: JCursor -> Json -> Json -> Maybe Json
7676
cursorSet JCursorTop v = Just <<< const v
77-
cursorSet (JField c i) v = foldJson (const d) (const d) (const d) (const d) (const d) g where
77+
cursorSet (JField i c) v = foldJson (const d) (const d) (const d) (const d) (const d) g where
7878
d = fromObject <<< M.singleton i <$> cursorSet c v (inferEmpty c)
7979
g m = fromObject <<< flip (M.insert i) m <$> (cursorSet c v $ fromMaybe (inferEmpty c) (M.lookup i m))
80-
cursorSet (JIndex c i) v = foldJson (const d) (const d) (const d) (const d) g (const d) where
80+
cursorSet (JIndex i c) v = foldJson (const d) (const d) (const d) (const d) g (const d) where
8181
d = fromArray <<< flip (A.updateAt i) (const jsonNull <$> A.range 0 i) <$> cursorSet c v (inferEmpty c)
8282
g a = (cursorSet c v $ fromMaybe (inferEmpty c) (a A.!! i)) >>= setArr a i
8383

@@ -95,10 +95,10 @@ module Data.Argonaut.JCursor
9595
(\s -> [Tuple JCursorTop $ primStr s])
9696
(\a -> let zipped = A.zipWith Tuple (A.range 0 (A.length a - 1)) a
9797

98-
f (Tuple i j) = (\t -> Tuple (JIndex (fst t) i) (snd t)) <$> toPrims j
98+
f (Tuple i j) = (\t -> Tuple (JIndex i (fst t)) (snd t)) <$> toPrims j
9999

100100
in zipped >>= f)
101-
(\o -> let f (Tuple i j) = (\t -> Tuple (JField (fst t) i) (snd t)) <$> toPrims j
101+
(\o -> let f (Tuple i j) = (\t -> Tuple (JField i (fst t)) (snd t)) <$> toPrims j
102102
in M.toList o >>= f)
103103

104104
fromPrims :: [Tuple JCursor JsonPrim] -> Maybe Json
@@ -108,16 +108,16 @@ module Data.Argonaut.JCursor
108108

109109
instance showJCursor :: Show JCursor where
110110
show JCursorTop = ""
111-
show (JField c i) = "." ++ i ++ show c
112-
show (JIndex c i) = "[" ++ show i ++ "]" ++ show c
111+
show (JField i c) = "." ++ i ++ show c
112+
show (JIndex i c) = "[" ++ show i ++ "]" ++ show c
113113

114114
instance showJsonPrim :: Show JsonPrim where
115115
show p = runJsonPrim p show show show show
116116

117117
instance eqJCursor :: Eq JCursor where
118118
(==) JCursorTop JCursorTop = true
119-
(==) (JField c1 i1) (JField c2 i2) = i1 == i2 && c1 == c2
120-
(==) (JIndex c1 i1) (JIndex c2 i2) = i1 == i2 && c1 == c2
119+
(==) (JField i1 c1) (JField i2 c2) = i1 == i2 && c1 == c2
120+
(==) (JIndex i1 c1) (JIndex i2 c2) = i1 == i2 && c1 == c2
121121
(==) _ _ = false
122122

123123
(/=) a b = not (a == b)
@@ -128,18 +128,18 @@ module Data.Argonaut.JCursor
128128
compare _ JCursorTop = GT
129129
compare (JField _ _) (JIndex _ _) = LT
130130
compare (JIndex _ _) (JField _ _) = GT
131-
compare (JField c1 i1) (JField c2 i2) = case compare i1 i2 of
131+
compare (JField i1 c1) (JField i2 c2) = case compare i1 i2 of
132132
EQ -> compare c1 c2
133133
x -> x
134-
compare (JIndex c1 i1) (JIndex c2 i2) = case compare i1 i2 of
134+
compare (JIndex i1 c1) (JIndex i2 c2) = case compare i1 i2 of
135135
EQ -> compare c1 c2
136136
x -> x
137137

138138
instance semigroupJCursor :: Semigroup JCursor where
139139
(<>) a JCursorTop = a
140140
(<>) JCursorTop b = b
141-
(<>) (JField a i) b = JField (a <> b) i
142-
(<>) (JIndex a i) b = JIndex (a <> b) i
141+
(<>) (JField i a) b = JField i (a <> b)
142+
(<>) (JIndex i a) b = JIndex i (a <> b)
143143

144144
instance monoidJCursor :: Monoid JCursor where
145145
mempty = JCursorTop

src/Data/Argonaut/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,8 @@
215215

216216
data JCursor where
217217
JCursorTop :: JCursor
218-
JField :: JCursor -> String -> JCursor
219-
JIndex :: JCursor -> Number -> JCursor
218+
JField :: String -> JCursor -> JCursor
219+
JIndex :: Number -> JCursor -> JCursor
220220

221221
newtype JsonPrim where
222222
JsonPrim :: (forall a. (JNull -> a) -> (JBoolean -> a) -> (JNumber -> a) -> (JString -> a) -> a) -> JsonPrim

0 commit comments

Comments
 (0)