Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

Is unique buggy?

 [1/8] from: louisaturk::coxinet::net at: 12-Aug-2002 23:44


Hi rebols, Does the unique function contain a bug, or am I doing something wrong?
>>database: unique database
corrupts the data (pointed out with an arrow below). Does unique not work with objects? Louis make object! [ code: "boo" chknum: "5032" date: 11-Jan-2002 amount: $200.00 special: $0.00 ] make object! [ code: "bpo" chknum: "392" date: 7-Mar-2002 amount: $0.00 special: $392.00 ] make object! [ code: "bc" chknum: "" date: 31-May-2002 amount: $100.00 special: $0.00 ] "bow" "bow6603" "bow6481" <======= corruption. make object! [ code: "bow" chknum: "6315" date: 22-Mar-2002 amount: $0.00 special: $98.00 ]

 [2/8] from: greggirwin:mindspring at: 12-Aug-2002 23:59


Hi Louis, << Does unique not work with objects? >> I think not. --Gregg

 [3/8] from: louisaturk:coxinet at: 13-Aug-2002 2:01


Hi Gregg, At 11:59 PM 8/12/2002 -0600, you wrote:
>Hi Louis, > ><< Does unique not work with objects? >> > >I think not.
I just did some testing, and you are right. Disappointing! Well then, how does one make a unique function that will work with objects? Has someone already done this? Louis

 [4/8] from: carl:cybercraft at: 13-Aug-2002 20:05


On 13-Aug-02, Gregg Irwin wrote:
> Hi Louis, > << Does unique not work with objects? >> > I think not.
Well, it sort of does. For instance this...
>> unique reduce [make object! [a: 10] make object! [a: 10]]
== [ make object! [ a: 10 ] make object! [ a: 10 ]] doesn't see those objects as unique, they not being the "same" objects, even if they're "equal" from our point of view. But when they are the same objects...
>> obj: make object! [a: 10] >> blk: reduce [obj obj]
== [ make object! [ a: 10 ] make object! [ a: 10 ]]
>> unique blk
== [ make object! [ a: 10 ]] unique can see they're the same. Either way, I'd say Louis's results are a bug, as it shouldn't be returning a corrupt block like that even if it can't tell if two objects are equal. If it's any help Louis, molding the objects in the block should allow you to extract the unique (as in equal) objects from it. Not a perfect solution, (for instance, the indexes of series wouldn't be preserved when you convert them back to an object, never mind the overhead in converting to a string then back to an object), but it might work for what you're doing. -- Carl Read

 [5/8] from: carl:cybercraft at: 13-Aug-2002 20:47


On 13-Aug-02, Louis A. Turk wrote:
> Hi Gregg, > At 11:59 PM 8/12/2002 -0600, you wrote:
<<quoted lines omitted: 6>>
> then, how does one make a unique function that will work with > objects? Has someone already done this?
Thought a bit more about it Louis, and as third returns an object's block, (or at least simple objects, anyway), using third instead of mold would be a bit more efficient, though you'd still have to convert the blocks back to objects afterwards. Example...
>> obj1: make object! [a: 10 b: "abc"] >> obj2: make object! [a: 10 b: "abc"] >> obj3: make object! [a: 10 b: "abcd"] >> unique reduce [obj1 obj2 obj3]
== [ make object! [ a: 10 b: "abc" ] make object! [ a: 10 b: "abc" ] make obj...
>> unique reduce [third obj1 third obj2 third obj3]
== [[a: 10 b: "abc"] [a: 10 b: "abcd"]] This also has the advantage of retaining the indexes of series...
>> obj2/b: next obj2/b
== "bc"
>> blk: unique reduce [third obj1 third obj2 third obj3]
== [[a: 10 b: "abc"] [a: 10 b: "bc"] [a: 10 b: "abcd"]]
>> blk/2/4
== "bc"
>> head blk/2/4
== "abc" -- Carl Read

 [6/8] from: louisaturk:coxinet at: 13-Aug-2002 7:27


Hi Gregg, At 08:47 PM 8/13/2002 +1200, you wrote:
>Thought a bit more about it Louis, and as third returns an object's >block, (or at least simple objects, anyway), using third instead of
<<quoted lines omitted: 16>>
> >> unique reduce [third obj1 third obj2 third obj3] >== [[a: 10 b: "abc"] [a: 10 b: "abcd"]]
I don't understand how to use third in my program. Below is a script showing what I'm wanting to do: Thanks for the help! Louis PS Seems to me there should be an objects refinement for unique. REBOL [] db-file: %test-data.txt record: context [code: chknum: date: amount: special: none] database: copy [] ; ******************************* FUNCTIONS ********************************** load-data: has [data] [ if exists? db-file [ data: reduce load/all db-file clear database if data [ foreach item data [ item: make record item repend database [join item/code item/chknum item] ;item/key_field item ] ] database: make hash! database ] ] ;************************** Main Program ************************************ ; Goal is to remove duplicates and sort the records by code chknum and date. ;**************************************************************************** load-data print ["Length of database : " length? database] y: sort/all unique reduce database ; <====<<< This doesn't remove dups, doesn't sort, and corrupts the data. print ["Length of processed database: " length? y] ; How do I get the number of records in the database? This doesn't work. probe y ;**************************************************************************** comment {Contents of %test-data.txt (8 records --- 4 of them duplicates **** make object! [ code: "bow" chknum: "130901" date: 13-Sep-2001 amount: $100.00 special: $0.00 ] make object! [ code: "bow" chknum: "200701" date: 20-Jul-2001 amount: $100.00 special: $0.00 ] make object! [ code: "bow" chknum: "110501" date: 11-May-2001 amount: $100.00 special: $0.00 ] make object! [ code: "bow" chknum: "10113" date: 16-Mar-2001 amount: $100.00 special: $0.00 ] make object! [ code: "bow" chknum: "130901" date: 13-Sep-2001 amount: $100.00 special: $0.00 ] make object! [ code: "bow" chknum: "200701" date: 20-Jul-2001 amount: $100.00 special: $0.00 ] make object! [ code: "bow" chknum: "110501" date: 11-May-2001 amount: $100.00 special: $0.00 ] make object! [ code: "bow" chknum: "10113" date: 16-Mar-2001 amount: $100.00 special: $0.00 ] } ;********************************* end *************************************

 [7/8] from: g:santilli:tiscalinet:it at: 13-Aug-2002 15:31


Hi Louis, On Tuesday, August 13, 2002, 2:27:04 PM, you wrote: LAT> PS Seems to me there should be an objects refinement for unique. No, actually you need to use the /SKIP refinement for SORT and UNIQUE, because you don't have just objects, but also string keys in your block, and the order (key, object) needs to be preserved. If keeping the keys unique is what you are after, UNIQUE/SKIP does the job for you perfectly even with objects. HTH, Gabriele. -- Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer Amigan -- AGI L'Aquila -- REB: http://web.tiscali.it/rebol/index.r

 [8/8] from: louisaturk:coxinet at: 13-Aug-2002 14:30


Hi Gabriele, At 03:31 PM 8/13/2002 +0200, you wrote:
>Hi Louis, >On Tuesday, August 13, 2002, 2:27:04 PM, you wrote:
<<quoted lines omitted: 6>>
>HTH, > Gabriele.
Thanks!. I had totally forgotten about the keys. Using skip works. Louis

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted