Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
"output"
],
"dependencies": {
"purescript-integers": "^3.1.0",
"purescript-strings": "^3.3.0",
"purescript-foldable-traversable": "^3.4.0",
"purescript-lists": "^4.9.0"
"purescript-integers": "^4.0.0",
"purescript-strings": "^4.0.1",
"purescript-foldable-traversable": "^4.1.1",
"purescript-lists": "^5.3.0"
},
"devDependencies": {
"purescript-psci-support": "^3.0.0"
"purescript-psci-support": "^4.0.0"
}
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
},
"homepage": "https://github.com/Thimoteus/purescript-coercible#readme",
"dependencies": {
"purescript": "^0.11.6",
"pulp": "^12.3.0",
"purescript": "^0.12.1",
"purescript-psa": "^0.5.1",
"rimraf": "^2.6.1",
"pulp": "^11.0.2"
"rimraf": "^2.6.1"
}
}
67 changes: 30 additions & 37 deletions src/Control/Coercible.purs
Original file line number Diff line number Diff line change
Expand Up @@ -9,56 +9,49 @@ import Data.Foldable (foldMap)
import Data.Int (toNumber)
import Data.List (List(..), (:), reverse)
import Data.Maybe (Maybe(..))
import Data.String (fromCharArray, toCharArray, uncons)
import Data.String (CodePoint, codePointFromChar, fromCodePointArray, toCodePointArray, uncons)

class Coercible a b where
coerce :: a -> b

instance coercibleId :: Coercible a a where
coerce = id

instance coercibleUnit :: Coercible a Unit where
coerce _ = unit

instance coercibleVoid :: Coercible Void a where
coerce = absurd

instance coercibleImage :: Coercible a (b -> a) where
coerce = const

instance coercibleIntNumber :: Coercible Int Number where
coerce = toNumber

instance coercibleCharString :: Coercible Char String where
coerce = fromChar

instance coercibleArrayCharString :: Coercible (Array Char) String where
coerce = fromCharArray

instance coercibleStringArrayChar :: Coercible String (Array Char) where
coerce = toCharArray

instance coercibleFunctor :: (Functor f, Coercible a b) => Coercible (f a) (f b) where
else instance coercibleCodePointString :: Coercible CodePoint String where
coerce = fromCodePoint
else instance coercibleCharCodePoint :: Coercible Char CodePoint where
coerce = codePointFromChar
else instance coercibleCharString :: Coercible Char String where
coerce = fromCodePoint <<< codePointFromChar
else instance coercibleArrayCodePointString :: Coercible (Array CodePoint) String where
coerce = fromCodePointArray
else instance coercibleArrayCharString :: Coercible (Array Char) String where
coerce = fromCodePointArray <<< map codePointFromChar
else instance coercibleStringArrayCodePoint :: Coercible String (Array CodePoint) where
coerce = toCodePointArray
else instance coercibleFunctor :: (Functor f, Coercible a b) => Coercible (f a) (f b) where
coerce = map coerce

instance coercibleApplicative :: Applicative f => Coercible a (f a) where
else instance coercibleApplicative :: Applicative f => Coercible a (f a) where
coerce = pure

instance coercibleBind :: Bind m => Coercible (m (m a)) (m a) where
else instance coercibleBind :: Bind m => Coercible (m (m a)) (m a) where
coerce = join

instance coercibleComonad :: Comonad m => Coercible (m a) a where
else instance coercibleComonad :: Comonad m => Coercible (m a) a where
coerce = extract

instance coercableListCharString :: Coercible (List Char) String where
coerce = foldMap fromChar

instance coercibleStringListChar :: Coercible String (List Char) where
else instance coercableListCodePointString :: Coercible (List CodePoint) String where
coerce = foldMap fromCodePoint
else instance coercableListCharString :: Coercible (List Char) String where
coerce = foldMap (fromCodePoint <<< codePointFromChar)
else instance coercibleStringListCodePoint :: Coercible String (List CodePoint) where
coerce = reverse <<< coerce' Nil where
coerce' acc str =
case uncons str of
Just { head, tail } -> coerce' (head : acc) str
_ -> acc
else instance coercibleUnit :: Coercible a Unit where
coerce _ = unit
else instance coercibleVoid :: Coercible Void a where
coerce = absurd
else instance coercibleImage :: Coercible a (b -> a) where
coerce = const

fromChar :: Char -> String
fromChar c = fromCharArray [c]
fromCodePoint :: CodePoint -> String
fromCodePoint c = fromCodePointArray [c]
11 changes: 5 additions & 6 deletions test/Main.purs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module Test.Main where

import Prelude
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, logShow)

import Data.List ((:), List(Nil))
import Control.Coercible (class Coercible, coerce)
import Data.List ((:), List(Nil))
import Effect (Effect)
import Effect.Class.Console (logShow)

data N = Z | S N

Expand All @@ -16,11 +16,10 @@ instance showN :: Show N where
instance coerceNInt :: Coercible N Int where
coerce Z = 0
coerce (S n) = 1 + coerce n

instance coerceNString :: Coercible N String where
else instance coerceNString :: Coercible N String where
coerce = show

main :: forall e. Eff (console :: CONSOLE | e) Unit
main :: Effect Unit
main = do
let three = S (S (S Z))
logShow $ coerce three :: String
Expand Down