[vwnc] FizzBuzz: What's wrong with the 3rd one?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

[vwnc] FizzBuzz: What's wrong with the 3rd one?

Peter Suk

Just for amusement, I implemented FizzBuzz.  For some reason, the 3rd one is off by one.  Why? 

 

Procedural Smalltalk:

1 to: 100 do: [ :i | | msg |
    msg := ''.
    (i rem: 3) = 0  ifTrue: [msg := msg , 'Fizz'].
    (i rem: 5) = 0 ifTrue: [msg := msg , 'Buzz'].
    msg isEmpty ifTrue: [msg := i displayString].
    Transcript show: msg; cr.
].

 

Functional-ish Smalltalk:

1 to: 100 do: [ :i |
    t := [  (i rem: 3) = 0 ifTrue: ['Fizz'] ifFalse: [''] ].
    f := [ (i rem: 5) = 0 ifTrue: ['Buzz'] ifFalse: [''] ].
    n := [ ( t value, f value ) isEmpty ifTrue: [ i displayString ] ifFalse: [''] ].
    Transcript show: t value, f value, n value; cr.
].

 

Ye-olde Array Indexing Hack with block contexts:

1 to: 100 do: [ :i | 
Transcript show: 
    ((#('' '' Fizz '' Buzz Fizz '' '' Fizz Buzz '' Fizz '' '' FizzBuzz) copy 
        replaceAll: '' with: [ i displayString ]) 
            at: (i rem: 15) + 1) value; cr.
].

 

I thought it might be a VW bug, but it works the same way in Squeak. 


_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: [vwnc] FizzBuzz: What's wrong with the 3rd one?

Peter Suk
1 to: 100 do: [ :i | 
Transcript show: 
    ((#(FizzBuzz '' '' Fizz '' Buzz Fizz '' '' Fizz Buzz '' Fizz '' '') copy 
        replaceAll: '' with: [ i displayString ]) 
            at: (i rem: 15) + 1) value; cr.
].

 

 

Solved it!  Sometimes zero indexing might be nice!

 


From: Peter Suk
Sent: Tuesday, February 26, 2008 4:58 PM
To: vwnc-list
Subject: FizzBuzz: What's wrong with the 3rd one?

 

Just for amusement, I implemented FizzBuzz.  For some reason, the 3rd one is off by one.  Why? 

 

Procedural Smalltalk:

1 to: 100 do: [ :i | | msg |
    msg := ''.
    (i rem: 3) = 0  ifTrue: [msg := msg , 'Fizz'].
    (i rem: 5) = 0 ifTrue: [msg := msg , 'Buzz'].
    msg isEmpty ifTrue: [msg := i displayString].
    Transcript show: msg; cr.
].

 

Functional-ish Smalltalk:

1 to: 100 do: [ :i |
    t := [  (i rem: 3) = 0 ifTrue: ['Fizz'] ifFalse: [''] ].
    f := [ (i rem: 5) = 0 ifTrue: ['Buzz'] ifFalse: [''] ].
    n := [ ( t value, f value ) isEmpty ifTrue: [ i displayString ] ifFalse: [''] ].
    Transcript show: t value, f value, n value; cr.
].

 

Ye-olde Array Indexing Hack with block contexts:

1 to: 100 do: [ :i | 
Transcript show: 
    ((#('' '' Fizz '' Buzz Fizz '' '' Fizz Buzz '' Fizz '' '' FizzBuzz) copy 
        replaceAll: '' with: [ i displayString ]) 
            at: (i rem: 15) + 1) value; cr.
].

 

I thought it might be a VW bug, but it works the same way in Squeak. 


_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

[vwnc] Questions about SQLite3ThreadedConnection

Carl Gundel
I'm not quite clear on the use of the SQLite3ThreadedConnection class.
Currently I'm using its superclass (aptly named SQLite3Connection) in my Run
BASIC appserver.  I'm guessing that the threaded one is more appropriate for
multiuser web apps, or do I misunderstand what this class is for?

Should I be able to just substitute one for the other?  Is there anything I
need to be careful about?  I hope someone here has some experience with it.
:-)

Thanks,

-Carl Gundel
Easy Windows programming - http://www.libertybasic.com
Easy web programming - http://www.runbasic.com

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: [vwnc] Questions about SQLite3ThreadedConnection

