www.digitalmars.com         C & C++   DMDScript  

D - opIn

reply "C" <dont respond.com> writes:
Can we get this ? And also add it to arrays ?

char []  x = "a string";

if ( "string" in x ) puts("matched");


C
Feb 18 2004
next sibling parent Mik Mifflin <mik42 NOadelphiaSPAM.net> writes:
C wrote:

 Can we get this ? And also add it to arrays ?
 
 char []  x = "a string";
 
 if ( "string" in x ) puts("matched");
 
 
 C
I think that particular example would be best implemented as a string search function. However, it would make more sense to have something like this: char[] str = "some string"; if ('t' in str) { ... } static int[3] arr = [10, 20, 30]; if (20 in arr) { ... } Though, perhaps that use would be better off as a function as well? Is the reason the in operator exists in associative array because the lookup is cheaper than a brute force iteration/compare? -- - Mik Mifflin
Feb 18 2004
prev sibling next sibling parent reply Manfred Nowak <svv1999 hotmail.com> writes:
C wrote:

 Can we get this ? And also add it to arrays ?
 char []  x = "a string";
 if ( "string" in x ) puts("matched");
What do you have against the regular expression package? And if you want some of the regular expression techniques integrated into the language why not all of them? So long.
Feb 18 2004
parent reply "davepermen" <davepermen hotmail.com> writes:
i never got the regular expression packages to do what i want. they are not
easy to use, the regular expressions..

could you provide a counter-example, showing how this would be done with
re's ? would be possibly very helpful..

"Manfred Nowak" <svv1999 hotmail.com> schrieb im Newsbeitrag
news:c10pkh$ejl$1 digitaldaemon.com...
 C wrote:

 Can we get this ? And also add it to arrays ?
 char []  x = "a string";
 if ( "string" in x ) puts("matched");
What do you have against the regular expression package? And if you want some of the regular expression techniques integrated into the language why not all of them? So long.
Feb 18 2004
parent reply Manfred Nowak <svv1999 hotmail.com> writes:
davepermen wrote:

[...]
 how this would be done with re's ?
[...] if((new RegExp("string","")).test("a string")) puts("matched"); I would like that Walter would have provided some syntactical sugar to omit an empty attributes list. So long.
Feb 19 2004
parent reply "C" <dont respond.com> writes:
Ugh , that was probably the worst example I could have written.  I just mean

if ( item in collection ) {}

Ignore the example ( I would probably just use if ( find( str,"str") != -1 )
for that ).

Charlie

However in my own defenese if opIn was overloadable we could also do

if ( subcollection in largercollection ) also :)  ( not for arrays )


"Manfred Nowak" <svv1999 hotmail.com> wrote in message
news:c12ck7$3e2$1 digitaldaemon.com...
 davepermen wrote:

 [...]
 how this would be done with re's ?
[...] if((new RegExp("string","")).test("a string")) puts("matched"); I would like that Walter would have provided some syntactical sugar to omit an empty attributes list. So long.
Feb 19 2004
parent reply larry cowan <larry_member pathlink.com> writes:
Maybe I'm too old-fashioned, but IMHOFO a lauguage should choose it's primitives
well, support them well, (within the compiler) and leave the other fancies to
libraries.

Sorry, but I don't think "collections" should be internalized to the compiler,
and its easy enough to write a function, a template, or a class to do what you
want.  Write it, tune it the way you want, and use it as needed.

And about subcollections within (or not) collections, overlapping (or not) with
other subcollections, ad infinitum - I think not - have you ever heard of loops?
- or were you thinking that the compiler should read your mind and know that
this collection should be hashed, and in such a way that subcollection1 and
subcollection2 can easily be subsetted, and have everything done in the most
efficient way (presuming that this is not data dependent - or maybe it should
adjust for that dynamically)? 

-larry

In article <c12ng3$nch$1 digitaldaemon.com>, C says...
Ugh , that was probably the worst example I could have written.  I just mean

if ( item in collection ) {}

Ignore the example ( I would probably just use if ( find( str,"str") != -1 )
for that ).

Charlie

However in my own defenese if opIn was overloadable we could also do

