Coder Social home page Coder Social logo

tables's Introduction

Tables

Build Status

This package provides simple in memory data tables with multiple indices.

Examples

So if we load examples/Foo.hs into ghci, we start with:

>>> test
fromList [ Foo {fooId = 1, fooBar = "One", fooBaz = 1.0}
         , Foo {fooId = 2, fooBar = "Two", fooBaz = 2.0}
         , Foo {fooId = 3, fooBar = "Three", fooBaz = 3.0}
         , Foo {fooId = 4, fooBar = "Four", fooBaz = 4.0}
         , Foo {fooId = 5, fooBar = "Five", fooBaz = 5.0} ]

We use uppercase constructor names to match on built-in keys

>>> test ^. with FooId (<) 3
fromList [ Foo {fooId = 1, fooBar = "One", fooBaz = 1.0}
         , Foo {fooId = 2, fooBar = "Two", fooBaz = 2.0} ]

Then we can use any lowercase field accessor (or any other function) to do a non-keyed lookup or filter

>>> test ^. with (length . fooBar) (<=) 3
fromList [ Foo {fooId = 1, fooBar = "One", fooBaz = 1.0}
         , Foo {fooId = 2, fooBar = "Two", fooBaz = 2.0} ]

You can delete by assigning to that filtered table:

>>> test & with (length . fooBar) (<=) 3 .~ empty
fromList [ Foo {fooId = 3, fooBar = "Three", fooBaz = 3.0}
         , Foo {fooId = 4, fooBar = "Four", fooBaz = 4.0}
         , Foo {fooId = 5, fooBar = "Five", fooBaz = 5.0} ]

You can edit the actual type of the fields if the table is configured to allow it:

>>> test & rows.fooBar_ %~ length
fromList [ Foo {fooId = 1, fooBar = 3, fooBaz = 1.0}
         , Foo {fooId = 2, fooBar = 3, fooBaz = 2.0}
         , Foo {fooId = 3, fooBar = 5, fooBaz = 3.0}
         , Foo {fooId = 4, fooBar = 4, fooBaz = 4.0}
         , Foo {fooId = 5, fooBar = 4, fooBaz = 5.0} ]

If you edit multiple fields, the edits all take place at the same time. so we can offset or swap a bunch of keys:

>>> test & with FooId (>=) 2.rows.fooId_ +~ 1
fromList [ Foo {fooId = 1, fooBar = "One", fooBaz = 1.0}
         , Foo {fooId = 3, fooBar = "Two", fooBaz = 2.0}
         , Foo {fooId = 4, fooBar = "Three", fooBaz = 3.0}
         , Foo {fooId = 5, fooBar = "Four", fooBaz = 4.0}
         , Foo {fooId = 6, fooBar = "Five", fooBaz = 5.0} ]

We can do grouping by arbitrary functions or fields similarly

>>> test ^@.. group (length.fooBar)
[ (3, fromList [ Foo {fooId = 1, fooBar = "One", fooBaz = 1.0}
               , Foo {fooId = 2, fooBar = "Two", fooBaz = 2.0} ])
, (4, fromList [ Foo {fooId = 4, fooBar = "Four", fooBaz = 4.0}
               , Foo {fooId = 5, fooBar = "Five", fooBaz = 5.0} ])
, (5, fromList [Foo {fooId = 3, fooBar = "Three", fooBaz = 3.0} ])
]

Contact Information

Contributions and bug reports are welcome!

Please feel free to contact me through github or on the #haskell or #haskell-lens IRC channels on irc.freenode.net.

-Edward Kmett

tables's People

Contributors

ekmett avatar taneb avatar dag avatar thoughtpolice avatar palmik avatar

Watchers

Kai avatar James Cloos avatar

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.