Boris Popov, DeepCove Labs (SNN)
Re: [vwnc] Questions about SQLite3ThreadedConnection

If its anything like ODBC threaded connection, then the benefit is that queries in one connection don't block the vm and hence other activity in the image. Internally each connection is protected with a semaphore for thread safety, so most of the time you can just substitute one for another. There is significant performance penalty involved however, so beware, but depending on your use cases it may not matter or matter very little.

Cheers!

-Boris (via BlackBerry)

----- Original Message -----
From: [hidden email] <[hidden email]>
To: vwnc-list <[hidden email]>
Sent: Sun Aug 24 18:49:31 2008
Subject: [vwnc] Questions about SQLite3ThreadedConnection

I'm not quite clear on the use of the SQLite3ThreadedConnection class.
Currently I'm using its superclass (aptly named SQLite3Connection) in my Run
BASIC appserver.  I'm guessing that the threaded one is more appropriate for
multiuser web apps, or do I misunderstand what this class is for?

Should I be able to just substitute one for the other?  Is there anything I
need to be careful about?  I hope someone here has some experience with it.
:-)

Thanks,

-Carl Gundel
Easy Windows programming - http://www.libertybasic.com
Easy web programming - http://www.runbasic.com

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc


_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: [vwnc] Questions about SQLite3ThreadedConnection

Carl Gundel
Performance may be an issue perhaps, but my interest in this class is that
it may prevent locking errors.  Is it designed for that, I wonder?

Thanks,

-Carl Gundel
Easy Windows programming - http://www.libertybasic.com
Easy web programming - http://www.runbasic.com
----- Original Message -----
From: "Boris Popov" <[hidden email]>
To: <[hidden email]>; <[hidden email]>
Sent: Sunday, August 24, 2008 10:07 PM
Subject: Re: [vwnc] Questions about SQLite3ThreadedConnection


> If its anything like ODBC threaded connection, then the benefit is that
> queries in one connection don't block the vm and hence other activity in
> the image. Internally each connection is protected with a semaphore for
> thread safety, so most of the time you can just substitute one for
> another. There is significant performance penalty involved however, so
> beware, but depending on your use cases it may not matter or matter very
> little.
>
> Cheers!
>
> -Boris (via BlackBerry)
>
> ----- Original Message -----
> From: [hidden email] <[hidden email]>
> To: vwnc-list <[hidden email]>
> Sent: Sun Aug 24 18:49:31 2008
> Subject: [vwnc] Questions about SQLite3ThreadedConnection
>
> I'm not quite clear on the use of the SQLite3ThreadedConnection class.
> Currently I'm using its superclass (aptly named SQLite3Connection) in my
> Run
> BASIC appserver.  I'm guessing that the threaded one is more appropriate
> for
> multiuser web apps, or do I misunderstand what this class is for?
>
> Should I be able to just substitute one for the other?  Is there anything
> I
> need to be careful about?  I hope someone here has some experience with
> it.
> :-)
>
> Thanks,
>
> -Carl Gundel
> Easy Windows programming - http://www.libertybasic.com
> Easy web programming - http://www.runbasic.com
>
> _______________________________________________
> vwnc mailing list
> [hidden email]
> http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
>

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: [vwnc] Questions about SQLite3ThreadedConnection

Chris Winemiller
In reply to this post by Carl Gundel
I just completed a test application using SQLite, both threaded and
unthreaded.  Performance issues aside, the threaded version does not
work well when, within a single image, you have one Smalltalk Process
(and hence OS thread) writing frequently to the database while a
separate Smalltalk Process (and hence OS thread) tries to read
extensively from the database at the same time.  Database errors
occurred from which the threads could not recover.  There were no OE
crashes, though.

It appears that the difference between SQLite3ThreadedConnection and
SQLite3Connection is that the non-threaded version ends up calling a
bunch of external library methods while the threaded version calls the
same methods but use the _threaded "keyword" in their invocations.

Chris

Carl Gundel wrote:

