Misc fixes

This commit is contained in:
flaviut 2014-07-18 16:15:17 -04:00
commit a5fdbc898e
9 changed files with 62 additions and 29 deletions

View file

@ -30,7 +30,7 @@ Alternate methods of indexing arrays are also allowed, the first type parameter
type
PartsOfSpeech {.pure.} = enum
Pronoun, Verb, Article, Adjective, Noun, Adverb
let partOfSpeechExamples: array[PartsOfSpeech] = [
let partOfSpeechExamples: array[PartsOfSpeech, string] = [
"he", "reads", "the", "green", "book", "slowly"
]
```

View file

@ -18,7 +18,7 @@ block outer:
let b = 3
block:
let b = "3" # Probably a dumb idea
let b = "3" # shadowing is probably a dumb idea
```
Parentheses can be used as an expression, but they do not provide end of statement inference, so it is necessary to place semicolons yourself. An interesting and unexpected side effect of this syntax is that Nimrod is suitable even for brace purists!

View file

@ -11,23 +11,23 @@ Nimrod also supports case statements, which are like switches in other languages
* It is required that every possible case be covered
``` nimrod
case "charlie"
of "alfa":
echo "A"
of "bravo":
echo "B"
of "charlie":
echo "C"
else:
echo "Unrecognized letter"
case "charlie":
of "alfa":
echo "A"
of "bravo":
echo "B"
of "charlie":
echo "C"
else:
echo "Unrecognized letter"
case 'h':
of 'a', 'e', 'i', 'o', 'u':
echo "Vowel"
of '\127'..'\255':
echo "Unknown"
else:
echo "Consonant"
of 'a', 'e', 'i', 'o', 'u':
echo "Vowel"
of '\127'..'\255':
echo "Unknown"
else:
echo "Consonant"
proc positiveOrNegative(num: int): string =
result = case num:
@ -54,11 +54,11 @@ Another feature of case statements is that the possible values are narrowed, mea
``` nimrod
let x = 2
case (x and 3) + 7
of 7: echo "A"
of 8: echo "B"
of 9: echo "C"
of 10: echo "D"
case (x and 3) + 7:
of 7: echo "A"
of 8: echo "B"
of 9: echo "C"
of 10: echo "D"
```
``` console
$ nimrod c -r ./case_stmts_1.nim

View file

@ -88,7 +88,7 @@ Closure iterators hold on to their state and can be resumed at any time. The `fi
``` nimrod
proc countTo(n: int): iterator(): int =
return iterator (): int =
return iterator(): int =
var i = 0
while i <= n:
yield i
@ -100,7 +100,7 @@ echo countTo20()
var output = ""
while not finished(countTo20):
output.add($countTo20())
output.add($countTo20() & " ")
echo output
output = ""

View file

@ -10,7 +10,7 @@ When inside a loop, `continue` can be used to skip the rest of the loop body and
Along with [its other uses](/block/), the `block` statement can be used to create a label so that it's possible to break out of nested loops.
``` nimrod
<!--skip-->``` nimrod
import strutils, math
randomize()

View file

@ -10,7 +10,7 @@ title: Main
Getting started? Head over to the [installation instructions](/getting_started/)!
``` nimrod
<!--skip-->``` nimrod
import tables, strutils
var wordFrequencies = newCountTable[string]()

View file

@ -17,7 +17,7 @@ Integers can also have `0[xX]`, `0o`, `0[Bb]` prepended to indicate a hex, octal
``` nimrod
let
a: int8 = 0x7F # Works
b: uint8 = 0x1111_1111 # Works
b: uint8 = 0b1111_1111 # Works
d = 0xFF # type is int
c: uint8 = 256 # Compile time error
```

View file

@ -6,7 +6,7 @@ title: Seqs
Seqs provide dynamically expandable storage, they are extended as needed.
There are two ways to create seqs, with the `@` operator and with the `newSeq[T](n: int)` method. Once a seq is created, it can be modified using the `add(item: T)`, `remove(idx: int)`. The length of a seq can be found through `len: int`, and the maximum index through `high: int`. The standard `items: T` and `pairs: tuple[i: int, v: T]` iterators are also available.
There are two ways to create seqs, with the `@` operator and with the `newSeq[T](n: int)` method. Once a seq is created, it can be modified using the `add(item: T)`, `delete(idx: int)`. The length of a seq can be found through `len: int`, and the maximum index through `high: int`. The standard `items: T` and `pairs: tuple[i: int, v: T]` iterators are also available.
``` nimrod
var
@ -19,7 +19,7 @@ for i, v in a:
for i in 4..100:
b.add(i * i)
b.remove(0) # takes «O(n)» time
b.delete(0) # takes «O(n)» time
b = a[0] & b # Same as original b
```