Select case VALUE within array, and sets in general

General discussion for topics related to the FreeBASIC project or its community.
Post Reply
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Select case VALUE within array, and sets in general

Post by Tourist Trap »

[edit] I reformulate this a little more clearly.

Many remarks melt here in fact.

About select case for arrays, it would be cool if it was possible to take profit to a notation close to the datas listed in a bracket form used for initialization: dim as single A(2) => { 0.7, 1.4 }. This nice notation is used beneath its potential, since as far as I know the only case is the static array initialization, which is rather unique .. (why not using this to assign an array at any time, anywhere?)

Then, pointers with size have not, always as far I know, their built-in type. Something like below in a built-in version would be useful to run within a pointer taken as an array.

Code: Select all

type INTEGERPOINTERWITHSIZE
      as integer size
      as integer ptr pointerwithsize
end type

I think the continuity of memory address is not a problem if pointer created with NEW[], but I'm not sure - having read this somewhere in the forum but no time to look forward.

If notation with { } brackets for sets were introduced in one hand, and pointers had a built-in counterpart storing the size, and memory continuity ensured for simplicity, then, this below would be something I find nice for style and logics:

Code: Select all

dim as string   A(1 to 10)
dim as zstring ptr B
select case _value as string
case within { chr(1) .. chr(10) } 
       'do something
case within { A(1) .. A(10) }
       'do something
case within { "x", "X", "bb" }
       'do something
case within { B[0] ... B[10] }
       'do something
end select 
As pointed out some of the case statements have their equivalent in term of 'X TO Y' or 'A, B, C' notation. But I think those are shortcut for multiple case lines. Said otherwise, those notations were not exactly made with sets in mind. For proof the simple array initializers brackets have not been used, and arrays are one thing we come to immedialtly when thinking about sets - as well as pointers.

Those things come besides the lack of FOR EACH statement, which also address the problem of running through a set of value, given in an array form, a collection (if we mean here pointers which we know the number of elements), or in my opinion anything given in the mathematical set notation: {a, b, c}, or {a .. z} (when for the last case a .. z is something translatable in an indexed list).

Code: Select all

for each element as single within { A(1) ..  A(10) }
      'process for
next element

(Thanks for reading)
Last edited by Tourist Trap on Dec 04, 2015 15:04, edited 3 times in total.
sancho2
Posts: 547
Joined: May 17, 2015 6:41

Re: Is it a hard deal to get a 'Select case VALUE... within

Post by sancho2 »

Unless I am missing something some of those are already doable:

Code: Select all

ScreenRes 800, 600 , 32
dim as string   A(1 to 10)
dim as zstring ptr B
Dim As String _value

'case within { chr(1) .. chr(10) }
'       'do something
_value = "d"
select case _value
	Case Chr(32) To Chr(110)
		Print "a yes"
	Case Else
		Print "a no"
End Select 

Select Case _value
	Case "a" To "z"
		Print "b yes"
End select

'case within { "x", "X", "bb" }
'       'do something
Select Case _value
	Case "doo", "roo", "soo", "d"
	 Print "c yes"
End Select

'case within { A(1) .. A(10) }
'       'do something
'case within { B[0] ... B[10] }
'       'do something

 sleep
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Is it a hard deal to get a 'Select case VALUE... within

Post by Tourist Trap »

sancho2 wrote:Unless I am missing something some of those are already doable
Yes sancho2, thanks. I've updated 1st post. What I wanted to propose is a usage of the set notation with the { } brackets for I find it best - for instance to translate pseudocode. In general in pseudocode, things are expressed in term of sets, arrays, lists, collections, and synomyms (synonyms of set! we're not in PERL).

In FB, I find that the {1, 2, 3} syntax is not used enough, yet very pretty (in my opinion).

Ok I've tried something that would maybe provide an insight of some treatment for dealing with the array case:

Code: Select all

dim as single A(1 to 10)
dim as single B(1 to 10)
dim as single C(1 to 10)

for i as integer = 1 to 10
    A(i) = 1/(i+1)
    B(i) = 2/(i+1)
    C(i) = 3/(i+1)
next i

select case value as single
case within A
    'code1
    'this is executed if value is found within the values of A
case within { B(5) .. B(8) }
    'code2
    'this is executed if value is found within the values of B
    'in the range specified
    '(and as usual if no case were found to be valid before)