> I'm not quite clear on the use of the SQLite3ThreadedConnection class.
> Currently I'm using its superclass (aptly named SQLite3Connection) in my Run
> BASIC appserver.  I'm guessing that the threaded one is more appropriate for
> multiuser web apps, or do I misunderstand what this class is for?
>
> Should I be able to just substitute one for the other?  Is there anything I
> need to be careful about?  I hope someone here has some experience with it.
> :-)
>
> Thanks,
>
> -Carl Gundel

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: [vwnc] Questions about SQLite3ThreadedConnection

david.long
Hi Chris,
is this a possibility with PostgreSQL also? I've been noticing slow performance in the database there also,
What's your recommended approach?

thanks,
David
On Mon, 2008-08-25 at 07:53 -0500, Chris Winemiller wrote:
I just completed a test application using SQLite, both threaded and 
unthreaded.  Performance issues aside, the threaded version does not 
work well when, within a single image, you have one Smalltalk Process 
(and hence OS thread) writing frequently to the database while a 
separate Smalltalk Process (and hence OS thread) tries to read 
extensively from the database at the same time.  Database errors 
occurred from which the threads could not recover.  There were no OE 
crashes, though.

It appears that the difference between SQLite3ThreadedConnection and 
SQLite3Connection is that the non-threaded version ends up calling a 
bunch of external library methods while the threaded version calls the 
same methods but use the _threaded "keyword" in their invocations.

Chris

Carl Gundel wrote:
> I'm not quite clear on the use of the SQLite3ThreadedConnection class. 
> Currently I'm using its superclass (aptly named SQLite3Connection) in my Run 
> BASIC appserver.  I'm guessing that the threaded one is more appropriate for 
> multiuser web apps, or do I misunderstand what this class is for?
>
> Should I be able to just substitute one for the other?  Is there anything I 
> need to be careful about?  I hope someone here has some experience with it. 
> :-)
>
> Thanks,
>
> -Carl Gundel

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Your business, your way.
http://sageteagroup.com

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: [vwnc] Questions about SQLite3ThreadedConnection

Chris Winemiller
david wrote:
Hi Chris,
is this a possibility with PostgreSQL also? I've been noticing slow performance in the database there also,
What's your recommended approach?
I doubt that PostgreSQL has the same problem because its access technique is entirely different.  With PostgreSQL, the database has a "server" OS process to which clients connect over a TCP/IP socket. So, the image has a socket connection to the PostgreSQL database.  And VisualWorks sockets seem very well behaved.  We use PostgreSQL as our Store database and we've never had problems like the one I tried to describe below.

Regarding PostgreSQL performance: I am no database expert.  However, I do know that PostgreSQL has a "vacuum/analyze" command that you should send to the library periodically.  This command is supposed to tune the database performance.  PostgreSQL's documentation might have a better explanation.

Regards,
Chris

On Mon, 2008-08-25 at 07:53 -0500, Chris Winemiller wrote:
I just completed a test application using SQLite, both threaded and 
unthreaded.  Performance issues aside, the threaded version does not 
work well when, within a single image, you have one Smalltalk Process 
(and hence OS thread) writing frequently to the database while a 
separate Smalltalk Process (and hence OS thread) tries to read 
extensively from the database at the same time.  Database errors 
occurred from which the threads could not recover.  There were no OE 
crashes, though.

It appears that the difference between SQLite3ThreadedConnection and 
SQLite3Connection is that the non-threaded version ends up calling a 
bunch of external library methods while the threaded version calls the 
same methods but use the _threaded "keyword" in their invocations.

Chris

Carl Gundel wrote:
> I'm not quite clear on the use of the SQLite3ThreadedConnection class. 
> Currently I'm using its superclass (aptly named SQLite3Connection) in my Run 
> BASIC appserver.  I'm guessing that the threaded one is more appropriate for 
> multiuser web apps, or do I misunderstand what this class is for?
>
> Should I be able to just substitute one for the other?  Is there anything I 
> need to be careful about?  I hope someone here has some experience with it. 
> :-)
>
> Thanks,
>
> -Carl Gundel


_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: [vwnc] Questions about SQLite3ThreadedConnection

Boris Popov, DeepCove Labs (SNN)
In reply to this post by Carl Gundel

PostgreSQL interface does not use DLLCC so there's nothing to add __threaded to.

Cheers!

-Boris (via BlackBerry)


