Re: Should to_number return a sequence?

new topic     » goto parent     » topic index » view thread      » older message » newer message
petelomax said...

I'd suggest:

function parse_num(sequence s) 
    atom b = is_number(s), v = 0 
    if b then 
        v = to_number(s) 
    end if 
    return {b, v} 
end function 

OTOH, I'm not at all sure how exactly that is supposed to beat just calling is/to inline directly.

Thanks for the code suggestion. Your version is clearer than what I wrote, for sure. I especially like the single exit point of the function. My version skipped 1 additional variable allocation, 2 assignment operations, and one variable lookup, but probably wasn't worth the additional clarity your version provides.

I don't think it's a matter of one approach beats another. In fact, I know now exactly how to get the answer as to whether or not a string is a number or not. I know how to do it Euphoria or Phix. I know how to do it cross dialect if I wanted to. Honestly, to everyone reading this who actually worked on the to_number function, thank you. It works and does what I need.

And, if you're interested in my personal UX, this is my 2 cents, FWIW. Functions that vary in return types seem confusing. That's not to say varied return types are invalid patterns. Just that it will require the end user to spend more time in the documentation learning about the cool features (and nuances) Euphoria/Phix has to offer detracting from the time spent using the cool features. Not a bad thing. Simply a matter of fact.

What follows is no ones fault but my own. But I'm going to describe my UX when looking up how to convert a string to a number. First, I was programming in Phix. So, I looked there first for docs but didn't any function that did what I wanted. Maybe it exists. I didn't find it. So, I hopped over to OpenEuphoria and found to_number. I thought to myself, that seems like it will work but, what if the string is not a number? So I started skimming the docs. What I read sort of went like this. Words, words, words. Blah, blah, blah. More words. Ah, there it is. -1 returns this data in a sequence, something else the sequence has blah blah. Ok, got it. It returns a sequence. Cool! And moved on. Just being honest. That's what happened.

So, basically, the fact that it worked as I expected in Phix was just pure luck. And when I tried to use the function in Euphoria I was surprised, but validated by my Phix experience. Thankfully, I had written tests that caught the problem. Anyway, I wrote a quick is_number function to get past the moment and started this thread.

My take-a-way is slow down reading the docs. Assume nothing. I can only hope this conversation had value to you, the readers. To ensure that I've contributed in some meaningful way, I created 2 wiki pages. You gave me your time and expertise. I tried to give back.

Also, FWIW. Euphoria has been my favorite language for a long time. Sadly, I haven't used it much over the years. Professionally, the languages I needed most were (in order) PHP, Javascript, C#, Java, C, Perl, Ruby, Cpp, VB Script, VB, and Python. For fun, I program in Euphoria, now Phix (which I really like), and Pascal. I've stopped programming in BASIC. But I've tried many of those including QBasic, BlitzBASIC, DarkBASIC (under 2K lines), PureBASIC (under 2k lines), RapidQ (loved this one), FreeBASIC (also great), and LibertyBASIC. In most languages I just mentioned I wrote at least 2000 lines of code. Some of the languages 100000 lines or more. Perhaps 300K+ in one of them. The reason I mention my experience is because I want you to know when I say I think Euhporia/Phix could possibly be the best language out there, the opinion is coming from a place of authority. I've tried a bunch.

Thank you.

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu