Skip to content

Commit dfddd0d

Browse files
committed
Merge pull request #6 from jdegoes/ready/condecon
ease pain of working with nested tuples
2 parents 6c0f8df + 17047a5 commit dfddd0d

File tree

2 files changed

+117
-58
lines changed

2 files changed

+117
-58
lines changed

README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,29 @@
5757

5858
unzip :: forall a b. [Tuple a b] -> Tuple [a] [b]
5959

60-
zip :: forall a b. [a] -> [b] -> [Tuple a b]
60+
zip :: forall a b. [a] -> [b] -> [Tuple a b]
61+
62+
63+
## Module Data.Tuple.Nested
64+
65+
### Values
66+
67+
(/\) :: forall a b. a -> b -> Tuple a b
68+
69+
con10 :: forall a b c d e f g h i j z. (a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> z) -> Tuple a (Tuple b (Tuple c (Tuple d (Tuple e (Tuple f (Tuple g (Tuple h (Tuple i j)))))))) -> z
70+
71+
con2 :: forall a b z. (a -> b -> z) -> Tuple a b -> z
72+
73+
con3 :: forall a b c z. (a -> b -> c -> z) -> Tuple a (Tuple b c) -> z
74+
75+
con4 :: forall a b c d z. (a -> b -> c -> d -> z) -> Tuple a (Tuple b (Tuple c d)) -> z
76+
77+
con5 :: forall a b c d e z. (a -> b -> c -> d -> e -> z) -> Tuple a (Tuple b (Tuple c (Tuple d e))) -> z
78+
79+
con6 :: forall a b c d e f z. (a -> b -> c -> d -> e -> f -> z) -> Tuple a (Tuple b (Tuple c (Tuple d (Tuple e f)))) -> z
80+
81+
con7 :: forall a b c d e f g z. (a -> b -> c -> d -> e -> f -> g -> z) -> Tuple a (Tuple b (Tuple c (Tuple d (Tuple e (Tuple f g))))) -> z
82+
83+
con8 :: forall a b c d e f g h z. (a -> b -> c -> d -> e -> f -> g -> h -> z) -> Tuple a (Tuple b (Tuple c (Tuple d (Tuple e (Tuple f (Tuple g h)))))) -> z
84+
85+
con9 :: forall a b c d e f g h i z. (a -> b -> c -> d -> e -> f -> g -> h -> i -> z) -> Tuple a (Tuple b (Tuple c (Tuple d (Tuple e (Tuple f (Tuple g (Tuple h i))))))) -> z

src/Data/Tuple.purs

Lines changed: 91 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,118 @@
11
module Data.Tuple where
2+
import Control.Comonad
3+
import Control.Extend
4+
import Control.Lazy
25

3-
import Control.Comonad
4-
import Control.Extend
5-
import Control.Lazy
6+
import Data.Array
7+
import Data.Monoid
68

7-
import Data.Array
8-
import Data.Monoid
9+
data Tuple a b = Tuple a b
910

10-
data Tuple a b = Tuple a b
11+
instance showTuple :: (Show a, Show b) => Show (Tuple a b) where
12+
show (Tuple a b) = "Tuple (" ++ show a ++ ") (" ++ show b ++ ")"
1113

12-
instance showTuple :: (Show a, Show b) => Show (Tuple a b) where
13-
show (Tuple a b) = "Tuple (" ++ show a ++ ") (" ++ show b ++ ")"
14+
instance eqTuple :: (Eq a, Eq b) => Eq (Tuple a b) where
15+
(==) (Tuple a1 b1) (Tuple a2 b2) = a1 == a2 && b1 == b2
16+
(/=) t1 t2 = not (t1 == t2)
1417

15-
instance eqTuple :: (Eq a, Eq b) => Eq (Tuple a b) where
16-
(==) (Tuple a1 b1) (Tuple a2 b2) = a1 == a2 && b1 == b2
17-
(/=) t1 t2 = not (t1 == t2)
18+
instance ordTuple :: (Ord a, Ord b) => Ord (Tuple a b) where
19+
compare (Tuple a1 b1) (Tuple a2 b2) = case compare a1 a2 of
20+
EQ -> compare b1 b2
21+
other -> other
1822

19-
instance ordTuple :: (Ord a, Ord b) => Ord (Tuple a b) where
20-
compare (Tuple a1 b1) (Tuple a2 b2) = case compare a1 a2 of
21-
EQ -> compare b1 b2
22-
other -> other
23+
instance semigroupoidTuple :: Semigroupoid Tuple where
24+
(<<<) (Tuple _ c) (Tuple a _) = Tuple a c
2325

24-
instance semigroupoidTuple :: Semigroupoid Tuple where
25-
(<<<) (Tuple _ c) (Tuple a _) = Tuple a c
26+
instance semigroupTuple :: (Semigroup a, Semigroup b) => Semigroup (Tuple a b) where
27+
(<>) (Tuple a1 b1) (Tuple a2 b2) = Tuple (a1 <> a2) (b1 <> b2)
2628

