Giter VIP home page Giter VIP logo

purescript-homogeneous-objects's Introduction

purescript-homogeneous-objects

Latest release

There's no way to get homogeneous objects in Purescript due to limitations in the type system. This library provides homogeneous JSON-like objects where the keys are all String types and the values all have the same type (homegeneous) that you specified. You can think of this as analagous to a typed array except for objects. It comes in three flavors:

TupleTree

A TupleTree is just a tree representation of the Tuple type from purescript-tuples with a String as the first argument to represent the key in the key/value pair:

data TupleTree a = Leaf a | Branch (Array (Tuple String (TupleTree a)))

You can construct one with the mkTree function:

sampleTree :: TupleTree Int
sampleTree  = mkTree [ "foo" -= 3
                     , "bar" -< [ "baz" -= 4 ]
                     ]

The -< and -= combinators are syntactic sugar to make your tree representation easy to read. The -< combinator is used for Branch nodes, and -= is used for Leaf nodes.

Json

You can construct a Json type from the purescript-argonaut package using the same -< and -= combinators but constructed with hJson. You must provide an instance of the EncodeJson typeclass for your homogeneous type if it doesn't already exist in order to be able to construct a Json type:

sampleJson :: Json
sampleJson = hJson [ "foo" -= (Just 1)
                   , "bar" -< [ "baz" -= Nothing
                              , "qux" -< [ "norf" -= (Just 2) ]
                              ]
                   , "worble" -= (Just 3)
                   ]

HObject

For situations where you don't want to use Json but you want to underlying representation to be a JSON object, you can use HObject. This is useful for situations where you have more complex types that strict Json doesn't support, but you still want a JSON representation internally for use in the FFI. It is constructed with hObj:

data SampleType = StrType | NumType | BoolType

-- | This show instance makes (HObject SampleType) serializable
instance showSampleType :: Show SampleType where
   show StrType  = "[Fn String]"
   show NumType  = "[Fn Number]"
   show BoolType = "[Fn Boolean]"

sampleHObj :: HObject SampleType
sampleHObj = hObj [ "foo" -= StrType
                  , "bar" -< [ "baz" -= BoolType ]
                  , "qux" -= NumType
                  ]

purescript-homogeneous-objects's People

Contributors

aykl avatar risto-stevcev avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

aykl boygao1992

purescript-homogeneous-objects's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.