Skip Menu |
 

This queue is for tickets about the DBD-SQLite CPAN distribution.

Report information
The Basics
Id: 47178
Status: resolved
Priority: 0/
Queue: DBD-SQLite

People
Owner: Nobody in particular
Requestors: gfuji [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: (no value)
Fixed in: (no value)



Subject: Sample programs in the cookbock is not good examples
Download (untitled) / with headers
text/plain 677b
Hello ADAMK and others, DBD::SQLite::Cookbook does not seem to be good examples. SQLite aggrigates are designed to be object-oriented, but these examples use no benefits of OOP. Take the highly-scalable version of variance() for example. It uses some global variabes ($mu, $count, $S), so it can be called only once in a SQL statement. Why do you use object properties, instead of global variables? The attached file "Variance.pm" is a new example, which implements simple variance (Variance::Simple) and highly-scalable variance (Variance::Fast). I think it is a little better. "variance.pl" is a demo file for "Variance.pm". Regards, -- Goro Fuji (gfx) GFUJI at CPAN.org
Subject: Variance.pm
Download Variance.pm
text/x-perl 927b
package Variance::Simple; use strict; use warnings; sub new { my($class) = @_; return bless { sum => 0, data => [], } => $class; } sub step { my($self, $value) = @_; $self->{sum} += $value; push @{$self->{data}}, $value; return; } sub finalize { my($self) = @_; my $n = scalar @{$self->{data}}; my $mu = $self->{sum} / $n; my $s = 0; foreach my $datum(@{$self->{data}}){ $s += ($datum - $mu) ** 2; } return $s / ($n-1); } package Variance::Fast; sub new { my($class) = @_; return bless { n => 0, sum => 0, mu => 0, s => 0, } => $class; } sub step { my($self, $value) = @_; $self->{n}++; my $delta = $value - $self->{mu}; $self->{mu} += $delta / $self->{n}; $self->{S} += $delta * ($value - $self->{mu}); return; } sub finalize { my($self) = @_; return $self->{S} / ($self->{n}-1); } 1;
Subject: variance.pl
Download variance.pl
text/x-perl 716b
#!perl -w use strict; use DBI; use Data::Dumper; use FindBin qw($Bin); use lib $Bin; use Variance; my $dbh = DBI->connect('dbi:SQLite:dbname=:memory:', undef, undef, { PrintError => 0, RaiseError => 1}); $dbh->do(<<'SQL'); CREATE TABLE foo( value INTEGER NOT NULL ); SQL { my $sth = $dbh->prepare('INSERT INTO foo VALUES(?)'); foreach my $value(10, 12, 11, 10, 8, 9, 10, 11, 9){ $sth->execute($value); } $sth->finish(); } foreach my $vclass (qw(Variance::Simple Variance::Fast)){ $dbh->sqlite_create_aggregate(variance => 1, $vclass); my $sth = $dbh->prepare('SELECT variance(value) FROM foo'); $sth->execute(); print Data::Dumper->new([$sth->fetchrow_hashref()], [$vclass])->Indent(1)->Dump; }
Subject: Re: [rt.cpan.org #47178] Sample programs in the cookbock is not good examples
Date: Sun, 21 Jun 2009 16:28:04 +1000
To: bug-DBD-SQLite [...] rt.cpan.org
From: Adam Kennedy <adamkennedybackup [...] gmail.com>
Download (untitled) / with headers
text/plain 1.3k
If you are interested in helping to rewrite the cookbook entries, I can get you commit rights to the repository, so that you can edit, improve and add to it directly. Would you prefer this? Adam K 2009/6/20 Goro Fuji via RT <bug-DBD-SQLite@rt.cpan.org> Show quoted text
> Sat Jun 20 02:57:22 2009: Request 47178 was acted upon. > Transaction: Ticket created by GFUJI > Queue: DBD-SQLite > Subject: Sample programs in the cookbock is not good examples > Broken in: (no value) > Severity: Normal > Owner: Nobody > Requestors: gfuji@cpan.org > Status: new > Ticket <URL: https://rt.cpan.org/Ticket/Display.html?id=47178 > > > > Hello ADAMK and others, > > DBD::SQLite::Cookbook does not seem to be good examples. SQLite > aggrigates are designed to be object-oriented, but these examples use no > benefits of OOP. > > Take the highly-scalable version of variance() for example. It uses some > global variabes ($mu, $count, $S), so it can be called only once in a > SQL statement. Why do you use object properties, instead of global > variables? > > The attached file "Variance.pm" is a new example, which implements > simple variance (Variance::Simple) and highly-scalable variance > (Variance::Fast). I think it is a little better. "variance.pl" is a demo > file for "Variance.pm". > > Regards, > -- > Goro Fuji (gfx) GFUJI at CPAN.org >
Download (untitled) / with headers
text/plain 865b
On Sat Jun 20 15:57:22 2009, GFUJI wrote: Show quoted text
> Hello ADAMK and others, > > DBD::SQLite::Cookbook does not seem to be good examples. SQLite > aggrigates are designed to be object-oriented, but these examples use no > benefits of OOP. > > Take the highly-scalable version of variance() for example. It uses some > global variabes ($mu, $count, $S), so it can be called only once in a > SQL statement. Why do you use object properties, instead of global > variables? > > The attached file "Variance.pm" is a new example, which implements > simple variance (Variance::Simple) and highly-scalable variance > (Variance::Fast). I think it is a little better. "variance.pl" is a demo > file for "Variance.pm". > > Regards,
Closing as https://github.com/DBD-SQLite/DBD-SQLite/commit/d30f3ab45934a55493de71703da39fecc3ab0438 had fixed the major part of this ticket. Thanks.


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

Please report any issues with rt.cpan.org to rt-cpan-admin@bestpractical.com.