Giter Site home page Giter Site logo

ra-data-record's Introduction

Build Status

NAME

Data::Record - Record types!

SYNOPSIS

use Data::Record;

# Data::Record introduces record types for maps, lists, and tuples:
my constant Schema = {@
    name  => Str:D,
    items => [@ <@ Int:D, Str:D @> @]
@} :name('Schema');

# With the type we just made, we can typecheck data structures to ensure they
# match it:
my %valid =
    name  => 'Kaiepi',
    items => [(1, 'Item 1'), (2, 'Item 2')];
my %invalid =
    name        => 'Mrofnet',
    items       => [],
    constructor => 'Thanks, JavaScript.';
say %valid ~~ Schema;   # OUTPUT: True
say %invalid ~~ Schema; # OUTPUT: False

# ...but typechecking this way is inefficient, and is always done eagerly!
# Using the (<<), (>>), (<>), and (><) operators provided, data can be coerced
# to our record type by various means:
%valid   := %valid (><) Schema;
%invalid := %invalid (<<) Schema;
say %invalid; # OUTPUT: {items => [], name => Mrofnet}

# For the most part, coerced data can be used the same way as the original
# data, with the bonus of extra typechecking:
{
    CATCH { default { say .^name } }
    %invalid<items>.push: (3, 'Item 3');        # OK!
    %invalid<items>.push: "OOPSIE WOOPSIE OwO"; # OUTPUT: X::Data::Record::TypeCheck
}

# Finally, to restore the data's original typing, simply call the unrecord
# method on it:
my %now-valid := %invalid.unrecord;
say %now-valid.^name;           # OUTPUT: Hash
say %now-valid<items>.^name;    # OUTPUT: Array
say %now-valid<items>[0].^name; # OUTPUT: List

DESCRIPTION

Data::Record is a library that adds support for record types to Raku. Operators for creating record types for maps, lists, and tuples are included. Data structures can then be coerced to these record types using the coercion operators provided, some of which will sanitize them, all of which allow for efficient typechecking for common operations you can do with them.

For documentation on how this library can be used, refer to the wiki.

AUTHOR

Ben Davies (Kaiepi)

COPYRIGHT AND LICENSE

Copyright 2022 Ben Davies

This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.

ra-data-record's People

Contributors

jnthn avatar kaiepi avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

jnthn

ra-data-record'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.