lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


2010/4/22 Petite Abeille <petite.abeille@gmail.com>:
>
> On Apr 22, 2010, at 12:49 PM, Jerome Vuarand wrote:
>
>> local file = io.open(... or "bigfile.iso", "rb")
>> while true do
>>       local chunk = file:read(16*1024) -- 16kB at a time
>>       if not chunk then break end
>>       evp:update(chunk)
>> end
>
> A bit off topic, but... out of curiosity, I was wondering about the use of 'while true do' + 'if not chunk then break' vs. 'while aChunk do' + 'aChunk = aFile:read()', e.g.:
>
> -- using LHF's previous version of lmd5, the one before it was married to openssl :D
> local sha1 = require( 'sha1' )
> local aHash = sha1.new()
> local aFile = assert( io.open( 'bigfile.iso', 'rb' ) )
> local aChunk = aFile:read( 16384 )
>
> while aChunk do
>    aHash:update( aChunk )
>    aChunk = aFile:read( 16384 )
> end
>
> Any reasons, aside from stylistic ones, to favor one form or another? I personally prefer an explicit conditional when using a 'while' clause, as I find it easier to comprehend, but I have noticed the above style quite often. Just curious :)
>
> All things consider, I would rather use an iterator anyway, but perhaps that's just me:
>
> local aReader = function() return aFile:read( 16384 ) end
>
> for aChunk in aReader do
>    aHash:update( aChunk )
> end

I prefer both syntaxes you present over mine (with the difference that
I wouldn't name the iterator), but I tend to write the one I sent
previously because of a very subtle combination of circumstances that
I understand, despise, but yet repeat helplessly.