if ( subcollection in largercollection ) also :)  ( not for arrays )


"Manfred Nowak" <svv1999 hotmail.com> wrote in message
news:c12ck7$3e2$1 digitaldaemon.com...
 davepermen wrote:

 [...]
 how this would be done with re's ?
[...] if((new RegExp("string","")).test("a string")) puts("matched"); I would like that Walter would have provided some syntactical sugar to omit an empty attributes list. So long.
Feb 19 2004
parent reply "C" <dont respond.com> writes:
I am must be horrible at explaining, or there is a misunderstanding
somewhere.  I want an operator for a class , for your own collections.

struct Deuque(T) {

 T[] collection;

 bit opIn(T t ) { /* enumerate through collection, return true if found*/ }

 }

 bit opIn(T[] t ) { /* return true if all of t is in collection */ }

 }

}


Deuque!(int) d;
/* fill d */
int userInput = 0;
/* get var from user */

if ( userInput in d ) {
  doFoo();
}


Why did u think I was talking about primitives ?  For D at least doesnt an
operator imply a class / struct / template ?

I dont want the compiler to do anything more than translate the 'in'
operator to the corresponding member function.

Charles.


"larry cowan" <larry_member pathlink.com> wrote in message
news:c12p6j$qh8$1 digitaldaemon.com...
 Maybe I'm too old-fashioned, but IMHOFO a lauguage should choose it's
primitives
 well, support them well, (within the compiler) and leave the other fancies
to
 libraries.

 Sorry, but I don't think "collections" should be internalized to the
compiler,
 and its easy enough to write a function, a template, or a class to do what
you
 want.  Write it, tune it the way you want, and use it as needed.

 And about subcollections within (or not) collections, overlapping (or not)
with
 other subcollections, ad infinitum - I think not - have you ever heard of
loops?
 - or were you thinking that the compiler should read your mind and know
that
 this collection should be hashed, and in such a way that subcollection1
and
 subcollection2 can easily be subsetted, and have everything done in the
most
 efficient way (presuming that this is not data dependent - or maybe it
should
 adjust for that dynamically)?

 -larry

 In article <c12ng3$nch$1 digitaldaemon.com>, C says...
Ugh , that was probably the worst example I could have written.  I just
mean
if ( item in collection ) {}

Ignore the example ( I would probably just use if ( find( str,"str")
!= -1 )
for that ).

Charlie

However in my own defenese if opIn was overloadable we could also do

if ( subcollection in largercollection ) also :)  ( not for arrays )


"Manfred Nowak" <svv1999 hotmail.com> wrote in message
news:c12ck7$3e2$1 digitaldaemon.com...
 davepermen wrote:

 [...]
 how this would be done with re's ?
[...] if((new RegExp("string","")).test("a string")) puts("matched"); I would like that Walter would have provided some syntactical sugar to omit an empty attributes list. So long.
Feb 19 2004
parent Manfred Nowak <svv1999 hotmail.com> writes:
C wrote:

[...]
 Why did u think I was talking about primitives ?
[...] Ahh, I've got it. Surely I am with you to be able to overload `in'. So long.
Feb 19 2004
prev sibling next sibling parent reply "Ben Hinkle" <bhinkle4 juno.com> writes:
"C" <dont respond.com> wrote in message news:c10g8o$307o$1 digitaldaemon.com...
| Can we get this ? And also add it to arrays ?
|
| char []  x = "a string";
|
| if ( "string" in x ) puts("matched");

the function string.find(char[] s, char[] sub) will also return the index
of the match (which is common information to want when you have a match).
One nice extension to some of the string functions would be to templatize
them:
 int find(T[] s, T[] sub)

Since it involves templates and containers/arrays that would best
be part of DTL.

-Ben
Feb 18 2004
parent reply "C" <dont respond.com> writes:
Your right that was a bad example.  Supposing we had an array of objects ?
I guess we have foreach which is really smooth , I just really like the look
and ease of 'in'.

C


"Ben Hinkle" <bhinkle4 juno.com> wrote in message
news:c1175t$13l8$1 digitaldaemon.com...
 "C" <dont respond.com> wrote in message
