Show
Ignore:
Timestamp:
08/28/08 05:23:09 (4 months ago)
Author:
Chuck.Esterbrook
Message:

Test that the default type for vars, properties and method arguments is dynamic?

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • cobra/trunk/HowTo/290-TranslatePseudoCodeToCobra1.cobra

    r1454 r1596  
    55 
    66Note that were pseudo-code says "i = i+1", the Cobra code uses augmented assignment 
    7 operators such as "i += 1". However, you can still write "i = i+1" if you choose. 
     7operators such as "i += 1". However, you can still write "i = i+1" if you like. 
     8 
     9Note that the default type for an argument is dynamic? which means that the argument 
     10can be any object or nil. 
    811 
    912Authors: Max Grender-Jones, Chuck Esterbrook 
     
    2326                        assert sortMethod([1,1,1,1]) == [1,1,1,1] 
    2427 
    25                 def bubbleSort(list) as dynamic 
     28                def bubbleSort(list) as dynamic? 
    2629                        """ 
    2730                        From http://en.wikipedia.org/wiki/Bubble_sort#Pseudocode_implementation 
     
    4144                                .testSort(ref .bubbleSort) 
    4245                        body 
    43                                 swapped = false 
    44                                 post while swapped 
     46                                if list 
    4547                                        swapped = false 
    46                                         for i in list.count - 1 
    47                                                 if list[i] > list[i+1] 
    48                                                         list.swap(i, i+1) 
    49                                                         swapped = true 
     48                                        post while swapped 
     49                                                swapped = false 
     50                                                for i in list.count - 1 
     51                                                        if list[i] > list[i+1] 
     52                                                                list.swap(i, i+1) 
     53                                                                swapped = true 
    5054                                return list 
    5155 
    52                 def heapSort(list) as dynamic 
     56                def heapSort(list) as dynamic? 
    5357                        """ 
    5458                        From http://en.wikipedia.org/wiki/Heapsort#Pseudocode 
     
    7276                                .testSort(ref .heapSort) 
    7377                        body 
    74                                 .heapify(list) 
    75                                 last = list.count - 1 
    76                                 while last > 0 
    77                                         list.swap(0, last) 
    78                                         last -= 1 
    79                                         .siftDown(list, 0, last) 
     78                                if list 
     79                                        .heapify(list) 
     80                                        last = list.count - 1 
     81                                        while last > 0 
     82                                                list.swap(0, last) 
     83                                                last -= 1 
     84                                                .siftDown(list, 0, last) 
    8085                                return list 
    8186 
    82                 def heapify(list) 
     87                def heapify(list as dynamic) 
    8388                        """ 
    8489                        From http://en.wikipedia.org/wiki/Heapsort#Pseudocode 
     
    129134                                        return 
    130135 
    131                 def insertionSort(list) as dynamic  
     136                def insertionSort(list) as dynamic? 
    132137                        """ 
    133138                        From http://en.wikipedia.org/wiki/Insertion_sort 
     
    144149                                .testSort(ref .insertionSort) 
    145150                        body 
    146                                 for i in 1 : list.count 
    147                                         value = list[i] 
    148                                         j = i - 1 
    149                                         while j >= 0 and list[j] > value 
    150                                                 list[j+1] = list[j] 
    151                                                 j -= 1 
    152                                         list[j+1] = value 
     151                                if list 
     152                                        for i in 1 : list.count 
     153                                                value = list[i] 
     154                                                j = i - 1 
     155                                                while j >= 0 and list[j] > value 
     156                                                        list[j+1] = list[j] 
     157                                                        j -= 1 
     158                                                list[j+1] = value 
    153159                                return list 
    154160 
    155161 
    156                 def quickSort(list) as dynamic 
     162                def quickSort(list) as dynamic? 
    157163                        """ 
    158164                        From http://en.wikipedia.org/wiki/Quicksort 
     
    171177                                .testSort(ref .quickSort) 
    172178                        body 
    173                                 if list.count <= 1 
     179                                if list is nil or list.count <= 1 
    174180                                        return list 
    175181                                else