Skip to content

Commit

Permalink
Merge pull request #28 from afcondon/examplesForv07
Browse files Browse the repository at this point in the history
simple edits to make examples work in PSv0.7
  • Loading branch information
paf31 committed Jul 9, 2015
2 parents 70f80e2 + 87691f9 commit 883232d
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 22 deletions.
3 changes: 2 additions & 1 deletion examples/Applicative.purs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Prelude
import Data.Foreign
import Data.Foreign.Index
import Data.Foreign.Class
import Control.Monad.Eff.Console

data Point = Point Number Number Number

Expand All @@ -16,5 +17,5 @@ instance pointIsForeign :: IsForeign Point where
<*> readProp "y" value
<*> readProp "z" value

main = Console.print $
main = print $
readJSON """{ "x": 1, "y": 2, "z": 3 }""" :: F Point
5 changes: 3 additions & 2 deletions examples/Complex.purs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import Data.Foreign
import Data.Foreign.Class
import Data.Foreign.NullOrUndefined
import Data.Maybe (Maybe())
import Control.Monad.Eff.Console

data SomeObject = SomeObject { foo :: String
, bar :: Boolean
, baz :: Number
, list :: [ListItem] }
, list :: Array ListItem }

instance showSomeObject :: Show SomeObject where
show (SomeObject o) =
Expand Down Expand Up @@ -48,4 +49,4 @@ instance listItemIsForeign :: IsForeign ListItem where

main = do
let json = """{"foo":"hello","bar":true,"baz":1,"list":[{"x":1,"y":2},{"x":3,"y":4,"z":999}]}"""
Console.print $ readJSON json :: F SomeObject
print $ readJSON json :: F SomeObject
5 changes: 3 additions & 2 deletions examples/JSONArrays.purs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import Prelude

import Data.Foreign
import Data.Foreign.Class
import Control.Monad.Eff.Console

main = do
Console.print $ readJSON """["hello", "world"]""" :: F (Array String)
Console.print $ readJSON """[1, 2, 3, 4]""" :: F (Array Number)
print $ readJSON """["hello", "world"]""" :: F (Array String)
print $ readJSON """[1, 2, 3, 4]""" :: F (Array Number)
7 changes: 4 additions & 3 deletions examples/JSONSimpleTypes.purs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import Prelude

import Data.Foreign
import Data.Foreign.Class
import Control.Monad.Eff.Console

-- Parsing of the simple JSON String, Number and Boolean types is provided
-- out of the box.
main = do
Console.print $ readJSON "\"a JSON string\"" :: F String
Console.print $ readJSON "42" :: F Number
Console.print $ readJSON "true" :: F Boolean
print $ readJSON "\"a JSON string\"" :: F String
print $ readJSON "42" :: F Number
print $ readJSON "true" :: F Boolean
5 changes: 3 additions & 2 deletions examples/MaybeNullable.purs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import Prelude
import Data.Foreign
import Data.Foreign.Null
import Data.Foreign.Class
import Control.Monad.Eff.Console

-- Parsing values that are allowed to null or undefined is possible by
-- using Maybe types.
main = do
Console.print $ runNull <$> readJSON "null" :: F (Null Boolean)
Console.print $ runNull <$> readJSON "true" :: F (Null Boolean)
print $ runNull <$> readJSON "null" :: F (Null Boolean)
print $ runNull <$> readJSON "true" :: F (Null Boolean)
7 changes: 4 additions & 3 deletions examples/Nested.purs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Control.Bind ((>=>))
import Data.Foreign
import Data.Foreign.Class
import Data.Foreign.Index
import Control.Monad.Eff.Console

data Foo = Foo Bar Baz

Expand All @@ -24,9 +25,9 @@ instance showBaz :: Show Baz where

instance fooIsForeign :: IsForeign Foo where
read value = do
s <- value # prop "foo" >=> readProp "bar"
n <- value # prop "foo" >=> readProp "baz"
s <- value # (prop "foo" >=> readProp "bar")
n <- value # (prop "foo" >=> readProp "baz")
return $ Foo (Bar s) (Baz n)

main = do
Console.print $ readJSON """{ "foo": { "bar": "bar", "baz": 1 } }""" :: F Foo
print $ readJSON """{ "foo": { "bar": "bar", "baz": 1 } }""" :: F Foo
5 changes: 2 additions & 3 deletions examples/Objects.purs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import Prelude
import Data.Either
import Data.Foreign
import Data.Foreign.Class
import Control.Monad.Eff
import Debug.Trace
import Control.Monad.Eff.Console

-- | To parse objects of a particular type, we need to define some helper
-- data types as making class instances for records is not possible.
Expand All @@ -25,4 +24,4 @@ instance pointIsForeign :: IsForeign Point where
return $ Point { x: x, y: y }

main = do
Console.print $ readJSON """{ "x": 1, "y": 2 }""" :: F Point
print $ readJSON """{ "x": 1, "y": 2 }""" :: F Point
9 changes: 5 additions & 4 deletions examples/ParseErrors.purs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Prelude

import Data.Foreign
import Data.Foreign.Class
import Control.Monad.Eff.Console

-- | See the `Objects` example for an explanation of how to parse objects
-- like this.
Expand All @@ -22,15 +23,15 @@ main = do

-- When trying to parse invalid JSON we catch an exception from
-- `JSON.parse` and pass it on.
Console.print $ readJSON "not even JSON" :: F String
print $ readJSON "not even JSON" :: F String

-- When attempting to coerce one type to another we get an error.
Console.print $ readJSON "26" :: F Boolean
print $ readJSON "26" :: F Boolean

-- When parsing fails in an array, we're told at which index the value that
-- failed to parse was, along with the reason the value didn't parse.
Console.print $ readJSON "[1, true, 3]" :: F (Array Boolean)
print $ readJSON "[1, true, 3]" :: F (Array Boolean)

-- When parsing fails in an object, we're the name of the property which
-- failed to parse was, along with the reason the value didn't parse.
Console.print $ readJSON """{ "x": 1, "y": false }""" :: F Point
print $ readJSON """{ "x": 1, "y": false }""" :: F Point
5 changes: 3 additions & 2 deletions examples/Union.purs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Prelude
import Data.Foreign
import Data.Foreign.Index
import Data.Foreign.Class
import Control.Monad.Eff.Console

data StringList = Nil | Cons String StringList

Expand All @@ -22,7 +23,7 @@ instance stringListIsForeign :: IsForeign StringList where

main = do

Console.print $ readJSON """
print $ readJSON """
{ "nil": false
, "head": "Hello"
, "tail":
Expand All @@ -34,7 +35,7 @@ main = do
}
""" :: F StringList

Console.print $ readJSON """
print $ readJSON """
{ "nil": false
, "head": "Hello"
, "tail":
Expand Down

0 comments on commit 883232d

Please sign in to comment.