27-
instance semigroupTuple :: (Semigroup a, Semigroup b) => Semigroup (Tuple a b) where
28-
(<>) (Tuple a1 b1) (Tuple a2 b2) = Tuple (a1 <> a2) (b1 <> b2)
29+
instance monoidTuple :: (Monoid a, Monoid b) => Monoid (Tuple a b) where
30+
mempty = Tuple mempty mempty
2931

30-
instance monoidTuple :: (Monoid a, Monoid b) => Monoid (Tuple a b) where
31-
mempty = Tuple mempty mempty
32+
instance functorTuple :: Functor (Tuple a) where
33+
(<$>) f (Tuple x y) = Tuple x (f y)
3234

33-
instance functorTuple :: Functor (Tuple a) where
34-
(<$>) f (Tuple x y) = Tuple x (f y)
35+
instance applyTuple :: (Semigroup a) => Apply (Tuple a) where
36+
(<*>) (Tuple a1 f) (Tuple a2 x) = Tuple (a1 <> a2) (f x)
3537

36-
instance applyTuple :: (Semigroup a) => Apply (Tuple a) where
37-
(<*>) (Tuple a1 f) (Tuple a2 x) = Tuple (a1 <> a2) (f x)
38+
instance applicativeTuple :: (Monoid a) => Applicative (Tuple a) where
39+
pure = Tuple mempty
3840

39-
instance applicativeTuple :: (Monoid a) => Applicative (Tuple a) where
40-
pure = Tuple mempty
41+
instance bindTuple :: (Semigroup a) => Bind (Tuple a) where
42+
(>>=) (Tuple a1 b) f = case f b of
43+
Tuple a2 c -> Tuple (a1 <> a2) c
4144

42-
instance bindTuple :: (Semigroup a) => Bind (Tuple a) where
43-
(>>=) (Tuple a1 b) f = case f b of
44-
Tuple a2 c -> Tuple (a1 <> a2) c
45+
instance monadTuple :: (Monoid a) => Monad (Tuple a)
4546

46-
instance monadTuple :: (Monoid a) => Monad (Tuple a)
47+
instance extendTuple :: Extend (Tuple a) where
48+
(<<=) f t@(Tuple a b) = Tuple a (f t)
4749

48-
instance extendTuple :: Extend (Tuple a) where
49-
(<<=) f t@(Tuple a b) = Tuple a (f t)
50+
instance comonadTuple :: Comonad (Tuple a) where
51+
extract = snd
5052

51-
instance comonadTuple :: Comonad (Tuple a) where
52-
extract = snd
53+
instance lazyTuple :: (Lazy a, Lazy b) => Lazy (Tuple a b) where
54+
defer f = Tuple (defer $ \_ -> fst (f unit)) (defer $ \_ -> snd (f unit))
5355

54-
instance lazyTuple :: (Lazy a, Lazy b) => Lazy (Tuple a b) where
55-
defer f = Tuple (defer $ \_ -> fst (f unit)) (defer $ \_ -> snd (f unit))
56+
instance lazyLazy1Tuple :: (Lazy1 l1, Lazy1 l2) => Lazy (Tuple (l1 a) (l2 b)) where
57+
defer f = Tuple (defer1 $ \_ -> fst (f unit)) (defer1 $ \_ -> snd (f unit))
5658

57-
instance lazyLazy1Tuple :: (Lazy1 l1, Lazy1 l2) => Lazy (Tuple (l1 a) (l2 b)) where
58-
defer f = Tuple (defer1 $ \_ -> fst (f unit)) (defer1 $ \_ -> snd (f unit))
59+
instance lazyLazy2Tuple :: (Lazy2 l1, Lazy2 l2) => Lazy (Tuple (l1 a b) (l2 c d)) where
60+
defer f = Tuple (defer2 $ \_ -> fst (f unit)) (defer2 $ \_ -> snd (f unit))
5961

60-
instance lazyLazy2Tuple :: (Lazy2 l1, Lazy2 l2) => Lazy (Tuple (l1 a b) (l2 c d)) where
61-
defer f = Tuple (defer2 $ \_ -> fst (f unit)) (defer2 $ \_ -> snd (f unit))
62+
fst :: forall a b. Tuple a b -> a
63+
fst (Tuple a _) = a
6264

63-
fst :: forall a b. Tuple a b -> a
64-
fst (Tuple a _) = a
65+
snd :: forall a b. Tuple a b -> b
66+
snd (Tuple _ b) = b
6567

66-
snd :: forall a b. Tuple a b -> b
67-
snd (Tuple _ b) = b
68+
curry :: forall a b c. (Tuple a b -> c) -> a -> b -> c
69+
curry f a b = f (Tuple a b)
6870

