mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 17:41:41 +00:00
Fixes and updates
This commit is contained in:
parent
08a444ddc6
commit
17e1f7293a
@ -1,5 +1,5 @@
|
|||||||
---
|
---
|
||||||
language: nim
|
language: Nim
|
||||||
filename: learnNim.nim
|
filename: learnNim.nim
|
||||||
contributors:
|
contributors:
|
||||||
- ["Jason J. Ayala P.", "http://JasonAyala.com"]
|
- ["Jason J. Ayala P.", "http://JasonAyala.com"]
|
||||||
@ -14,20 +14,12 @@ Nim is efficient, expressive, and elegant.
|
|||||||
|
|
||||||
```nimrod
|
```nimrod
|
||||||
|
|
||||||
var x: int # Declare a variable and its type
|
var # Declare and assign several variables,
|
||||||
x = 1 # Assign it a value
|
letter: char = 'n' # with or without type annotations
|
||||||
var z = "Yep" # Declare and assign, with or without type annotations
|
lang = "Nimrod"
|
||||||
|
|
||||||
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
|
|
||||||
boat: float
|
boat: float
|
||||||
|
truth: bool = false
|
||||||
|
seconds: int = 42
|
||||||
|
|
||||||
let # Use let to declare and bind an variable *once*.
|
let # Use let to declare and bind an variable *once*.
|
||||||
legs = 400 # legs is immutable.
|
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.
|
# is unused. `discard` bypasses this.
|
||||||
|
|
||||||
discard """
|
discard """
|
||||||
This can work as a
|
This can work as a multiline comment.
|
||||||
multiline comment
|
Or for unparsable, broken code
|
||||||
"""
|
"""
|
||||||
|
|
||||||
#
|
#
|
||||||
# Common Operations on Basic Types
|
# Common Operations on Basic Types
|
||||||
#
|
#
|
||||||
|
|
||||||
|
# Strings
|
||||||
let
|
let
|
||||||
phrase = "Nim is a progamming language"
|
phrase = "Nim is a"
|
||||||
nim = phrase[0..5]
|
nim = phrase[0..5]
|
||||||
|
fullPhrase = phrase & "programming language."
|
||||||
# TODO More common operations?
|
length = len(fullPhrase) # Or: fullPhrase.len
|
||||||
|
|
||||||
#
|
#
|
||||||
# Data Structures
|
# Data Structures
|
||||||
@ -103,7 +96,7 @@ var
|
|||||||
john.age = newage # But still works because int and Age are synonyms
|
john.age = newage # But still works because int and Age are synonyms
|
||||||
|
|
||||||
type
|
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.
|
Desc = distinct string # base type.
|
||||||
|
|
||||||
var
|
var
|
||||||
@ -166,7 +159,9 @@ counter[my_roll] += 1
|
|||||||
|
|
||||||
var anotherArray = ["Default index", "starts at", "0"]
|
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
|
# IO and Control Flow
|
||||||
@ -183,26 +178,24 @@ of "yes", "Yes":
|
|||||||
else:
|
else:
|
||||||
echo "That's great; I assume."
|
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!"
|
echo "I'm thinking of a number between 41 and 43. Guess which!"
|
||||||
|
let number: int = 42
|
||||||
var
|
var
|
||||||
number: int = 42
|
|
||||||
raw_guess: string
|
raw_guess: string
|
||||||
guess: int
|
guess: int
|
||||||
while guess != number:
|
while guess != number:
|
||||||
raw_guess = readLine(stdin)
|
raw_guess = readLine(stdin)
|
||||||
if raw_guess == "":
|
|
||||||
continue # `continue` restarts loop/block
|
|
||||||
guess = str.parseInt(raw_guess)
|
guess = str.parseInt(raw_guess)
|
||||||
if guess == 1001:
|
if guess == 1001:
|
||||||
echo("AAAAAAGGG!")
|
echo("AAAAAAGGG!")
|
||||||
break
|
break
|
||||||
elif guess > number:
|
elif guess > number:
|
||||||
echo("Too high.")
|
echo("Nope. Too high.")
|
||||||
elif guess < number:
|
elif guess < number:
|
||||||
echo("Too low")
|
echo(guess, " is too low")
|
||||||
else:
|
else:
|
||||||
echo("Yeeeeeehaw!")
|
echo("Yeeeeeehaw!")
|
||||||
|
|
||||||
@ -210,23 +203,25 @@ while guess != number:
|
|||||||
# Iteration
|
# Iteration
|
||||||
#
|
#
|
||||||
|
|
||||||
# Iterate with the `for` keyword
|
for i, elem in ["Yes", "No", "Maybe so"]: # Or just `for elem in`
|
||||||
# TODO `for` examples for strings, arrays, etc
|
echo(elem, " is at index: ", i)
|
||||||
|
|
||||||
for elem in ["Yes", "No", "Maybe so"]:
|
for k, v in items(@[(person: "You", power: 100), (person: "Me", power: 9000)]):
|
||||||
echo elem
|
echo v
|
||||||
|
|
||||||
# string iterators
|
|
||||||
|
|
||||||
let myString = """
|
let myString = """
|
||||||
an example
|
an <example>
|
||||||
string to
|
`string` to
|
||||||
play with
|
play with
|
||||||
"""
|
""" # Multiline raw string
|
||||||
|
|
||||||
for line in splitLines(myString):
|
for line in splitLines(myString):
|
||||||
echo(line)
|
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
|
# Procedures
|
||||||
#
|
#
|
||||||
@ -244,8 +239,9 @@ proc ask(question: string): Answer =
|
|||||||
else: echo("Please be clear: yes or no")
|
else: echo("Please be clear: yes or no")
|
||||||
|
|
||||||
proc addSugar(amount: int = 2) = # Default amount is 2, returns nothing
|
proc addSugar(amount: int = 2) = # Default amount is 2, returns nothing
|
||||||
|
assert(amount > 0 or amount < 9000, "Crazy Sugar")
|
||||||
for a in 1..amount:
|
for a in 1..amount:
|
||||||
echo a, " sugar..."
|
echo(a, " sugar...")
|
||||||
|
|
||||||
case ask("Would you like sugar in your tea?")
|
case ask("Would you like sugar in your tea?")
|
||||||
of aYes:
|
of aYes:
|
||||||
@ -255,8 +251,25 @@ of aNo:
|
|||||||
addSugar()
|
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"
|
||||||
|
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
|
## Further Reading
|
||||||
|
|
||||||
* [Home Page](http://nimrod-lang.org)
|
* [Home Page](http://nimrod-lang.org)
|
||||||
@ -266,3 +279,4 @@ of aNo:
|
|||||||
* [Documentation](http://nimrod-lang.org/documentation.html)
|
* [Documentation](http://nimrod-lang.org/documentation.html)
|
||||||
* [Manual](http://nimrod-lang.org/manual.html)
|
* [Manual](http://nimrod-lang.org/manual.html)
|
||||||
* [Standard Libray](http://nimrod-lang.org/lib.html)
|
* [Standard Libray](http://nimrod-lang.org/lib.html)
|
||||||
|
* [Rosetta Code](http://rosettacode.org/wiki/Category:Nimrod)
|
||||||
|
Loading…
Reference in New Issue
Block a user