news:c10g8o$307o$1 digitaldaemon.com...
 | Can we get this ? And also add it to arrays ?
 |
 | char []  x = "a string";
 |
 | if ( "string" in x ) puts("matched");

 the function string.find(char[] s, char[] sub) will also return the index
 of the match (which is common information to want when you have a match).
 One nice extension to some of the string functions would be to templatize
 them:
  int find(T[] s, T[] sub)

 Since it involves templates and containers/arrays that would best
 be part of DTL.

 -Ben
Feb 18 2004
parent "Matthew" <matthew.hat stlsoft.dot.org> writes:
I heartily agree on "in". In fact I think the language is broken without it.

But your example was not an example of in, since you were not searching for
an item (char) in your collection (string). I *strongly* disagree on that
use of "in".

But

    char []  x = "a string";

    if ( ' ' in x )
    {
        puts("matched");
    }

is a perfectly reasonable use of "in".


"C" <dont respond.com> wrote in message
news:c118ae$15dr$1 digitaldaemon.com...
 Your right that was a bad example.  Supposing we had an array of objects ?
 I guess we have foreach which is really smooth , I just really like the
look
 and ease of 'in'.

 C


 "Ben Hinkle" <bhinkle4 juno.com> wrote in message
 news:c1175t$13l8$1 digitaldaemon.com...
 "C" <dont respond.com> wrote in message
news:c10g8o$307o$1 digitaldaemon.com...
 | Can we get this ? And also add it to arrays ?
 |
 | char []  x = "a string";
 |
 | if ( "string" in x ) puts("matched");

 the function string.find(char[] s, char[] sub) will also return the
index
 of the match (which is common information to want when you have a
match).
 One nice extension to some of the string functions would be to
templatize
 them:
  int find(T[] s, T[] sub)

 Since it involves templates and containers/arrays that would best
 be part of DTL.

 -Ben
Feb 18 2004
prev sibling next sibling parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
While it was 2/18/04 10:08 PM throughout the UK, C sprinkled little 
black dots on a white screen, and they fell thus:

 Can we get this ? And also add it to arrays ?
 
 char []  x = "a string";
 
 if ( "string" in x ) puts("matched");
That would be semantics bending. The role of 'in' is to determine whether a key exists in an associative array. If it's ever defined on normal arrays, it would have to mean the given index being within the array's bounds. We have '~' for string concatenation, to avoid confusingly overloading '+'. Surely we shouldn't be confusingly overloading 'in' either? Stewart. -- My e-mail is valid but not my primary mailbox, aside from its being the unfortunate victim of intensive mail-bombing at the moment. Please keep replies on the 'group where everyone may benefit.
Feb 19 2004
next sibling parent reply "Matthew" <matthew.hat stlsoft.dot.org> writes:
"Stewart Gordon" <smjg_1998 yahoo.com> wrote in message
news:c12787$2rkt$1 digitaldaemon.com...
 While it was 2/18/04 10:08 PM throughout the UK, C sprinkled little
 black dots on a white screen, and they fell thus:

 Can we get this ? And also add it to arrays ?

 char []  x = "a string";

 if ( "string" in x ) puts("matched");
That would be semantics bending. The role of 'in' is to determine whether a key exists in an associative array. If it's ever defined on normal arrays, it would have to mean the given index being within the array's bounds.
Why so? What's wrong with 'in' meaning just an element exists within a collection? Why must it ape the semantics of the associative arrays?
Feb 19 2004
parent reply Luke D <Luke_member pathlink.com> writes:
In article <c129oq$2vmq$1 digitaldaemon.com>, Matthew says...
"Stewart Gordon" <smjg_1998 yahoo.com> wrote in message
news:c12787$2rkt$1 digitaldaemon.com...
 While it was 2/18/04 10:08 PM throughout the UK, C sprinkled little
 black dots on a white screen, and they fell thus:

 Can we get this ? And also add it to arrays ?

 char []  x = "a string";

 if ( "string" in x ) puts("matched");
That would be semantics bending. The role of 'in' is to determine whether a key exists in an associative array. If it's ever defined on normal arrays, it would have to mean the given index being within the array's bounds.
Why so? What's wrong with 'in' meaning just an element exists within a collection? Why must it ape the semantics of the associative arrays?
The use of in would be entirely ambiguous though. If you'd use in on an associative array, right now it'll return a bool if the key exists in the array. If it would be extended to also say whether a value exists in a collection/array, what would it mean if the operator was used on an associative array? IMO, the in operator should be overloadable to make classes that mimic associative arrays, and if an operator is really needed to check whether a value exists in an array, it would need to be a new operator (though... it might be a bit confusing since when people see in, they'll think it means to check for value if they haven't used that part of the language before...)
Feb 19 2004
parent "Matthew" <matthew.hat stlsoft.dot.org> writes:
"Luke D" <Luke_member pathlink.com> wrote in message
news:c13bf6$1s53$1 digitaldaemon.com...
 In article <c129oq$2vmq$1 digitaldaemon.com>, Matthew says...
"Stewart Gordon" <smjg_1998 yahoo.com> wrote in message
news:c12787$2rkt$1 digitaldaemon.com...
 While it was 2/18/04 10:08 PM throughout the UK, C sprinkled little
 black dots on a white screen, and they fell thus:

 Can we get this ? And also add it to arrays ?

 char []  x = "a string";

 if ( "string" in x ) puts("matched");
That would be semantics bending. The role of 'in' is to determine whether a key exists in an associative array. If it's ever defined on normal arrays, it would have to mean the given index being within the array's bounds.
Why so? What's wrong with 'in' meaning just an element exists within a collection? Why must it ape the semantics of the associative arrays?
The use of in would be entirely ambiguous though. If you'd use in on an associative array, right now it'll return a bool if the key exists in the
array.
 If it would be extended to also say whether a value exists in a
 collection/array, what would it mean if the operator was used on an
associative
 array?
The same thing as it does now. I don't see the problem personally, but then I've always looked at maps/assoc-arrays as having the keys being the important thing. Nonetheless, I understand your point. It needs further thought. How do other languages deal with this?
 IMO, the in operator should be overloadable to make classes that mimic
 associative arrays, and if an operator is really needed to check whether a
value
 exists in an array, it would need to be a new operator (though... it might
be a
 bit confusing since when people see in, they'll think it means to check
for
 value if they haven't used that part of the language before...)
Feb 19 2004
prev sibling parent "Ivan Senji" <ivan.senji public.srce.hr> writes:
"Stewart Gordon" <smjg_1998 yahoo.com> wrote in message
news:c12787$2rkt$1 digitaldaemon.com...
 While it was 2/18/04 10:08 PM throughout the UK, C sprinkled little
 black dots on a white screen, and they fell thus:

 Can we get this ? And also add it to arrays ?

 char []  x = "a string";

 if ( "string" in x ) puts("matched");
That would be semantics bending. The role of 'in' is to determine whether a key exists in an associative array. If it's ever defined on normal arrays, it would have to mean the given index being within the array's bounds.
i don't agree I often wan't to write something like this: int[] numbers; int x; if(x in numbers) { //do something } but then i remember that there is no in for arrays and have to do it like this foreach(int i; numbers) { if(i == x) { //do something break; } } Which is a lot more typing when the types are more complex than int[]
 We have '~' for string concatenation, to avoid confusingly overloading
 '+'.  Surely we shouldn't be confusingly overloading 'in' either?

 Stewart.

 --
 My e-mail is valid but not my primary mailbox, aside from its being the
 unfortunate victim of intensive mail-bombing at the moment.  Please keep
 replies on the 'group where everyone may benefit.
Feb 19 2004
prev sibling parent Vathix <vathix dprogramming.com> writes:
C wrote:

 Can we get this ? And also add it to arrays ?
 
 char []  x = "a string";
 
 if ( "string" in x ) puts("matched");
 
 
 C
I would like it to return a boolean if a particular "sub item" is within the object. If it's either a wchar in a int[wchar], a char in a char[], or a FooItem in a Foo (custom)... but I don't really think finding "string" in "a string" is good. -- Christopher E. Miller www.dprogramming.com irc.dprogramming.com #D
Feb 19 2004