Changeset 2335
- Timestamp:
- 03/20/10 04:42:28 (23 months ago)
- Location:
- cobra/trunk/HowTo
- Files:
-
- 11 modified
-
101-WriteBasicSyntax.cobra (modified) (3 diffs)
-
111-UseProperties.cobra (modified) (4 diffs)
-
125-MakeAnIfElseLadder.cobra (modified) (1 diff)
-
130-MakeABranchStatement.cobra (modified) (1 diff)
-
150-DeclareInits.cobra (modified) (4 diffs)
-
180-UseLists.cobra (modified) (3 diffs)
-
186-UseArrays.cobra (modified) (2 diffs)
-
210-UseNilAndNilableTypes.cobra (modified) (1 diff)
-
282-PassReferencesToMethods.cobra (modified) (1 diff)
-
310-IterateThroughRecursiveDataWithYield.cobra (modified) (1 diff)
-
350-MakeACollectionClass.cobra (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
cobra/trunk/HowTo/101-WriteBasicSyntax.cobra
r2127 r2335 52 52 53 53 # If-else statement 54 # See also: "Make An If Else Ladder" How To 54 55 if a > b 55 56 print 'a is greater' … … 57 58 print 'a is not greater' 58 59 59 # Can put target on same iftarget is just one statement60 # Can put target on same line if the target is just one statement 60 61 if a > b, print 'a is greater' 61 62 else, print 'b is greater or equal' … … 64 65 while a > b 65 66 a -= 1 # augmented assignment 67 68 # While loop in one line 66 69 while a > b, a -= 1 70 71 # Parallel assignment 72 a, b = 1, 2 73 74 # Lists -- see the "Use Lists" How To for more info 75 t = ['a', 'b', 'c'] 76 for letter in t, print letter 77 78 # Dictionaries 79 d = {'a': 1, 'b': 2} 80 assert d['a'] == 1 and d['b'] == 2 81 for key, value in d 82 print '[key] = [value]' # string interpolation 83 84 # Sets -- like lists, but no repetition 85 letters = {'a', 'b', 'c'} 86 assert 'a' in letters 87 for letter in letters, print letter 67 88 68 89 # Line continuation is implicit with parenthesized arguments -
cobra/trunk/HowTo/111-UseProperties.cobra
r2127 r2335 44 44 45 45 # Many properties just cover for an underlying variable. When that 46 # is the case, you can use a shortcut syntax es. Later, you can46 # is the case, you can use a shortcut syntax. Later, you can 47 47 # change the property to the full form in order to add logic. 48 48 … … 61 61 62 62 63 # You can even pack an initial value into the declaration 64 # which also be used to infer the data type. 65 66 class Person3 67 68 pro name from var = '' # inferred as String 69 70 pro age from var = 0 # inferred as int 71 72 63 73 # In this shortcut syntax, the vars were already declared so 64 74 # the property simply "... from var": 65 75 66 class Person 376 class Person4 67 77 68 78 var _name as String … … 88 98 # you can specify that name instead of "var": 89 99 90 class Person 4100 class Person5 91 101 92 102 var _name as String … … 101 111 # Properties can be read-only: 102 112 103 class Person 5113 class Person6 104 114 105 115 cue init(name as String) -
cobra/trunk/HowTo/125-MakeAnIfElseLadder.cobra
r2127 r2335 8 8 * Use the `branch` statement (see MakeABranchStatement.cobra) 9 9 10 When using an if-else ladder, consider raising a FallThroughException() at the10 When using an if-else ladder, consider throwing a FallThroughException() at the 11 11 bottom if you expect that it should never happen. 12 12 -
cobra/trunk/HowTo/130-MakeABranchStatement.cobra
r2127 r2335 84 84 assert word == 'number' 85 85 86 # Don't forget that you canbranch on enumerations, strings and characters.86 # You can also branch on enumerations, strings and characters. -
cobra/trunk/HowTo/150-DeclareInits.cobra
r2127 r2335 8 8 9 9 cue init 10 # code10 # statements 11 11 12 12 cue init(ARG as TYPE) 13 # code13 # statements 14 14 15 15 cue init(ARG1 as TYPE1, ARG2 as TYPE2) 16 # code 16 # statements 17 18 Where the first statement is call to another initializer in the same class or 19 the base class: 17 20 18 21 Some key points: … … 20 23 * You can have 0 or more arguments. 21 24 22 * If you declare no initalizers at all for a given class, Cobra will 23 provide one, public, parameterless initializer. 25 * If you declare no initializers at all, Cobra will automatically provide 26 them to match each non-private initializer in the base class. 27 28 * If you declare even one initializer, Cobra will not automatically add any 29 more. This gives you control over how a class must be initialized. 24 30 25 31 * Initializers can be overloaded by the number and type of their arguments. … … 27 33 * Initializers are public by default. 28 34 29 * Initializers can say "base.init(ARGS)" to invoke a base init. 35 * Initializers can say "base.init" or "base.init(ARGS)" to invoke 36 a base initializer. 30 37 31 * Initializers can say ".init" or ".init(ARGS)" to invoke a fellow init. 38 * Initializers can say ".init" or ".init(ARGS)" to invoke 39 a fellow initialier. 32 40 33 41 * Initializers can have their own unit tests just like methods. … … 94 102 get age from var as int 95 103 104 105 class Program 106 96 107 def main 97 108 pass -
cobra/trunk/HowTo/180-UseLists.cobra
r2216 r2335 230 230 assert names.indexOf('foo', 1, 2) == -1 # start at 1, search 2 items 231 231 232 # left off on .insertRange232 # TODO: left off on .insertRange 233 233 234 234 # additional methods. search web for "msdn generic list" … … 269 269 require .count > 0 270 270 return this[.count // 2] 271 271 272 272 273 # a subclass with specific type … … 289 290 return leftMost 290 291 292 291 293 # a generic subclass with a type constraint is even better, 292 294 # because then you can specify a type parameter when using it -
cobra/trunk/HowTo/186-UseArrays.cobra
r2046 r2335 53 53 body 54 54 sum = 0 55 for num in nums 56 sum += num 55 for num in nums, sum += num 57 56 return sum 58 57 … … 61 60 # list or any object that implements IList. 62 61 sum = 0 63 for num in nums 64 sum += num 62 for num in nums, sum += num 65 63 return sum 66 64 -
cobra/trunk/HowTo/210-UseNilAndNilableTypes.cobra
r2127 r2335 92 92 name = if(value and value <> 0, 'value', nil) # type is "String?" 93 93 if name, print name 94 95 # you can coalesce with ? as a binary operator 96 displayName = name ? '(no name)' 97 print displayName -
cobra/trunk/HowTo/282-PassReferencesToMethods.cobra
r2127 r2335 47 47 for cust in customers, print cust 48 48 49 # Yes, there will be lambdas in a future version so 50 # the comparison can be inlined in the sort() call. 49 # you can localize the comparison code with a closure 50 customers.sort(do(a as Customer, b as Customer)) 51 return a.name.toLower.compareTo(b.name.toLower) 52 53 # or a lambda expression 54 customers.sort(do(a as Customer, b as Customer)=a.name.toLower.compareTo(b.name.toLower)) 51 55 52 56 def orderByName(a as Customer, b as Customer) as int is shared -
cobra/trunk/HowTo/310-IterateThroughRecursiveDataWithYield.cobra
r2127 r2335 37 37 def dump 38 38 print this stop 39 if _left 40 print ' left:', _left stop 41 if _right 42 print ' right:', _right stop 39 if _left, print ' left:', _left stop 40 if _right, print ' right:', _right stop 43 41 print 44 42 if _left, _left.dump -
cobra/trunk/HowTo/350-MakeACollectionClass.cobra
r2127 r2335 2 2 MakeACollectionClass.cobra 3 3 4 This HowTo shows the syntax and techniques for declaring an IList. 4 This HowTo shows the syntax and techniques for declaring a class that 5 implements IList<of T>. 5 6 """ 6 7