69-
curry :: forall a b c. (Tuple a b -> c) -> a -> b -> c
70-
curry f a b = f (Tuple a b)
71+
uncurry :: forall a b c. (a -> b -> c) -> Tuple a b -> c
72+
uncurry f (Tuple a b) = f a b
7173

72-
uncurry :: forall a b c. (a -> b -> c) -> Tuple a b -> c
73-
uncurry f (Tuple a b) = f a b
74+
zip :: forall a b. [a] -> [b] -> [Tuple a b]
75+
zip = zipWith Tuple
7476

75-
zip :: forall a b. [a] -> [b] -> [Tuple a b]
76-
zip = zipWith Tuple
77+
unzip :: forall a b. [Tuple a b] -> Tuple [a] [b]
78+
unzip ((Tuple a b):ts) = case unzip ts of
79+
Tuple as bs -> Tuple (a : as) (b : bs)
80+
unzip [] = Tuple [] []
7781

78-
unzip :: forall a b. [Tuple a b] -> Tuple [a] [b]
79-
unzip ((Tuple a b):ts) = case unzip ts of
80-
Tuple as bs -> Tuple (a : as) (b : bs)
81-
unzip [] = Tuple [] []
82+
swap :: forall a b. Tuple a b -> Tuple b a
83+
swap (Tuple a b) = Tuple b a
8284

83-
swap :: forall a b. Tuple a b -> Tuple b a
84-
swap (Tuple a b) = Tuple b a
85+
module Data.Tuple.Nested where
86+
import Data.Tuple
87+
88+
con2 :: forall a b z. (a -> b -> z) -> (Tuple a b) -> z
89+
con2 f = \(Tuple a b) -> f a b
90+
91+
con3 :: forall a b c z. (a -> b -> c -> z) -> (Tuple a (Tuple b c)) -> z
92+
con3 f = \(Tuple a (Tuple b c)) -> f a b c
93+
94+
con4 :: forall a b c d z. (a -> b -> c -> d -> z) -> (Tuple a (Tuple b (Tuple c d))) -> z
95+
con4 f = \(Tuple a (Tuple b (Tuple c d))) -> f a b c d
96+
97+
con5 :: forall a b c d e z. (a -> b -> c -> d -> e -> z) -> (Tuple a (Tuple b (Tuple c (Tuple d e)))) -> z
98+
con5 f = \(Tuple a (Tuple b (Tuple c (Tuple d e)))) -> f a b c d e
99+
100+
con6 :: forall a b c d e f z. (a -> b -> c -> d -> e -> f -> z) -> (Tuple a (Tuple b (Tuple c (Tuple d (Tuple e f))))) -> z
101+
con6 f = \(Tuple a (Tuple b (Tuple c (Tuple d (Tuple e f'))))) -> f a b c d e f'
102+
103+
con7 :: forall a b c d e f g z. (a -> b -> c -> d -> e -> f -> g -> z) -> (Tuple a (Tuple b (Tuple c (Tuple d (Tuple e (Tuple f g)))))) -> z
104+
con7 f = \(Tuple a (Tuple b (Tuple c (Tuple d (Tuple e (Tuple f' g)))))) -> f a b c d e f' g
105+
106+
con8 :: forall a b c d e f g h z. (a -> b -> c -> d -> e -> f -> g -> h -> z) -> (Tuple a (Tuple b (Tuple c (Tuple d (Tuple e (Tuple f (Tuple g h))))))) -> z
107+
con8 f = \(Tuple a (Tuple b (Tuple c (Tuple d (Tuple e (Tuple f' (Tuple g h))))))) -> f a b c d e f' g h
108+
109+
con9 :: forall a b c d e f g h i z. (a -> b -> c -> d -> e -> f -> g -> h -> i -> z) -> (Tuple a (Tuple b (Tuple c (Tuple d (Tuple e (Tuple f (Tuple g (Tuple h i)))))))) -> z
110+
con9 f = \(Tuple a (Tuple b (Tuple c (Tuple d (Tuple e (Tuple f' (Tuple g (Tuple h i)))))))) -> f a b c d e f' g h i
111+
112+
con10 :: forall a b c d e f g h i j z. (a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> z) -> (Tuple a (Tuple b (Tuple c (Tuple d (Tuple e (Tuple f (Tuple g (Tuple h (Tuple i j))))))))) -> z
113+
con10 f = \(Tuple a (Tuple b (Tuple c (Tuple d (Tuple e (Tuple f' (Tuple g (Tuple h (Tuple i j))))))))) -> f a b c d e f' g h i j
114+
115+
infixr 6 /\
116+
117+
(/\) :: forall a b. a -> b -> Tuple a b
118+
(/\) a b = Tuple a b

0 commit comments

Comments
 (0)