case within { C(5), C(7), C(1) }
    'code3
    'this is executed if value is found within the values of C
    'in the range specified
    '(and as usual if no case were found to be valid before)
'----> equivalent to CASE C(5), C(7), C(1)
end select ' as single value

:case within A:
var checkVariable = TRUE
var lowIndex = lBound(A)
var hiIndex = uBound(A)
for i as integer = lowIndex to hiIndex 
    if value<>A(i) then 
        checkVariable = FALSE
        exit for
    end if
next i
if checkVariable then 
    execute code1
    exit select
end if

:case within { B(1) .. B(10) }:
var checkVariable = TRUE
var lowIndex = 5
var hiIndex = 8
for i as integer = lowIndex to hiIndex 
    if value<>B(i) then 
        checkVariable = FALSE
        exit for
    end if
next i
if checkVariable then 
    execute code2
    exit select
end if
For pointers lists, I don't see what to do while we don't have a LIST built-in keyword, or anything that fits. Or I can think of some udt, but I still don't master this NEW[] affair (for memory continuity, yet this is not necessary if size, backward and forward adddesses are stored).
RockTheSchock
Posts: 252
Joined: Mar 12, 2006 16:25

Re: Select case VALUE within array, and sets in general

Post by RockTheSchock »

Please not another quirky syntax. just to allow UDTs / arrays to be used in select case.

The only thing needed, is to use the right operators. So for arrays you need the following operators

for
next
step
>=
<=
=

If you implement them for udt's you could even use udt's in select cases.

Code: Select all

Dim value As Integer
Dim A(7) As Integer => { 4,2,3,1,5,7,6,8 }

Select case value
	case A()   'needs operators for / next / step / =
		Print "a"
	case A(5) .. A(8) 'needs operators for / next / step / >= / <=
		Print "b"
	case A(5), A(7), A(1) 'needs operator  = (is working already with std datatypes)
		Print "c"
end select
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Select case VALUE within array, and sets in general

Post by Tourist Trap »

RockTheSchock wrote:The only thing needed, is to use the right operators. So for arrays you need the following operators
  • for
    next
    step
    >=
    <=
    =

Code: Select all

Select case value
	case A()   'needs operators for / next / step / =
		Print "a"
	case A(5) .. A(8) 'needs operators for / next / step / >= / <=
		Print "b"
	case A(5), A(7), A(1) 'needs operator  = (is working already with std datatypes)
		Print "c"
end select
Hi RockTheSchock,

I didn't know this. I can see however the relation between operator = and the example above. If I define:

Code: Select all

declare operator =(as single, () as single) as boolean
I can understand that I can force select_case to use this equality. But I don't see at all how to use for...next! I don't even see where select_case needs to do for_next? Can you be more precise about this detail - when you have time?

Other remark, I see 2 difficulties in doing as you suggest, that's a lot of extra work and it's technical - but with good example could be handled... Second issue is that overloading equality for emulation of the inclusion is not too much satisfying, I would prefere an dedicated independant operator (even a text operator, I mean not necessary to have new symbol).

And at last, I still love the set brackets { ... }. But this is affair of taste.

Thanks for the hint anyway. I hope we could have some example, I really don't get the full trick - in particular with for_next overloading.
Drago
Posts: 116
Joined: Aug 10, 2005 13:15

Re: Select case VALUE within array, and sets in general

Post by Drago »

Tourist Trap wrote: And at last, I still love the set brackets { ... }. But this is affair of taste.
Uhhh.... and that is why I use Freebasic.

All the way it shruggs me if y see the nasty " ; " and " { } " within the code

In times I did some UI stuff in dbox and other following DVB-Recievers all the time I got ill if I had to work with C++ and later python :) :)

Please, Please .... to me such stuctures are unreadable.

To iterate through an Array we could just cycle from lowBound to upBound.... Cause it is always done by the computer.... in some languages on a more high level.... in some some deeper ...

Maybe it is because I don't code for the beauty of the code.... but to solve some of my problems.

Greetings
Drago
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Select case VALUE within array, and sets in general

Post by Tourist Trap »

Drago wrote:All the way it shruggs me if y see the nasty " ; " and " { } " within the code
..
Please, Please .... to me such stuctures are unreadable.
Drago, I think you are misinterpreting. I mean only set brackets for .... writting sets. - Not for wrapping code blocks, which, I agree, costs too much keyboard typing , above all if you add to this pain an ";" to terminate each lines.
sancho2
Posts: 547
Joined: May 17, 2015 6:41

