Updated Nim for Python Programmers (markdown)
This commit is contained in:
parent
ed383baeb5
commit
6c8430079c
|
@ -117,187 +117,4 @@ It is possible to implement object-orientation features from other languages (li
|
|||
|
||||
### Structs - Tuples and Objects
|
||||
|
||||
Tuples and Objects in Nim are kind of like structs in C, but not really.
|
||||
|
||||
### Cheat Sheet
|
||||
WARNING: FIXME: this cheat sheet is for C, not Python.
|
||||
|
||||
Note: Code examples are not exactly one-to-one, there may be subtle differences in the semantics. See comments.
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<th>C</th><th>Nim</th><th>Comment</th>
|
||||
</tr>
|
||||
|
||||
|
||||
<tr>
|
||||
<td>
|
||||
<pre>
|
||||
int x;
|
||||
int y = 2;
|
||||
</pre>
|
||||
</td>
|
||||
<td>
|
||||
<pre>
|
||||
var x : int
|
||||
var y1 : int = 2
|
||||
var y2 = 2
|
||||
let z = 2
|
||||
</pre>
|
||||
</td>
|
||||
<td><b>Define variable</b>. y2 uses type inference. z is single-assignment. In Nim, uninitialized variables is initialized to 0/nil or similar defaults.</i>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>
|
||||
<pre>
|
||||
char s[] = "Hello World.";
|
||||
char s0 = s[0]; // 'H'
|
||||
char *t = s; // Ptr to s
|
||||
s[11] = '!';
|
||||
// s, t == "Hello World!"
|
||||
</pre>
|
||||
</td>
|
||||
<td>
|
||||
<pre>
|
||||
var s = "Hello World."
|
||||
var s0: char = s[0] # 'H'
|
||||
var t = s # Copy of s
|
||||
s[11] = '!'
|
||||
# s is "Hello World!"
|
||||
# t is "Hello World."
|
||||
</pre>
|
||||
</td>
|
||||
<td><b>Strings and char</b>. Strings are pass-by-value (copied on assignment) and strictly bounds-checked on access.</i>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>
|
||||
<pre>
|
||||
char a = '\n';
|
||||
printf("byte %d\nA%cB\n",
|
||||
a, a);
|
||||
</pre>
|
||||
</td>
|
||||
<td>
|
||||
<pre>
|
||||
let a = '\L'
|
||||
echo "byte ", $int(a),
|
||||
"\nA" & $a & "B"
|
||||
</pre>
|
||||
</td>
|
||||
<td><b>Newlines and chars</b>. In nim you can't use ``\n`` as a character literal, because on the Windows platform it expands to CR+LR. So you need to specify which char to use.</i>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>
|
||||
<pre>
|
||||
9 % 8 // 1
|
||||
-9 % 8 // -1
|
||||
(unsigned)(-9) %
|
||||
(unsigned)(8) // 7
|
||||
</pre>
|
||||
</td>
|
||||
<td>
|
||||
<pre>
|
||||
9 mod 8 # 1
|
||||
-9 mod 8 # -1
|
||||
-9 %% 8 # 7
|
||||
</pre>
|
||||
</td>
|
||||
<td><b>Modulo operator</b>. <i>%% treats its argument as unsigned numbers. <a href="http://nim-lang.org/manual.html#pre-defined-integer-types">See</a></i></i>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
|
||||
<tr>
|
||||
<td>
|
||||
<pre>
|
||||
int x = foobar() ? 42 : 0;
|
||||
</pre>
|
||||
</td>
|
||||
<td>
|
||||
<pre>
|
||||
var x = if foobar(): 42
|
||||
else: 0
|
||||
</pre>
|
||||
</td>
|
||||
<td>
|
||||
<i>If-statements return the value of the expression they evaluate to, so Nim doesn't need a </i><b>ternary operator</b>.</i>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
|
||||
<tr>
|
||||
<td>
|
||||
<pre>
|
||||
void foo() {
|
||||
printf("Hello World\n");
|
||||
}
|
||||
int bar() {
|
||||
return 2;
|
||||
}
|
||||
int baz(int x) {
|
||||
return x*2;
|
||||
}
|
||||
</pre>
|
||||
</td>
|
||||
<td>
|
||||
<pre>
|
||||
proc foo() =
|
||||
echo "Hello World"
|
||||
|
||||
proc bar(): int =
|
||||
2
|
||||
|
||||
proc baz(x: int) =
|
||||
x*2
|
||||
</pre>
|
||||
</td>
|
||||
<td><b>Function/Procedure.</b> </td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>
|
||||
<pre>
|
||||
int a = 3
|
||||
int *b = &a;
|
||||
</pre>
|
||||
</td>
|
||||
<td>
|
||||
<pre>
|
||||
var a = 3
|
||||
var b = addr(a)
|
||||
</pre>
|
||||
</td>
|
||||
<td><b>Getting an address.</b> <i></i> </td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>
|
||||
<pre>
|
||||
void foobar(person_t *a) {
|
||||
person_t b;
|
||||
b = *a;
|
||||
b.name = "Bob";
|
||||
*a = b;
|
||||
}
|
||||
</pre>
|
||||
</td>
|
||||
<td>
|
||||
<pre>
|
||||
proc foobar(a: ref TPerson) =
|
||||
var b: TPerson
|
||||
b = a[]
|
||||
b.name = "Bob"
|
||||
a[] = b
|
||||
</pre>
|
||||
</td>
|
||||
<td><b>Dereference.</b> <i>In C, only the pointer to the strings in the struct is copied. In Nim, the string is also copied, but refs are not deep-copied.</i> </td>
|
||||
</tr>
|
||||
|
||||
|
||||
</table>
|
||||
Tuples are entirely different from Python
|
Loading…
Reference in New Issue