Advertisement
Guest User

Untitled

a guest
Sep 24th, 2016
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6 6.57 KB | None | 0 0
  1. ### DataTable.pm6 file content ###
  2. use v6;                                                                                                
  3. use Data::Dump;                                                                                        
  4.                                                                                                        
  5. unit class DataTable:ver<0.0.1>:auth<github:tushardave26>;                                            
  6.                                                                                                        
  7. #custom types                                                                                          
  8. subset Int-or-Str where Int|Str;                                                                      
  9. subset Array-or-Str where Array|Str;                                                                  
  10. subset Array-or-Int where Array|Int;                                                                  
  11.                                                                                                        
  12. #attributes                                                                                            
  13. has Array @.data is rw = [];                                                                          
  14. has Int-or-Str @.header is rw = [];                                                                    
  15. has Int $.type is readonly = 0;
  16.  
  17. # This method performs several sanity checks.                                                      
  18. method !sanity-check () {                                                                          
  19.                                                                                                    
  20.     # 1. check whether all elements of all arrays of array of arrays (i.e. data) is equal or not    
  21.     unless [==] @!data {                                                                            
  22.         fail "The number of observations in each rows are not equal.!!";                            
  23.     }                                                                                              
  24.                                                                                                    
  25.     # 2. check whether the header is provided in object creation or not                            
  26.     unless @!header {                                                                              
  27.         @!header = @!data[0].keys.map('V'~*)                                                        
  28.     }                                                                                              
  29.                                                                                                    
  30.     # 3. check whether the number of observation meaning number elements in a row is equal to      
  31.     # number of columns or not                                                                      
  32.     unless @!data.[0].elems == @!header.elems {                                                    
  33.         fail "The number of observations and number of columns are not equal.!!";                  
  34.     }                                                                                              
  35.                                                                                                    
  36.     # call sanity-check method within it definition                                                
  37.     #self!sanity-check();                                                                          
  38.                                                                                                    
  39.     #return True;                                                                                  
  40. }
  41.  
  42. multi method get-row (Int :$index! --> Array) {                                                    
  43.                                                                                                    
  44.     # check the provided data consistency and other possible issues                                
  45.     self!sanity-check;                                                                              
  46.                                                                                                    
  47.     return @!data[$index].Array;                                                                    
  48. }                                                                                                  
  49.                                                                                                    
  50. multi method get-row (Array :@index! --> Array) {                                                        
  51.                                                                                                    
  52.     # check the provided data consistency and other possible issues                                
  53.     self!sanity-check;                                                                              
  54.                                                                                                    
  55.     return @!data[@index];                                                                          
  56. }
  57.  
  58. ### test.p6 content ###
  59. use v6;                                                                                                
  60. use lib '../lib';                                                                                      
  61. use Data::Dump;                                                                                        
  62. use DataTable;                                                                                        
  63.                                                                                                        
  64. my $dt = DataTable.new(data => [["Tushar", "Dave", 29], ["John", "Adams", 30]],                        
  65.     header => ["Fname", "Lname", "Age"],                                                              
  66. );                                                                                                    
  67.                                                                                                        
  68. my @row = $dt.get-row(index => [0..1]);                                                                
  69. #my @row = $dt.get-row(index => 0);                                                                    
  70.                                                                                                        
  71. @row.say;                                                                                              
  72.                                                                                                        
  73. exit;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement