Created
September 12, 2018 18:22
-
-
Save paulvictor/e948a4f20206ffbab133f842d180f9bc to your computer and use it in GitHub Desktop.
Using Json for multiple types into an array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module MultipleTypes where | |
| import Data.Argonaut.Decode.Combinators ((.?)) | |
| import Prelude | |
| import Data.Argonaut.Core (Json, caseJson) | |
| import Data.Argonaut.Core (stringify) as Json | |
| import Data.Argonaut.Decode.Class (decodeJson) | |
| import Data.Argonaut.Encode.Class (encodeJson) | |
| import Data.Either (Either, either) | |
| import Data.Traversable (traverse_) | |
| import Effect (Effect) | |
| import Effect.Class.Console (log) | |
| import Foreign.Object (singleton) as Obj | |
| appendFoo :: Json -> Either String Json | |
| appendFoo = decodeJson >>> map ((_ <> "foo") >>> encodeJson) | |
| addOne :: Json -> Either String Json | |
| addOne = decodeJson >>> map ((_ + 1) >>> encodeJson) | |
| getFooANdAppendBar :: Json -> Either String Json | |
| getFooANdAppendBar = decodeJson >=> (_ .? "foo") >>> map ((_ <> "bar") >>> encodeJson) | |
| allFunctions :: Array (Json → Either String Json) | |
| allFunctions = [appendFoo, addOne, getFooANdAppendBar] | |
| allArgs :: Array Json | |
| allArgs = [encodeJson 1, encodeJson "foo", encodeJson (Obj.singleton "foo" "bar")] | |
| applyingAllArgsToAllFunctions :: Array (Either String Json) | |
| applyingAllArgsToAllFunctions = allFunctions <*> allArgs | |
| printResults :: Array (Either String Json) → Effect Unit | |
| printResults = traverse_ log <<< map (either show (caseJson (const "value is Null") show show identity (encodeJson >>> Json.stringify) (encodeJson >>> Json.stringify))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment