From 6c8430079cd72aa2b1ddb134349e477dae341f5a Mon Sep 17 00:00:00 2001 From: Federico Ceratto Date: Sat, 4 Apr 2015 00:03:48 +0100 Subject: [PATCH] Updated Nim for Python Programmers (markdown) --- Nim-for-Python-Programmers.md | 185 +--------------------------------- 1 file changed, 1 insertion(+), 184 deletions(-) diff --git a/Nim-for-Python-Programmers.md b/Nim-for-Python-Programmers.md index 24a43a0..23a2da5 100644 --- a/Nim-for-Python-Programmers.md +++ b/Nim-for-Python-Programmers.md @@ -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. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
CNimComment
-
-int x;
-int y = 2;
-
-
-
-var x : int
-var y1 : int = 2
-var y2 = 2
-let z = 2
-
-
Define variable. y2 uses type inference. z is single-assignment. In Nim, uninitialized variables is initialized to 0/nil or similar defaults. -
-
-char s[] = "Hello World.";
-char s0 = s[0]; // 'H'
-char *t = s; // Ptr to s
-s[11] = '!';
-// s, t == "Hello World!"
-
-
-
-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."
-
-
Strings and char. Strings are pass-by-value (copied on assignment) and strictly bounds-checked on access. -
-
-char a = '\n';
-printf("byte %d\nA%cB\n", 
-        a, a);
-
-
-
-let a = '\L'
-echo "byte ", $int(a), 
-     "\nA" & $a & "B"
-
-
Newlines and chars. 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. -
-
-9 % 8  // 1
--9 % 8 // -1
-(unsigned)(-9) % 
-   (unsigned)(8) // 7
-
-
-
-9 mod 8  # 1
--9 mod 8 # -1
--9 %% 8  # 7
-
-
Modulo operator. %% treats its argument as unsigned numbers. See -
-
-int x = foobar() ? 42 : 0;
-
-
-
-var x = if foobar(): 42 
-               else: 0
-
-
-If-statements return the value of the expression they evaluate to, so Nim doesn't need a ternary operator. -
-
-void foo() {
-  printf("Hello World\n");
-}
-int bar() {
-  return 2;
-}
-int baz(int x) {
-  return x*2;
-}
-
-
-
-proc foo() =
-  echo "Hello World"
-
-proc bar(): int =
-  2
-
-proc baz(x: int) =
-  x*2
-
-
Function/Procedure.
-
-int a = 3
-int *b = &a;
-
-
-
-var a = 3
-var b = addr(a)
-
-
Getting an address.
-
-void foobar(person_t *a) {
-  person_t b;
-  b = *a;
-  b.name = "Bob";
-  *a = b;
-}
-
-
-
-proc foobar(a: ref TPerson) =
-  var b: TPerson
-  b = a[]
-  b.name = "Bob"
-  a[] = b
-
-
Dereference. 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.
\ No newline at end of file +Tuples are entirely different from Python \ No newline at end of file