View this PageEdit this PageUploads to this PageHistory of this PageTop of the SwikiRecent ChangesSearch the SwikiHelp Guide

Pbit / Pfbit

"It is not A;

it is not non-A;

it is both A and non-A;

and it is neither A nor non-A."





Pbit.hqx classes: Pbit/Pbit2/Pfbit/Pfbit2


//returns the bitvalues of the numbers from back to front contained in the list. 
//Julian Rohrhuber 2000/5               
                        
Pbit : ListPattern {
        asStream {
                ^Routine.new({ arg inval;
                        repeats.do({
                        list.do({ arg item;
                                var bit;
                                while({ item > 0}, {
                                        bit = if(item % 2 == 0, { 0 }, { 1 });
                                        item = item div: 2;
                                        bit.embedInStream(inval)
                                })
                                })
                
                        })
                })
        }
} 

___              
-> James McCartney wrote:

You should only use embedInStream if there is possibly an embedded stream.
Since you are turning whatever is in the list into a zero or one then it
can't be a stream. Thus you do not need embedInStream and yield is more
efficient. also the correct usage would have been:

    inval = item.embedInStream(inval);
(The assignment is important because that is where the new event comes in.)


Pbit : ListPattern {
    asStream {
        ^Routine.new({ arg inval;
            repeats.do({
            list.do({ arg item;
                var bit;
                while({ item > 0}, {
                     bit = item & 1;
                     item = item +>> 1;
                     bit.yield;
                 })
                 })
        
            })
        })
    }
}  


//as I wanted to use any input stream, I wrote the following class:
//filters a stram of integers into bit values
//jr 3/01
Pfbit : FilterPattern {
    asStream {
        ^Routine({
                var stream, bit, item;
                stream = pattern.asStream;
                loop({
                        
                        item = stream.next;
                if(item.notNil, {
                        while({ item > 0}, {
                                 bit = item & 1;
                                 item = item +>> 1;
                                 bit.yield;
                        })
                 }, {
                 nil.alwaysYield
                 });
               })
              })
        
         }
}                       


      

                



Links to this Page