Wishing well

郭雪松 <[email protected]>
Newsgroups gmane.comp.lang.pike.user
Message-ID <[email protected]>
> this is actually one of the things i miss in pike.

I wish there are multiset/mapping/array compared by values.

I wrote a ByValue.pmod to implement this, but still want a native one, must inherit ByValue.Item is boring.

$ cat ByValue.pmod/module.pmod
#! /bin/env pike
class Array(mixed ... a)
{
        mixed `[](int pos){ return a[pos];}
        mixed `[]=(int pos,mixed val){ return a[pos]=val;}
        int _sizeof()/*{{{*/
        {
                return sizeof(a);
        }/*}}}*/
        int `==(object rhd)/*{{{*/
        {
                if(!objectp(rhd))
                        return 0;
                return equal(a,rhd->a);
        }/*}}}*/
        int `<(object rhd)/*{{{*/
        {
                if(sizeof(a)==sizeof(rhd->a)){
                        if(sizeof(a)==0)
                                return 0;
                        if(a[0]<rhd->a[0])
                                return 1;
                        else if(a[0]==rhd->a[0])
                                return .Array(@a[1..])<.Array(@rhd->a[1..]);
                        else if(a[0]>rhd->a[0])
                                return 0;
                }
        }/*}}}*/
        object `-(object rhd)/*{{{*/
        {
                object res=.Array(@allocate(sizeof(a)));
                for(int i=0;i<sizeof(a)&&i<sizeof(rhd->a);i++){
                        res->a[i]=a[i]-rhd->a[i];
                }
                return res;
        }/*}}}*/
        object `+(object ... args)/*{{{*/
        {
                object res=.Array(@allocate(sizeof(a)));
                for(int i=0;i<sizeof(a);i++)
                        res->a[i]=a[i];
                foreach(args,object rhd){
                        for(int i=0;i<sizeof(res->a)&&i<sizeof(rhd->a);i++){
                                res->a[i]=res->a[i]+rhd->a[i];
                        }
                }
                return res;
        }/*}}}*/
        int __hash()/*{{{*/
        {
                return hash_value(predef::`+(0.0,@map(a,hash_value)));
        }/*}}}*/
        string _sprintf(int t)/*{{{*/
        {
                if(t=='O'){
                        array aa=({});
                        foreach(a,mixed v){
                                aa+=({sprintf("%O",v)});
                        }
                        return "Array("+aa*","+")";
                }
        }/*}}}*/
}
class Pair{
        inherit .Array;
        void create(mixed first,mixed second)/*{{{*/
        {
                ::create(first,second);
        }/*}}}*/
        string _sprintf(int t)/*{{{*/
        {
                if(t=='O'){
                        array aa=({});
                        foreach(a,mixed v){
                                aa+=({sprintf("%O",v)});
                        }
                        return "Pair("+aa*","+")";
                }
        }/*}}}*/
}
class Set{
        inherit .Array;
        void create(mixed ... args)/*{{{*/
        {
                ::create(@sort(args));
        }/*}}}*/
        string _sprintf(int t)/*{{{*/
        {
                if(t=='O'){
                        array aa=({});
                        foreach(a,mixed v){
                                aa+=({sprintf("%O",v)});
                        }
                        return "Set("+aa*","+")";
                }
        }/*}}}*/
}
int sn;
int autoid()
{
        return ++sn;
}
class Item{
        int _byvalue_item_id=.autoid();
        int `<(object rhd)/*{{{*/
        {
                if(Program.inherits(rhd,Item)){
                        return _byvalue_item_id<rhd->_byvalue_item_id;
                }
        }/*}}}*/
        int __hash()/*{{{*/
        {
                return hash_value(_byvalue_item_id);
        }/*}}}*/
        string _sprintf(int t)/*{{{*/
        {
                if(t=='O'){
                        return "ByValue.Item("+_byvalue_item_id+")";
                }
        }/*}}}*/
}



Guo Xue Song


> Date: Sun, 16 Nov 2014 17:58:56 +0100
> From: [email protected]
> To: [email protected]
> CC: [email protected]; [email protected]
> Subject: Re: New "cut" operator (was: Pike 8.0 RC1)
> 
> Excerpts from 郭雪松's message of 2014-11-16 17:38:07 +0100:
> > > string cutfront(string text, string divisor)
> > If pike allows me write code like this:
> > "<some thing>"->cutfront(">")
> > I would think cutfront is ok. object oriented you know, the object was always come out first.
> 
> this is actually one of the things i miss in pike.
> 
> i'd love 
> string_value->function(args ...);
> to be syntax sugar for:
> String.function(string_value, args ...);
> same for other types.
> 
> greetings, martin.
> 
> -- 
> eKita                   -   the online platform for your entire academic life
> -- 
> chief engineer                                                       eKita.co
> pike programmer      pike.lysator.liu.se    caudium.net     societyserver.org
> BLUG secretary                                                 beijinglug.org
> foresight developer  foresightlinux.org                            realss.com
> unix sysadmin
> Martin Bähr          working in china        http://societyserver.org/mbaehr/
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.