Skip to content

Instantly share code, notes, and snippets.

@holyshared
Last active August 8, 2017 05:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save holyshared/642672c78550b40c03d673e3ce9d3a04 to your computer and use it in GitHub Desktop.
Save holyshared/642672c78550b40c03d673e3ce9d3a04 to your computer and use it in GitHub Desktop.
Nim Tutorial Part1
#
# break -> leaves the loop
# break [block] -> leaves the loop for block
#
block myblock:
echo "entering block - break"
while true:
echo "looping - break"
break # leaves the loop, but not the block
echo "still in block - break"
block myblock2:
echo "entering block"
while true:
echo "looping"
break myblock2 # leaves the block (and the loop)
echo "still in block"
import strutils, sequtils, parseopt
type NumberSet = tuple[odd: seq[int], even: seq[int]]
type CheckDigitResult = tuple[input: string, check_digit: int]
proc isOdd(n: int): bool =
n mod 2 == 0
proc collectNumbers(number: string): NumberSet =
var odd_numbers, even_numbers: seq[int] = @[]
for pos in 0..(len(number) - 1):
let next = pos + 1
let v = parseInt(number[pos..pos])
if isOdd(next):
odd_numbers.add(v)
else:
even_numbers.add(v)
(odd: odd_numbers, even: even_numbers)
proc checkDigit(number: string): CheckDigitResult =
let numbers = collectNumbers(number)
let odd_sum = foldl(numbers.odd, a + b)
let even_sum = foldl(numbers.even, a + b)
let odd_even_sum = odd_sum + (even_sum * 3)
let str_num = intToStr(odd_even_sum)
let last_index = len(str_num) - 1
let last_num = parseInt(str_num[last_index..last_index])
var check_digit = 0
if last_num == 0:
check_digit = 0
else:
check_digit = 10 - last_num
(input: number, check_digit: check_digit)
proc writeHelp(): void =
echo "help"
quit(QuitSuccess)
proc writeVersion(): void =
echo "1.0.0"
quit(QuitSuccess)
for kind, key, val in getopt():
case kind
of cmdArgument:
continue
of cmdLongOption, cmdShortOption:
case key
of "help", "h": writeHelp()
of "version", "v": writeVersion()
of cmdEnd: assert(false)
while endOfFile(stdin) == false:
let number = readLine(stdin)
let result = checkDigit(number)
echo result.input, result.check_digit
echo "Counting to ten: "
for i in countup(1, 10):
echo i
echo "Counting to 10: "
var i = 1
while i <= 10:
echo i
inc(i) # increment i by 1
echo "Counting down from 10 to 1: "
for i in countdown(10, 1):
echo i
echo "Counting to 1..10: "
for i in 1..10:
echo i
for index, item in ["a", "b"].pairs:
echo item, " at index ", index
# This is a comment
echo "What's your name? "
#[
multi line comment!!
]#
let name = readLine(stdin)
case name
of "":
echo "Poor soul, you lost your name?"
of "name":
echo "Very funny, your name is name."
of "Dave", "Frank":
echo "Cool name!"
else:
echo "Hi, ", name, "!"
.PHONY: build release
sources := greetings.nim number.nim while_stmt.nim for_stmt.nim when_stmt.nim block_stmt.nim stmt_and_indent.nim procedures.nim
build:
$(foreach source, $(sources), $(shell nim compile $(source) > /dev/null))
@echo "build finish"
release:
$(foreach source, $(sources), $(shell nim c -d:release $(source) > /dev/null))
@echo "release compile finish"
clean:
rm -rf nimcache
from strutils import parseInt
echo "A number please: "
let n = parseInt(readLine(stdin))
case n
of 0..2, 4..7: echo "The number is in the set: {0, 1, 2, 4, 5, 6, 7}"
of 3, 8: echo "The number is 3 or 8"
else: discard
proc yes(question: string): bool =
echo question, " (y/n)"
while true:
case readLine(stdin)
of "y", "Y", "yes", "Yes": return true
of "n", "N", "no", "No": return false
else: echo "Please be clear: yes or no"
if yes("Should I delete all your important files?"):
echo "I'm sorry Dave, I'm afraid I can't do that."
else:
echo "I think you know what the problem is just as well as I do."
var x, y: bool
#[
x = true
y = true
]#
# no indentation needed for single assignment statement:
if x: x = false
# indentation needed for nested if statement:
if x:
if y:
y = false
else:
y = true
# indentation needed, because two statements follow the condition:
if x:
x = false
y = false
when system.hostOS == "windows":
echo "running on Windows!"
elif system.hostOS == "linux":
echo "running on Linux!"
elif system.hostOS == "macosx":
echo "running on Mac OS X!"
else:
echo "unknown operating system"
echo "What's your name? "
var name = readLine(stdin)
while name == "":
echo "Please tell me your name: "
name = readLine(stdin)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment