Created
September 25, 2016 03:00
-
-
Save tushardave26/5e9d78a33e6fc5bacf516b08de5b497d to your computer and use it in GitHub Desktop.
This file contains 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
### DataTable.pm6 file content ### | |
use v6; | |
use Data::Dump; | |
unit class DataTable:ver<0.0.1>:auth<github:tushardave26>; | |
#custom types | |
subset Int-or-Str where Int|Str; | |
subset Array-or-Str where Array|Str; | |
subset Array-or-Int where Array|Int; | |
#attributes | |
has Array @.data is rw = []; | |
has Int-or-Str @.header is rw = []; | |
has Int $.type is readonly = 0; | |
# This method performs several sanity checks. | |
method !sanity-check () { | |
# 1. check whether all elements of all arrays of array of arrays (i.e. data) is equal or not | |
unless [==] @!data { | |
fail "The number of observations in each rows are not equal.!!"; | |
} | |
# 2. check whether the header is provided in object creation or not | |
unless @!header { | |
@!header = @!data[0].keys.map('V'~*) | |
} | |
# 3. check whether the number of observation meaning number elements in a row is equal to | |
# number of columns or not | |
unless @!data.[0].elems == @!header.elems { | |
fail "The number of observations and number of columns are not equal.!!"; | |
} | |
# call sanity-check method within it definition | |
#self!sanity-check(); | |
#return True; | |
} | |
multi method get-row (Int :$index! --> Array) { | |
# check the provided data consistency and other possible issues | |
self!sanity-check; | |
return @!data[$index].Array; | |
} | |
multi method get-row (Array :@index! --> Array) { | |
# check the provided data consistency and other possible issues | |
self!sanity-check; | |
return @!data[@index]; | |
} | |
### test.p6 content ### | |
use v6; | |
use lib '../lib'; | |
use Data::Dump; | |
use DataTable; | |
my $dt = DataTable.new(data => [["Tushar", "Dave", 29], ["John", "Adams", 30]], | |
header => ["Fname", "Lname", "Age"], | |
); | |
my @row = $dt.get-row(index => [0..1]); | |
#my @row = $dt.get-row(index => 0); | |
@row.say; | |
exit; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment