Forums

Sample demo code: Write number in plain english

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Re: Sample demo code: Write number in plain english

Postby hopscc » Mon Jan 12, 2009 4:56 am

JonathonDavid,

you can get enumerate in your own code in the interim by adding in the code given in ticket:126

ticket:129 is about iterating on a keyValuePair not about iterating on a dict....
iterating on a dict does work in the (fm src tree) version (0.8.0 post release)

Csaba,
did you try it? (modulo getting the syntax for a dict literal right)
:o
Code: Select all
 for bindingInt, bindingWord in { 1_000_000_000 : ' billion', 1_000_000 : ' million', 1000 : ' thousand', 100: ' hundred'}
                        print bindingInt, bindingWord

# gives
Code: Select all
> cobc0 csa.cobra
1000000000  billion
1000000  million
1000  thousand
100  hundred


Theres an example in the test cases - multi-arg-forDict.cobra

Code: Select all
 
             xdict = {0:'z', 1:'w', 3:'x'}
                for k,v in xdict
                        print 'k=',k, 'v=',v
                        assert k in [0,1,3]
                        assert v in ['z', 'w', 'x']


The doc/wiki unfortunately needs a lot of updating...
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: Sample demo code: Write number in plain english

Postby Charles » Mon Jan 12, 2009 5:04 am

I don't think you are guaranteed the order of the key/value pairs to be the same as they appear in the literal. Therefore, if your algorithm depends on getting the larger values first, you should use a list instead. Or you could get the keys and reverse sort them, but I don't think that's providing any advantage.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Sample demo code: Write number in plain english

Postby hopscc » Mon Jan 12, 2009 6:15 am

Should have also mentioned (jonathonDavid items 3 and 4 from a while back)

cobra does have 'case' except its called branch
case items.count
when 0, return ' '
when 1, return items[0]

is done as
Code: Select all
branch items.count
    on 0, return ' '
    on 1, return items[0]


The item 4 code about use of join can also be done except that it relies on the .Net join method on Arrays/Collections /something(?)
or on the Cobra rtl version on Strings..
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: Sample demo code: Write number in plain english

Postby Csaba » Mon Jan 12, 2009 6:36 am

Hi,
Well, hmm, I tried with [..] not with (...).... and suprise, it didn't work. Thank you!
Regards
Csaba
Csaba
 
Posts: 42

Re: Sample demo code: Write number in plain english

Postby jonathandavid » Mon Jan 12, 2009 8:45 am

@hopscc: thanks for the feedback, I had completely forgotten about the branch statement, and I wasn't .NET strings had their own version of join. However, String.join takes a String[], so it's not a useful as Python's counterpart (I think to be as useful it should take a IEnumerable<Object> and perform the conversion of each item to String under the hoods).

@Chuck: you're right, a dictionary is not a viable option in this case, the original order must be preserved. Apologies to Csaba for the confusion I might have created. Chuck, would it be possible to use a list of tuples? I'm thinking about the following:

numAndWord =  [ (1_000_000_000 ,  ' billion'),
(1_000_000 , ' million'),
( 1000 , ' thousand'),
( 100, ' hundred' ) ]

for num,word in numAndWord
# this preserves our order!!
jonathandavid
 
Posts: 159

Re: Sample demo code: Write number in plain english

Postby Csaba » Mon Jan 12, 2009 1:21 pm

Hi,

Well, that to rewrite teh Ruby version. Yes, this is possible and works in Ruby:

Code: Select all
 
      # Go through the number for binding numbers/words and take care of the left part recursively
      for (bindingNumber, bindingWord) in  [ [1000000000 , ' billion'], [1000000, ' million'] , [1000,' thousand'] , [100, ' hundred']] do
         # Find the left part to the binding number (like " hundred")
         left_part_of_integer = integer_to_translate / bindingNumber
         # Find the part of the integer to the right of the binding number (like " hundred") for further analyses
         integer_to_translate =  integer_to_translate  - left_part_of_integer * bindingNumber  # mod is nicer
         if left_part_of_integer> 0  # Recursion to build up the left part of the number
            integer_as_text +=  left_part_of_integer.in_english + bindingWord
            if integer_to_translate > 0  # Just to make it neat
               integer_as_text +=  ' '# So we don't write as millionone....
            end # if
         end #if
      end # for
 


Similar would be nice in Cobra, it but maybe have to wait until post v1 - This use is not that common
(Ruby IS bloated, I think there are too many posibilities in Ruby. Although also many nice possibilities.)

Regards
Csaba
Csaba
 
Posts: 42

Re: Sample demo code: Write number in plain english

Postby Charles » Mon Jan 12, 2009 2:18 pm

A nested list of nested lists has worked for a long time in Cobra. The "for a, b ..." has worked for awhile now thanks to hopscc.
class P

def main is shared
for bindingNumber, bindingWord in [ [1000000000 , ' billion'], [1000000, ' million'] , [1000,' thousand'] , [100, ' hundred']]
print bindingNumber, bindingWord

Although the compiler infers the types as Object when they should either be "dynamic" or ("int", "String").
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Sample demo code: Write number in plain english

Postby jonathandavid » Tue Jan 13, 2009 9:46 am

Chuck wrote:Although the compiler infers the types as Object when they should either be "dynamic" or ("int", "String").


That's a major drawback. Any plans to add tuple support in the future? I know they're not in C#, but they shouldn't be hard to implement. Hereare some C# implementations. There is a similar Java implementation, and in D the support is native.

It would be nice to have a generic Tuple<of X,Y,Z> class, and the syntactic sugar necessary so that (1,"A", foo) creates an instance of that class. Of course "x,y,z = tuple" should bind the variables appropriately, so that we can iterate through a list L of tuples by writing "for x,y,z in L". We could also be allowed to write "for x in tuple", and have x bound to each element in turn (the type of x should be the GCD of all the tuple types, Object in the less favorable case). I know many people claim that tuples are evil, but in many cases (as in Csaba's NumberToEnglish function) they lead to the most elegant code.
jonathandavid
 
Posts: 159

Re: Sample demo code: Write number in plain english

Postby Charles » Fri Jan 16, 2009 3:20 am

I haven't decided yet, but I think there is some real possibility. My impression is that like generics, typed tuples are practically necessary to get the full benefits of static typing. But this is a lower priority than stabilizing the current feature set and some other things. For now, I have created a ticket to track this.

Thanks for the thought and the links.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Sample demo code: Write number in plain english

Postby Csaba » Sat Feb 21, 2009 7:49 am

Hi,

In my opinion. Chucks example:

for bindingNumber, bindingWord in [ [1000000000 , ' billion'], [1000000, ' million'] , [1000,' thousand'] , [100, ' hundred']] print bindingNumber, bindingWord

or something similar should be a "for" example in http://cobra-language.com/docs/manual/c ... e-for.html.

Regards
Csaba
Csaba
 
Posts: 42

PreviousNext

Return to Discussion

Who is online

Users browsing this forum: No registered users and 8 guests