From 695159a751966becf726436761e78c33d51c80ba Mon Sep 17 00:00:00 2001 From: JasonJAyalaP Date: Thu, 18 Sep 2014 18:07:51 -0500 Subject: [PATCH 01/17] IC --- nim.html.markdown | 259 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 259 insertions(+) create mode 100644 nim.html.markdown diff --git a/nim.html.markdown b/nim.html.markdown new file mode 100644 index 00000000..25b99e57 --- /dev/null +++ b/nim.html.markdown @@ -0,0 +1,259 @@ +--- +language: nim +filename: learnNim.c +contributors: + - ["Jason J. Ayala P.", "http://JasonAyala.com"] +--- + +Nim is a statically typed, imperative programming language that tries to give +the programmer ultimate power without compromises on runtime efficiency. This +means it focuses on compile-time mechanisms in all their various forms. + +Nim is efficient, expressive, and elegant. + +```nimrod + +var x: int # Declare a variable and its type +x = 1 # Assign it a value +var z = "Yep" # Declare and assign, with or without type annotations + +var # Several, with or without type annotations + letter: char = 'n' # One byte character + name = "Nimrod" # string + truth: bool = false # Common boolean operators: `and` `not` `or` + seconds: int = 42 + thoughts = """ +A great programming language +that everyone can enjoy! +""" # Multiline raw strings + boat: float + +let # Use let to declare and bind an variable *once*. + legs = 400 # legs is immutable. + arms = 2_000 # _ are ignored and are useful for long numbers. + +const # Constants are computed at compile time. This provides + debug = true # performance and is useful in compile time expressions. + aboutPi = 3.15 + compileBadCode = false + +when compileBadCode: # `when` is a compile time `if` + legs = legs + 1 # This error will never be compiled. + const input = readline(stdin) # const values must be known at compile time. + +discard 1 > 2 # The compiler will complain if the result of an expression + # is unused. `discard` bypasses this. + +discard """ +This can work as a +multiline comment +""" + +# +# Common Operations on Basic Types +# + +var nim = "Nimrod is a progamming language" +name = nim[0..5] + +# TODO More common operations? + +# +# Data Structures +# + +# Tuples + +var + child: tuple[name: string, age: int] # Tuples have *both* field names + today: tuple[sun: string, temp: float] # *and* order. + +child = (name: "Rudiger", age: 2) # Assign all at once with literal () +today.sun = "Overcast" # or individual fields. +today.temp = 70.1 + +# Sequences + +var + drinks: seq[string] + +drinks = @["Water", "Juice", "Chocolate"] # @[V1,..,Vn] is the sequence literal + +# +# Defining Your Own Types +# + +# Defining your own types puts the compiler to work for you. It's what makes +# static typing powerful and useful. + +type + Name = string # A type alias gives you a new type that is interchangable + Age = int # with the old type but is more descriptive. + Person = tuple[name: Name, age: Age] # Define data structures too. + +var + john: Person = ("John B.", 17) + newage: int = 18 # It would be better to use Age than int + +john.age = newage # But still works because int and Age are synonyms + +type + Cash = distinct int # `distinct` makes a new type incompatible with it's + Desc = distinct string # base type. + +var + money: Cash = 100.Cash # `.Cash` converts the int to our type + desc: Desc = "Interesting".Desc + +when compileBadCode: + john.age = money # Error! age is of type int and money is Cash + john.name = desc # Compiler says: "No way!" + +# +# More Types and Data Structures +# + +# Enumerations allow a type to be one of a limited number of values + +type + Directions = enum north, west, east, south + Colors = enum red, blue, green +var + orient = north # `orient` is of type Directions, with the value `north` + pixel = green # `pixel` is of type Colors, with the value `green` + +discard north > east # Enums are usually an "ordinal" type + +# Subranges specify a limited valid range + +type + DieFaces = range[1..20] # Only an int from 1 to 20 is a valid value +var + my_roll: DieFaces = 13 + +when compileBadCode: + my_roll = 23 # Error! + +# Arrays + +type + RollCounter = array[DieFaces, int] # Array's are fixed length and + DirNames = array[Directions, string] # indexed by any ordinal type. + Truths = array[42..44, bool] +var + rollCounter: RollCounter + directions: DirNames + truths: Truths + +truths = [false, false, false] # Literal arrays are created with [V1,..,Vn] +truths[42] = true + +directions[north] = "Ahh. The Great White North!" +directions[west] = "No, don't go there." + +my_roll = 13 +rollCounter[my_roll] += 1 +rollCounter[my_roll] += 1 + +var anotherArray = ["Default index", "starts at", "0"] + +# TODO common operations + +# +# IO and Control Flow +# + +# `case`, `readLine()` + +echo "Read any good books lately?" +case readLine(stdin) +of "no", "No": + echo "Go to your local library." +of "yes", "Yes": + echo "Carry on, then." +else: + echo "That's great; I assume." + +# `while`, `if`, `continue`, `break` + +import strutils as str +echo "I'm thinking of a number between 41 and 43. Guess which!" +var + answer: int = 42 + raw_guess: string + guess: int +while guess != answer: + raw_guess = readLine(stdin) + if raw_guess == "": + continue # `continue` restarts loop/block + guess = str.parseInt(raw_guess) + if guess == 1001: + echo("AAAAAAGGG!") + break + elif guess > answer: + echo("Too high.") + elif guess < answer: + echo("Too low") + else: + echo("Yeeeeeehaw!") + +# +# Iteration +# + +# Iterate with the `for` keyword +# TODO `for` examples for strings, arrays, etc + +for elem in ["Yes", "No", "Maybe so"]: + echo elem + +# string iterators + +let myString = """ +an example +string to +play with +""" + +for line in splitLines(myString): + echo(line) + +# +# Procedures +# + +type Answer = enum yes, no + +proc ask(question: string): Answer = + echo(question, " (y/n)") + while true: + case readLine(stdin) + of "y", "Y", "yes", "Yes": + return Answer.yes # Enums can be qualified + of "n", "N", "no", "No": + return Answer.no + else: echo("Please be clear: yes or no") + +proc addSugar(amount: int = 2) = # Default amount is 2, returns nothing + for a in 1..amount: + echo a, " sugar..." + +case ask("Would you like sugar in your tea?") +of yes: + addSugar(3) +of no: + echo "Oh do take a little!" + addSugar() +# No need for an `else` here. only `yes` and `no` are possible. + +``` + +## Further Reading + +* [Home Page](http://nimrod-lang.org) +* [Download](http://nimrod-lang.org/download.html) +* [Community](http://nimrod-lang.org/community.html) +* [FAQ](http://nimrod-lang.org/question.html) +* [Documentation](http://nimrod-lang.org/documentation.html) +* [Manual](http://nimrod-lang.org/manual.html) +* [Standard Libray](http://nimrod-lang.org/lib.html) From f665bf27539b51a24b5dc8a1e8a1fdb7bbb57a52 Mon Sep 17 00:00:00 2001 From: JasonJAyalaP Date: Thu, 18 Sep 2014 18:11:27 -0500 Subject: [PATCH 02/17] filename fix --- nim.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nim.html.markdown b/nim.html.markdown index 25b99e57..f9ba975e 100644 --- a/nim.html.markdown +++ b/nim.html.markdown @@ -1,6 +1,6 @@ --- language: nim -filename: learnNim.c +filename: learnNim.nim contributors: - ["Jason J. Ayala P.", "http://JasonAyala.com"] --- From a261b8c445d0c0af70348d6518d3e0204da94e1d Mon Sep 17 00:00:00 2001 From: JasonJAyalaP Date: Thu, 18 Sep 2014 18:28:42 -0500 Subject: [PATCH 03/17] mention rename --- nim.html.markdown | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/nim.html.markdown b/nim.html.markdown index f9ba975e..ae9f5ce0 100644 --- a/nim.html.markdown +++ b/nim.html.markdown @@ -5,9 +5,10 @@ contributors: - ["Jason J. Ayala P.", "http://JasonAyala.com"] --- -Nim is a statically typed, imperative programming language that tries to give -the programmer ultimate power without compromises on runtime efficiency. This -means it focuses on compile-time mechanisms in all their various forms. +Nim (formally Nimrod) is a statically typed, imperative programming language +that tries to give the programmer ultimate power without compromises on runtime +efficiency. This means it focuses on compile-time mechanisms in all their +various forms. Nim is efficient, expressive, and elegant. From 08a444ddc632603178900484477186a6ae7d9b9e Mon Sep 17 00:00:00 2001 From: JasonJAyalaP Date: Thu, 18 Sep 2014 19:02:33 -0500 Subject: [PATCH 04/17] Renamed vars and types for clarity; minor stuff; enum prefix --- nim.html.markdown | 80 ++++++++++++++++++++++++++--------------------- 1 file changed, 44 insertions(+), 36 deletions(-) diff --git a/nim.html.markdown b/nim.html.markdown index ae9f5ce0..69d0f804 100644 --- a/nim.html.markdown +++ b/nim.html.markdown @@ -18,15 +18,15 @@ var x: int # Declare a variable and its type x = 1 # Assign it a value var z = "Yep" # Declare and assign, with or without type annotations -var # Several, with or without type annotations - letter: char = 'n' # One byte character - name = "Nimrod" # string - truth: bool = false # Common boolean operators: `and` `not` `or` +var # Several, with or without type annotations + letter: char = 'n' # One byte character + lang = "Nimrod" # string + truth: bool = false # Common boolean operators: `and` `not` `or` seconds: int = 42 - thoughts = """ + thoughts = """ A great programming language that everyone can enjoy! -""" # Multiline raw strings +""" # Multiline raw strings boat: float let # Use let to declare and bind an variable *once*. @@ -54,8 +54,9 @@ multiline comment # Common Operations on Basic Types # -var nim = "Nimrod is a progamming language" -name = nim[0..5] +let + phrase = "Nim is a progamming language" + nim = phrase[0..5] # TODO More common operations? @@ -91,6 +92,9 @@ type Name = string # A type alias gives you a new type that is interchangable Age = int # with the old type but is more descriptive. Person = tuple[name: Name, age: Age] # Define data structures too. + LongTuple = tuple + fieldOne: string + secondField: int var john: Person = ("John B.", 17) @@ -104,11 +108,11 @@ type var money: Cash = 100.Cash # `.Cash` converts the int to our type - desc: Desc = "Interesting".Desc + description: Desc = "Interesting".Desc when compileBadCode: - john.age = money # Error! age is of type int and money is Cash - john.name = desc # Compiler says: "No way!" + john.age = money # Error! age is of type int and money is Cash + john.name = description # Compiler says: "No way!" # # More Types and Data Structures @@ -117,13 +121,17 @@ when compileBadCode: # Enumerations allow a type to be one of a limited number of values type - Directions = enum north, west, east, south - Colors = enum red, blue, green + Color = enum cRed, cBlue, cGreen + Direction = enum # Alternative formating + dNorth + dWest + dEast + dSouth var - orient = north # `orient` is of type Directions, with the value `north` - pixel = green # `pixel` is of type Colors, with the value `green` + orient = dNorth # `orient` is of type Direction, with the value `dNorth` + pixel = cGreen # `pixel` is of type Color, with the value `cGreen` -discard north > east # Enums are usually an "ordinal" type +discard dNorth > dEast # Enums are usually an "ordinal" type # Subranges specify a limited valid range @@ -138,23 +146,23 @@ when compileBadCode: # Arrays type - RollCounter = array[DieFaces, int] # Array's are fixed length and - DirNames = array[Directions, string] # indexed by any ordinal type. - Truths = array[42..44, bool] + RollCounter = array[DieFaces, int] # Array's are fixed length and + DirNames = array[Direction, string] # indexed by any ordinal type. + Truths = array[42..44, bool] var - rollCounter: RollCounter + counter: RollCounter directions: DirNames - truths: Truths + possible: Truths -truths = [false, false, false] # Literal arrays are created with [V1,..,Vn] -truths[42] = true +possible = [false, false, false] # Literal arrays are created with [V1,..,Vn] +possible[42] = true -directions[north] = "Ahh. The Great White North!" -directions[west] = "No, don't go there." +directions[dNorth] = "Ahh. The Great White North!" +directions[dWest] = "No, don't go there." my_roll = 13 -rollCounter[my_roll] += 1 -rollCounter[my_roll] += 1 +counter[my_roll] += 1 +counter[my_roll] += 1 var anotherArray = ["Default index", "starts at", "0"] @@ -180,10 +188,10 @@ else: import strutils as str echo "I'm thinking of a number between 41 and 43. Guess which!" var - answer: int = 42 + number: int = 42 raw_guess: string guess: int -while guess != answer: +while guess != number: raw_guess = readLine(stdin) if raw_guess == "": continue # `continue` restarts loop/block @@ -191,9 +199,9 @@ while guess != answer: if guess == 1001: echo("AAAAAAGGG!") break - elif guess > answer: + elif guess > number: echo("Too high.") - elif guess < answer: + elif guess < number: echo("Too low") else: echo("Yeeeeeehaw!") @@ -223,16 +231,16 @@ for line in splitLines(myString): # Procedures # -type Answer = enum yes, no +type Answer = enum aYes, aNo proc ask(question: string): Answer = echo(question, " (y/n)") while true: case readLine(stdin) of "y", "Y", "yes", "Yes": - return Answer.yes # Enums can be qualified + return Answer.aYes # Enums can be qualified of "n", "N", "no", "No": - return Answer.no + return Answer.aNo else: echo("Please be clear: yes or no") proc addSugar(amount: int = 2) = # Default amount is 2, returns nothing @@ -240,9 +248,9 @@ proc addSugar(amount: int = 2) = # Default amount is 2, returns nothing echo a, " sugar..." case ask("Would you like sugar in your tea?") -of yes: +of aYes: addSugar(3) -of no: +of aNo: echo "Oh do take a little!" addSugar() # No need for an `else` here. only `yes` and `no` are possible. From 17e1f7293a2054260519cb39ba2ac743f64d573a Mon Sep 17 00:00:00 2001 From: JasonJAyalaP Date: Fri, 19 Sep 2014 19:55:52 -0500 Subject: [PATCH 05/17] Fixes and updates --- nim.html.markdown | 98 +++++++++++++++++++++++++++-------------------- 1 file changed, 56 insertions(+), 42 deletions(-) diff --git a/nim.html.markdown b/nim.html.markdown index 69d0f804..edbbd92c 100644 --- a/nim.html.markdown +++ b/nim.html.markdown @@ -1,5 +1,5 @@ --- -language: nim +language: Nim filename: learnNim.nim contributors: - ["Jason J. Ayala P.", "http://JasonAyala.com"] @@ -14,20 +14,12 @@ Nim is efficient, expressive, and elegant. ```nimrod -var x: int # Declare a variable and its type -x = 1 # Assign it a value -var z = "Yep" # Declare and assign, with or without type annotations - -var # Several, with or without type annotations - letter: char = 'n' # One byte character - lang = "Nimrod" # string - truth: bool = false # Common boolean operators: `and` `not` `or` - seconds: int = 42 - thoughts = """ -A great programming language -that everyone can enjoy! -""" # Multiline raw strings +var # Declare and assign several variables, + letter: char = 'n' # with or without type annotations + lang = "Nimrod" boat: float + truth: bool = false + seconds: int = 42 let # Use let to declare and bind an variable *once*. legs = 400 # legs is immutable. @@ -46,19 +38,20 @@ discard 1 > 2 # The compiler will complain if the result of an expression # is unused. `discard` bypasses this. discard """ -This can work as a -multiline comment +This can work as a multiline comment. +Or for unparsable, broken code """ # # Common Operations on Basic Types # +# Strings let - phrase = "Nim is a progamming language" + phrase = "Nim is a" nim = phrase[0..5] - -# TODO More common operations? + fullPhrase = phrase & "programming language." + length = len(fullPhrase) # Or: fullPhrase.len # # Data Structures @@ -90,7 +83,7 @@ drinks = @["Water", "Juice", "Chocolate"] # @[V1,..,Vn] is the sequence literal type Name = string # A type alias gives you a new type that is interchangable - Age = int # with the old type but is more descriptive. + Age = int # with the old type but is more descriptive. Person = tuple[name: Name, age: Age] # Define data structures too. LongTuple = tuple fieldOne: string @@ -103,11 +96,11 @@ var john.age = newage # But still works because int and Age are synonyms type - Cash = distinct int # `distinct` makes a new type incompatible with it's + Cash = distinct int # `distinct` makes a new type incompatible with its Desc = distinct string # base type. var - money: Cash = 100.Cash # `.Cash` converts the int to our type + money: Cash = 100.Cash # `.Cash` converts the int to our type description: Desc = "Interesting".Desc when compileBadCode: @@ -121,7 +114,7 @@ when compileBadCode: # Enumerations allow a type to be one of a limited number of values type - Color = enum cRed, cBlue, cGreen + Color = enum cRed, cBlue, cGreen Direction = enum # Alternative formating dNorth dWest @@ -129,7 +122,7 @@ type dSouth var orient = dNorth # `orient` is of type Direction, with the value `dNorth` - pixel = cGreen # `pixel` is of type Color, with the value `cGreen` + pixel = cGreen # `pixel` is of type Color, with the value `cGreen` discard dNorth > dEast # Enums are usually an "ordinal" type @@ -166,7 +159,9 @@ counter[my_roll] += 1 var anotherArray = ["Default index", "starts at", "0"] -# TODO common operations +# More data structures are available, including tables, sets, lists, queues, +# and crit bit trees. +# http://nimrod-lang.org/lib.html#collections-and-algorithms # # IO and Control Flow @@ -183,26 +178,24 @@ of "yes", "Yes": else: echo "That's great; I assume." -# `while`, `if`, `continue`, `break` +# `while`, `if`, `break` -import strutils as str +import strutils as str # http://nimrod-lang.org/strutils.html echo "I'm thinking of a number between 41 and 43. Guess which!" +let number: int = 42 var - number: int = 42 raw_guess: string guess: int while guess != number: raw_guess = readLine(stdin) - if raw_guess == "": - continue # `continue` restarts loop/block guess = str.parseInt(raw_guess) if guess == 1001: echo("AAAAAAGGG!") break elif guess > number: - echo("Too high.") + echo("Nope. Too high.") elif guess < number: - echo("Too low") + echo(guess, " is too low") else: echo("Yeeeeeehaw!") @@ -210,23 +203,25 @@ while guess != number: # Iteration # -# Iterate with the `for` keyword -# TODO `for` examples for strings, arrays, etc +for i, elem in ["Yes", "No", "Maybe so"]: # Or just `for elem in` + echo(elem, " is at index: ", i) -for elem in ["Yes", "No", "Maybe so"]: - echo elem - -# string iterators +for k, v in items(@[(person: "You", power: 100), (person: "Me", power: 9000)]): + echo v let myString = """ -an example -string to +an +`string` to play with -""" +""" # Multiline raw string for line in splitLines(myString): echo(line) +for i, c in myString: # Index and letter. Or `for j in` for just letter + if i mod 2 == 0: continue # Compact `if` form + elif c == 'X': break + else: echo(c) # # Procedures # @@ -244,8 +239,9 @@ proc ask(question: string): Answer = else: echo("Please be clear: yes or no") proc addSugar(amount: int = 2) = # Default amount is 2, returns nothing + assert(amount > 0 or amount < 9000, "Crazy Sugar") for a in 1..amount: - echo a, " sugar..." + echo(a, " sugar...") case ask("Would you like sugar in your tea?") of aYes: @@ -255,8 +251,25 @@ of aNo: addSugar() # No need for an `else` here. only `yes` and `no` are possible. +proc pluralize(a: int): string = + if a > 1 or a == 0: return "s" + else: return "" + +# +# FFI +# + +# Because Nim compiles to C, FFI is easy: + +proc strcmp(a, b: cstring): cint {.importc: "strcmp", nodecl.} + +var cmp = strcmp("C?", "Easy!") + ``` +Additionally, Nim separates itself from its peers with metaprogramming, +performance, and compile-time features. + ## Further Reading * [Home Page](http://nimrod-lang.org) @@ -266,3 +279,4 @@ of aNo: * [Documentation](http://nimrod-lang.org/documentation.html) * [Manual](http://nimrod-lang.org/manual.html) * [Standard Libray](http://nimrod-lang.org/lib.html) +* [Rosetta Code](http://rosettacode.org/wiki/Category:Nimrod) From e0c71555d9f521480c44df2da58334bdacb7cdc9 Mon Sep 17 00:00:00 2001 From: JasonJAyalaP Date: Fri, 19 Sep 2014 19:57:33 -0500 Subject: [PATCH 06/17] shorter desc --- nim.html.markdown | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/nim.html.markdown b/nim.html.markdown index edbbd92c..48a24834 100644 --- a/nim.html.markdown +++ b/nim.html.markdown @@ -7,8 +7,7 @@ contributors: Nim (formally Nimrod) is a statically typed, imperative programming language that tries to give the programmer ultimate power without compromises on runtime -efficiency. This means it focuses on compile-time mechanisms in all their -various forms. +efficiency. Nim is efficient, expressive, and elegant. From 4595c7e3ed55b10b449e817657f3647f51e5a501 Mon Sep 17 00:00:00 2001 From: JasonJAyalaP Date: Fri, 19 Sep 2014 20:00:13 -0500 Subject: [PATCH 07/17] Removed common operations, explained continue --- nim.html.markdown | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/nim.html.markdown b/nim.html.markdown index 48a24834..f22d0365 100644 --- a/nim.html.markdown +++ b/nim.html.markdown @@ -41,17 +41,6 @@ This can work as a multiline comment. Or for unparsable, broken code """ -# -# Common Operations on Basic Types -# - -# Strings -let - phrase = "Nim is a" - nim = phrase[0..5] - fullPhrase = phrase & "programming language." - length = len(fullPhrase) # Or: fullPhrase.len - # # Data Structures # @@ -218,8 +207,8 @@ for line in splitLines(myString): echo(line) for i, c in myString: # Index and letter. Or `for j in` for just letter - if i mod 2 == 0: continue # Compact `if` form - elif c == 'X': break + if i mod 2 == 0: continue # Skip rest of iteration + elif c == 'X': break # Compact `if` form else: echo(c) # # Procedures @@ -259,7 +248,7 @@ proc pluralize(a: int): string = # # Because Nim compiles to C, FFI is easy: - +# proc strcmp(a, b: cstring): cint {.importc: "strcmp", nodecl.} var cmp = strcmp("C?", "Easy!") From 08c18afeb327303bab9473199f43d903d03a2607 Mon Sep 17 00:00:00 2001 From: JasonJAyalaP Date: Fri, 19 Sep 2014 20:02:13 -0500 Subject: [PATCH 08/17] cleanup --- nim.html.markdown | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/nim.html.markdown b/nim.html.markdown index f22d0365..f84b8bbd 100644 --- a/nim.html.markdown +++ b/nim.html.markdown @@ -210,6 +210,7 @@ for i, c in myString: # Index and letter. Or `for j in` for just letter if i mod 2 == 0: continue # Skip rest of iteration elif c == 'X': break # Compact `if` form else: echo(c) + # # Procedures # @@ -237,7 +238,7 @@ of aYes: of aNo: echo "Oh do take a little!" addSugar() -# No need for an `else` here. only `yes` and `no` are possible. +# No need for an `else` here. Only `yes` and `no` are possible. proc pluralize(a: int): string = if a > 1 or a == 0: return "s" @@ -248,7 +249,7 @@ proc pluralize(a: int): string = # # Because Nim compiles to C, FFI is easy: -# + proc strcmp(a, b: cstring): cint {.importc: "strcmp", nodecl.} var cmp = strcmp("C?", "Easy!") From 91e68f583099518b7ab64c323d5f13750eb2b14c Mon Sep 17 00:00:00 2001 From: JasonJAyalaP Date: Fri, 19 Sep 2014 20:04:08 -0500 Subject: [PATCH 09/17] cleanup --- nim.html.markdown | 4 ---- 1 file changed, 4 deletions(-) diff --git a/nim.html.markdown b/nim.html.markdown index f84b8bbd..929d0a74 100644 --- a/nim.html.markdown +++ b/nim.html.markdown @@ -240,10 +240,6 @@ of aNo: addSugar() # No need for an `else` here. Only `yes` and `no` are possible. -proc pluralize(a: int): string = - if a > 1 or a == 0: return "s" - else: return "" - # # FFI # From b87f1acc216ae59ec29c7481008d86de081ffedc Mon Sep 17 00:00:00 2001 From: JasonJAyalaP Date: Fri, 19 Sep 2014 20:14:51 -0500 Subject: [PATCH 10/17] cleanup --- nim.html.markdown | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/nim.html.markdown b/nim.html.markdown index 929d0a74..eca3aa2d 100644 --- a/nim.html.markdown +++ b/nim.html.markdown @@ -12,28 +12,27 @@ efficiency. Nim is efficient, expressive, and elegant. ```nimrod - var # Declare and assign several variables, letter: char = 'n' # with or without type annotations - lang = "Nimrod" + lang = "N" & "im" + n_let : int = len(lang) boat: float truth: bool = false - seconds: int = 42 let # Use let to declare and bind an variable *once*. legs = 400 # legs is immutable. arms = 2_000 # _ are ignored and are useful for long numbers. + aboutPi = 3.15 const # Constants are computed at compile time. This provides debug = true # performance and is useful in compile time expressions. - aboutPi = 3.15 compileBadCode = false when compileBadCode: # `when` is a compile time `if` legs = legs + 1 # This error will never be compiled. - const input = readline(stdin) # const values must be known at compile time. + const input = readline(stdin) # Const values must be known at compile time. -discard 1 > 2 # The compiler will complain if the result of an expression +discard 1 > 2 # Note: The compiler will complain if the result of an expression # is unused. `discard` bypasses this. discard """ @@ -63,7 +62,7 @@ var drinks = @["Water", "Juice", "Chocolate"] # @[V1,..,Vn] is the sequence literal # -# Defining Your Own Types +# Defining Types # # Defining your own types puts the compiler to work for you. It's what makes @@ -73,13 +72,13 @@ type Name = string # A type alias gives you a new type that is interchangable Age = int # with the old type but is more descriptive. Person = tuple[name: Name, age: Age] # Define data structures too. - LongTuple = tuple + AnotherSyntax = tuple fieldOne: string secondField: int var - john: Person = ("John B.", 17) - newage: int = 18 # It would be better to use Age than int + john: Person = (name: "John B.", age: 17) + newage: int = 18 # It would be better to use Age than int john.age = newage # But still works because int and Age are synonyms @@ -166,7 +165,7 @@ of "yes", "Yes": else: echo "That's great; I assume." -# `while`, `if`, `break` +# `while`, `if`, `continue`, `break` import strutils as str # http://nimrod-lang.org/strutils.html echo "I'm thinking of a number between 41 and 43. Guess which!" @@ -176,6 +175,7 @@ var guess: int while guess != number: raw_guess = readLine(stdin) + if raw_guess = "": continue # Skip this iteration guess = str.parseInt(raw_guess) if guess == 1001: echo("AAAAAAGGG!") @@ -207,8 +207,8 @@ for line in splitLines(myString): echo(line) for i, c in myString: # Index and letter. Or `for j in` for just letter - if i mod 2 == 0: continue # Skip rest of iteration - elif c == 'X': break # Compact `if` form + if i mod 2 == 0: continue # Compact `if` form + elif c == 'X': break else: echo(c) # @@ -240,6 +240,10 @@ of aNo: addSugar() # No need for an `else` here. Only `yes` and `no` are possible. +proc pluralize(a: int): string = + if a > 1 or a == 0: return "s" + else: return "" + # # FFI # @@ -249,7 +253,6 @@ of aNo: proc strcmp(a, b: cstring): cint {.importc: "strcmp", nodecl.} var cmp = strcmp("C?", "Easy!") - ``` Additionally, Nim separates itself from its peers with metaprogramming, From 64cd3c4e2245b66546dbc091c1737d9660f7ef94 Mon Sep 17 00:00:00 2001 From: JasonJAyalaP Date: Fri, 19 Sep 2014 20:17:59 -0500 Subject: [PATCH 11/17] cleanup --- nim.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nim.html.markdown b/nim.html.markdown index eca3aa2d..3b826cf4 100644 --- a/nim.html.markdown +++ b/nim.html.markdown @@ -12,14 +12,14 @@ efficiency. Nim is efficient, expressive, and elegant. ```nimrod -var # Declare and assign several variables, +var # Declare (and assign) variables, letter: char = 'n' # with or without type annotations lang = "N" & "im" n_let : int = len(lang) boat: float truth: bool = false -let # Use let to declare and bind an variable *once*. +let # Use let to declare and bind variables *once*. legs = 400 # legs is immutable. arms = 2_000 # _ are ignored and are useful for long numbers. aboutPi = 3.15 From 2caa82052da942c95b8bfa937c7183ac16652718 Mon Sep 17 00:00:00 2001 From: JasonJAyalaP Date: Fri, 19 Sep 2014 20:19:16 -0500 Subject: [PATCH 12/17] cleanup --- nim.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nim.html.markdown b/nim.html.markdown index 3b826cf4..b14f0a7e 100644 --- a/nim.html.markdown +++ b/nim.html.markdown @@ -15,7 +15,7 @@ Nim is efficient, expressive, and elegant. var # Declare (and assign) variables, letter: char = 'n' # with or without type annotations lang = "N" & "im" - n_let : int = len(lang) + n_length : int = len(lang) boat: float truth: bool = false From 24cee5a4e6c2f7f4dd5519bd2ae161a5df21de0a Mon Sep 17 00:00:00 2001 From: JasonJAyalaP Date: Fri, 19 Sep 2014 20:20:08 -0500 Subject: [PATCH 13/17] cleanup --- nim.html.markdown | 4 ---- 1 file changed, 4 deletions(-) diff --git a/nim.html.markdown b/nim.html.markdown index b14f0a7e..cb5f71a2 100644 --- a/nim.html.markdown +++ b/nim.html.markdown @@ -240,10 +240,6 @@ of aNo: addSugar() # No need for an `else` here. Only `yes` and `no` are possible. -proc pluralize(a: int): string = - if a > 1 or a == 0: return "s" - else: return "" - # # FFI # From dce5898d0696fe76940723b85a52c6fe191196fd Mon Sep 17 00:00:00 2001 From: Jason J Ayala P Date: Fri, 19 Sep 2014 20:26:31 -0500 Subject: [PATCH 14/17] cleanup --- nim.html.markdown | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/nim.html.markdown b/nim.html.markdown index cb5f71a2..c878b5b6 100644 --- a/nim.html.markdown +++ b/nim.html.markdown @@ -6,8 +6,7 @@ contributors: --- Nim (formally Nimrod) is a statically typed, imperative programming language -that tries to give the programmer ultimate power without compromises on runtime -efficiency. +that gives the programmer power without compromises on runtime efficiency. Nim is efficient, expressive, and elegant. From 7c450ebc0c10aa7a696045131e41db8c09974dc8 Mon Sep 17 00:00:00 2001 From: Jason J Ayala P Date: Fri, 19 Sep 2014 20:30:11 -0500 Subject: [PATCH 15/17] minor fix --- nim.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nim.html.markdown b/nim.html.markdown index c878b5b6..468e6442 100644 --- a/nim.html.markdown +++ b/nim.html.markdown @@ -174,7 +174,7 @@ var guess: int while guess != number: raw_guess = readLine(stdin) - if raw_guess = "": continue # Skip this iteration + if raw_guess == "": continue # Skip this iteration guess = str.parseInt(raw_guess) if guess == 1001: echo("AAAAAAGGG!") From 86022f3f3fa9a17ae0902855f25578815b6be49d Mon Sep 17 00:00:00 2001 From: Jason J Ayala P Date: Fri, 19 Sep 2014 20:33:21 -0500 Subject: [PATCH 16/17] proof reading --- nim.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nim.html.markdown b/nim.html.markdown index 468e6442..06adba81 100644 --- a/nim.html.markdown +++ b/nim.html.markdown @@ -97,7 +97,7 @@ when compileBadCode: # More Types and Data Structures # -# Enumerations allow a type to be one of a limited number of values +# Enumerations allow a type to have one of a limited number of values type Color = enum cRed, cBlue, cGreen From d7e939ffd712e3b6ae1f45b81b1cc0248a28d00a Mon Sep 17 00:00:00 2001 From: Jason J Ayala P Date: Sat, 20 Sep 2014 07:07:06 -0500 Subject: [PATCH 17/17] minor tweaks --- nim.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nim.html.markdown b/nim.html.markdown index 06adba81..4619975d 100644 --- a/nim.html.markdown +++ b/nim.html.markdown @@ -14,7 +14,7 @@ Nim is efficient, expressive, and elegant. var # Declare (and assign) variables, letter: char = 'n' # with or without type annotations lang = "N" & "im" - n_length : int = len(lang) + nLength : int = len(lang) boat: float truth: bool = false @@ -247,7 +247,7 @@ of aNo: proc strcmp(a, b: cstring): cint {.importc: "strcmp", nodecl.} -var cmp = strcmp("C?", "Easy!") +let cmp = strcmp("C?", "Easy!") ``` Additionally, Nim separates itself from its peers with metaprogramming,