Skip to content

Commit

Permalink
Change Pair constructor operator to tilde sign (~)
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkdp committed Aug 27, 2016
1 parent 512c4b2 commit 23f270e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 48 deletions.
13 changes: 8 additions & 5 deletions README.md
Expand Up @@ -11,15 +11,18 @@ Example
-------

``` purescript
> let point1 = 2 ^ 3
> let point2 = 4 ^ 7
> let point1 = 2 ~ 3
> let point2 = 4 ~ 7
> sum point1
5
> (+) <$> point1 <*> point2
(6) ^ (10)
(6 ~ 10)
> ("Hello" ^ "foo") <> pure " " <> ("World" ^ "bar")
("Hello World") ^ ("foo bar")
> ("Hello" ~ "foo") <> pure " " <> ("World" ~ "bar")
("Hello World" ~ "foo bar")
> collect (\x → x ~ x*x) (1 .. 5)
([1,2,3,4,5] ~ [1,4,9,16,25])
```
3 changes: 2 additions & 1 deletion bower.json
Expand Up @@ -23,6 +23,7 @@
},
"devDependencies": {
"purescript-quickcheck-laws": "^1.0.0",
"purescript-assert": "^1.0.0"
"purescript-assert": "^1.0.0",
"purescript-psci-support": "^1.0.0"
}
}
72 changes: 36 additions & 36 deletions src/Data/Pair.purs
@@ -1,10 +1,10 @@
-- | This module defines a datatype `Pair` together with a few useful instances
-- | and helper functions. Note that this is not just `Tuple a a` but rather a
-- | list with exactly two elements. Specifically, the `Functor` instance maps
-- | over both values in contrast to the `Functor` instance for `Tuple`.
-- | over both values (in contrast to the `Functor` instance for `Tuple a`).
module Data.Pair
( Pair(..)
, (^)
, (~)
, fst
, snd
, curry
Expand All @@ -24,67 +24,67 @@ import Test.QuickCheck.Arbitrary (class Arbitrary, arbitrary)
-- | A pair simply consists of two values of the same type.
data Pair a = Pair a a

infixl 6 Pair as ^
infixl 6 Pair as ~

-- | Returns the first component of a pair.
fst :: a. Pair a -> a
fst (x ^ _) = x
fst a. Pair a a
fst (x ~ _) = x

-- | Returns the second component of a pair.
snd :: a. Pair a -> a
snd (_ ^ y) = y
snd a. Pair a a
snd (_ ~ y) = y

-- | Turn a function that expects a pair into a function of two arguments.
curry :: a b. (Pair a -> b) -> a -> a -> b
curry f x y = f (x ^ y)
curry a b. (Pair a b) a a b
curry f x y = f (x ~ y)

-- | Turn a function of two arguments into a function that expects a pair.
uncurry :: a b. (a -> a -> b) -> Pair a -> b
uncurry f (x ^ y) = f x y
uncurry a b. (a a b) Pair a b
uncurry f (x ~ y) = f x y

-- | Exchange the two components of the pair
swap :: a. Pair a -> Pair a
swap (x ^ y) = y ^ x
swap a. Pair a Pair a
swap (x ~ y) = y ~ x

derive instance eqPair :: Eq a => Eq (Pair a)
derive instance eqPair Eq a Eq (Pair a)

derive instance ordPair :: Ord a => Ord (Pair a)
derive instance ordPair Ord a Ord (Pair a)

instance showPair :: Show a => Show (Pair a) where
show (x ^ y) = "(" <> show x <> ") ^ (" <> show y <> ")"
instance showPair Show a Show (Pair a) where
show (x ~ y) = "(" <> show x <> " ~ " <> show y <> ")"

instance functorPair :: Functor Pair where
map f (x ^ y) = f x ^ f y
instance functorPair Functor Pair where
map f (x ~ y) = f x ~ f y

instance applyPair :: Apply Pair where
apply (f ^ g) (x ^ y) = f x ^ g y
instance applyPair Apply Pair where
apply (f ~ g) (x ~ y) = f x ~ g y

instance applicativePair :: Applicative Pair where
pure x = x ^ x
instance applicativePair Applicative Pair where
pure x = x ~ x

instance bindPair :: Bind Pair where
bind (x ^ y) f = fst (f x) ^ snd (f y)
instance bindPair Bind Pair where
bind (x ~ y) f = fst (f x) ~ snd (f y)

instance monadPair :: Monad Pair
instance monadPair Monad Pair

instance semigroupPair :: Semigroup a => Semigroup (Pair a) where
append (x1 ^ y1) (x2 ^ y2) = (x1 <> x2) ^ (y1 <> y2)
instance semigroupPair Semigroup a Semigroup (Pair a) where
append (x1 ~ y1) (x2 ~ y2) = (x1 <> x2) ~ (y1 <> y2)

instance monoidPair :: Monoid a => Monoid (Pair a) where
mempty = mempty ^ mempty
instance monoidPair Monoid a Monoid (Pair a) where
mempty = mempty ~ mempty

instance foldablePair :: Foldable Pair where
instance foldablePair Foldable Pair where
foldr f z (Pair x y) = x `f` (y `f` z)
foldl f z (Pair x y) = (z `f` x) `f` y
foldMap f (Pair x y) = f x <> f y

instance traversablePair :: Traversable Pair where
instance traversablePair Traversable Pair where
traverse f (Pair x y) = Pair <$> f x <*> f y
sequence (Pair mx my) = Pair <$> mx <*> my

instance distributivePair :: Distributive Pair where
distribute xs = map fst xs ^ map snd xs
collect f xs = map (fst <<< f) xs ^ map (snd <<< f) xs
instance distributivePair Distributive Pair where
distribute xs = map fst xs ~ map snd xs
collect f xs = map (fst <<< f) xs ~ map (snd <<< f) xs

instance arbitraryPair :: Arbitrary a => Arbitrary (Pair a) where
instance arbitraryPair Arbitrary a Arbitrary (Pair a) where
arbitrary = Pair <$> arbitrary <*> arbitrary
12 changes: 6 additions & 6 deletions test/Main.purs
Expand Up @@ -5,7 +5,7 @@ import Prelude
import Type.Proxy (Proxy(..), Proxy2(..))

import Data.Array (cons, snoc, fromFoldable)
import Data.Pair (Pair(..), (^), fst, snd, swap, uncurry)
import Data.Pair (Pair(..), (~), fst, snd, swap, uncurry)
import Data.Foldable (foldMap, foldr, foldl)
import Data.Traversable (sum, product, sequence)
import Data.Distributive (distribute, collect)
Expand Down Expand Up @@ -65,14 +65,14 @@ main = do
assert $ (sum <<< map square) p1 == 13
assert $ p2 > p1
assert $ product p2 == 28
assert $ show p1 == "(2) ^ (3)"
assert $ show p1 == "(2 ~ 3)"
assert $ foldMap show p1 == "23"
assert $ fromFoldable p1 == [2, 3]
assert $ foldr cons [] p1 == [2, 3]
assert $ foldl snoc [] p1 == [2, 3]
assert $ ((+) <$> p1 <*> p2) == point 6 10
assert $ (uncurry (+) p1) == 5
assert $ sequence (Just 2 ^ Just 5) == Just (2 ^ 5)
assert $ sequence (Just 2 ^ Nothing) == Nothing
assert $ distribute [2 ^ 3, 4 ^ 5] == [2, 4] ^ [3, 5]
assert $ collect (\x -> x ^ square x) [1, 2, 3] == [1, 2, 3] ^ [1, 4, 9]
assert $ sequence (Just 2 ~ Just 5) == Just (2 ~ 5)
assert $ sequence (Just 2 ~ Nothing) == Nothing
assert $ distribute [2 ~ 3, 4 ~ 5] == [2, 4] ~ [3, 5]
assert $ collect (\x -> x ~ square x) [1, 2, 3] == [1, 2, 3] ~ [1, 4, 9]

0 comments on commit 23f270e

Please sign in to comment.