Re: Select case VALUE within array, and sets in general

Post by sancho2 »

Hello TouristTrap. Its an interesting idea. Very much like a c# lambda expression within a case statement.
However your psuedocode examples won't work. They set the checkVariable to false and exit the for loop if any of the case statement values don't match the select value. Here is what I think you meant.

Code: Select all

:case within A:
var checkVariable = FALSE
var lowIndex = lBound(A)
var hiIndex = uBound(A)
for i as integer = lowIndex to hiIndex
    if value=A(i) then
        checkVariable = TRUE
        exit for
    end if
next i
if checkVariable then
    execute code1
    exit select
end if

:case within { B(1) .. B(10) }:
var checkVariable = FALSE
var lowIndex = 5
var hiIndex = 8
for i as integer = lowIndex to hiIndex
    if value=B(i) then
        checkVariable = TRUE
        exit for
    end if
next i
if checkVariable then
    execute code2
    exit select
end if
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Select case VALUE within array, and sets in general

Post by Tourist Trap »

sancho2 wrote:Hello TouristTrap. Its an interesting idea. Very much like a c# lambda expression within a case statement.
Thanks for fixing the fake-code.

Finally the primary idea here is maybe to ask for a way to overload "select case" in order to customize its behaviour.

I don't know too much about lambda expression (I've only noticed that Python has implemented this feature very early in its development) but yes it's about making the arguments of "select case" more dynamic, and in particular possibly defined as functions.

A funny trick would also be to be able to do something like:

Code: Select all

select case {A(1) .. A(5)}
   case {B(1) ... B(10)}
      'code
   case else
      'code
end select
This would be used then to test at any time if {A(1) .. A(5)} is included in the set {B(1) .. B(10)}. So more compact any of the classical ways I can think of.

Last thing. As pointed rightly in an above post, select_case lies heavily on operators checks. So why being restricted to =, and the comparators? Something more prone to creativity would be for instance addition of such a syntax:

Code: Select all

select case X
case Y   regarding_to   Z_Operator
   'code
case else
   'code
end select 'X
And to close this list (that some people may find worth to a horror museum), this above maybe leads to think about a blank operator. I mean an operator that would mean nothing in general and that one may use and define if in need of something fully custom, without interferance with classic operators. If there were a symbol to use for that, I would vote for %. Since I've often needed the percentage operator to do its percentage job at least, and hadn't found then a way to overload this symbol - not used at its full potential as many others.

Of course all of that for the present day is theorical ideas. I still really think it would help for coding closer to natural language, not really for efficiency (or assembler would fit better - at the readability cost). However maybe the lambda calculus is the way to do this in the rule of art?
sancho2
Posts: 547
Joined: May 17, 2015 6:41

Re: Select case VALUE within array, and sets in general

Post by sancho2 »

With regards to Lambda expressions: I have no experience with it as a Calculus entity and I have only played with simple ones on C#. If you look at examples of C# Lambda's on the internet, they tend to get quite complex. In reading past posts on this forum, and If I remember correctly, there is no love for them here.
Concerning the case statements targeted to operators: There is a form of that already existing as well.
Consider:

Code: Select all

ScreenRes 800,600, 32

Dim As Integer value
value = 13
Select Case value
	Case Is < 12
		Print "Less than a dozen"
	Case Is > 12
		Print "More than a dozen"
	Case Else
		Print "A dozen"
End Select
Sleep
You could say that the first two case statements target operators other than '='.

In my mind, expanding the capability of a language cannot be a bad thing.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Select case VALUE within array, and sets in general

Post by Tourist Trap »

sancho2 wrote:With regards to Lambda expressions: I have no experience with it as a Calculus entity and I have only played with simple ones on C#. If you look at examples of C# Lambda's on the internet, they tend to get quite complex.
Apparently lambda expression cant be handled natively by some languages, like C. Maybe that's why there's not attempt of doing this in fb?
sancho2 wrote: In my mind, expanding the capability of a language cannot be a bad thing.
For me, if the backward compatibility is maintained, and the expressivity of language increased, that's all good. I mean by expressivity the fact to clarify the code and make it closer to natural language (or calculus practice for this means comfort when translating pseudocode) - and in any case not obfuscating it. This is those qualities that make people love Basic I think.
Post Reply