From: [hidden email]
To: Chris Winemiller
Cc: vwnc-list
Sent: Mon Aug 25 06:16:17 2008
Subject: Re: [vwnc] Questions about SQLite3ThreadedConnection

Hi Chris,
is this a possibility with PostgreSQL also? I've been noticing slow performance in the database there also,
What's your recommended approach?

thanks,
David
On Mon, 2008-08-25 at 07:53 -0500, Chris Winemiller wrote:
I just completed a test application using SQLite, both threaded and 
unthreaded.  Performance issues aside, the threaded version does not 
work well when, within a single image, you have one Smalltalk Process 
(and hence OS thread) writing frequently to the database while a 
separate Smalltalk Process (and hence OS thread) tries to read 
extensively from the database at the same time.  Database errors 
occurred from which the threads could not recover.  There were no OE 
crashes, though.

It appears that the difference between SQLite3ThreadedConnection and 
SQLite3Connection is that the non-threaded version ends up calling a 
bunch of external library methods while the threaded version calls the 
same methods but use the _threaded "keyword" in their invocations.

Chris

Carl Gundel wrote:
> I'm not quite clear on the use of the SQLite3ThreadedConnection class. 
> Currently I'm using its superclass (aptly named SQLite3Connection) in my Run 
> BASIC appserver.  I'm guessing that the threaded one is more appropriate for 
> multiuser web apps, or do I misunderstand what this class is for?
>
> Should I be able to just substitute one for the other?  Is there anything I 
> need to be careful about?  I hope someone here has some experience with it. 
> :-)
>
> Thanks,
>
> -Carl Gundel

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Your business, your way.
http://sageteagroup.com

_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc
Reply | Threaded
Open this post in threaded view
|

Re: [vwnc] Questions about SQLite3ThreadedConnection

Boris Popov, DeepCove Labs (SNN)
In reply to this post by Carl Gundel

All recent versions of PostgreSQL come with autovacuum daemon, just make sure its enabled in your configuration.

Cheers!

-Boris (via BlackBerry)


From: [hidden email]
To: vwnc-list
Sent: Mon Aug 25 06:47:57 2008
Subject: Re: [vwnc] Questions about SQLite3ThreadedConnection

david wrote:
Hi Chris,
is this a possibility with PostgreSQL also? I've been noticing slow performance in the database there also,
What's your recommended approach?
I doubt that PostgreSQL has the same problem because its access technique is entirely different.  With PostgreSQL, the database has a "server" OS process to which clients connect over a TCP/IP socket. So, the image has a socket connection to the PostgreSQL database.  And VisualWorks sockets seem very well behaved.  We use PostgreSQL as our Store database and we've never had problems like the one I tried to describe below.

Regarding PostgreSQL performance: I am no database expert.  However, I do know that PostgreSQL has a "vacuum/analyze" command that you should send to the library periodically.  This command is supposed to tune the database performance.  PostgreSQL's documentation might have a better explanation.

Regards,
Chris

On Mon, 2008-08-25 at 07:53 -0500, Chris Winemiller wrote:
I just completed a test application using SQLite, both threaded and 
unthreaded.  Performance issues aside, the threaded version does not 
work well when, within a single image, you have one Smalltalk Process 
(and hence OS thread) writing frequently to the database while a 
separate Smalltalk Process (and hence OS thread) tries to read 
extensively from the database at the same time.  Database errors 
occurred from which the threads could not recover.  There were no OE 
crashes, though.

It appears that the difference between SQLite3ThreadedConnection and 
SQLite3Connection is that the non-threaded version ends up calling a 
bunch of external library methods while the threaded version calls the 
same methods but use the _threaded "keyword" in their invocations.

Chris

Carl Gundel wrote:
> I'm not quite clear on the use of the SQLite3ThreadedConnection class. 
> Currently I'm using its superclass (aptly named SQLite3Connection) in my Run 
> BASIC appserver.  I'm guessing that the threaded one is more appropriate for 
> multiuser web apps, or do I misunderstand what this class is for?
>
> Should I be able to just substitute one for the other?  Is there anything I 
> need to be careful about?  I hope someone here has some experience with it. 
> :-)
>
> Thanks,
>
> -Carl Gundel


_______________________________________________
vwnc mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/vwnc