Skip to content

Instantly share code, notes, and snippets.

@nikki93
Created October 15, 2020 17:55
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 nikki93/3571568c1abcaa7942936872b14c97b6 to your computer and use it in GitHub Desktop.
Save nikki93/3571568c1abcaa7942936872b14c97b6 to your computer and use it in GitHub Desktop.
# Floats
block:
let f = 1.0
echo f.abs
# `let`, `var`, `const`, ...
block:
block: # let
let number = 10
#number = 4000 # <-- can't reassign
echo number
block: # var
var number = 10
number = 5
echo number
block: # const
proc generate(): string =
result = ""
echo("generating at compile time...")
for i in 0..4:
result.add($i)
const generated = generate()
echo generated
# assert
block:
assert 3 == 3
#assert 2 == 3
# Procedures
block:
# `discard`
proc foo1(param: string): string = "foo'd: " & param
#foo("ohai") # <-- need to use result
echo foo1("ohai")
discard foo1("ohai")
# `result`
proc foo2(): string =
result = "wooo"
#"nope" # <-- ambiguity of result
echo foo2()
# Overloading
proc foo3(a: int) =
echo("1: ", a)
proc foo3(a, b: int) =
echo("2: ", a, b)
proc foo3(s: string) =
echo("3: ", s)
foo3(1)
foo3(1, 2)
foo3("ooo")
# Default parameters
proc fooDef(a: int, b: int = 3) =
echo("4: ", a, b)
fooDef(1, 2)
# Varargs
proc fooVarargs(things: varargs[int]): string =
result = ""
for i in things:
result.add($i)
echo fooVarargs(1, 2, 3)
# Lambdas
proc doIt(a: int, f: proc (x: int): int) =
echo f(a)
doIt(2, proc (x: int): int = 2 * x)
# Lambda captures
echo "captures"
var k = 3
let f = proc (x: int): int = k * x
doIt(2, f)
k = 4
doIt(2, f)
echo "end captures"
import sugar
block:
# Sugary lambdas
proc doIt(a: int, f: proc (x: int): int) =
echo f(a)
doIt(2, (x: int) -> int => 2 * x)
# Arrays
block:
echo "arrays"
var arr: array[3, int]
arr = [1, 2, 3]
echo arr[1]
#arr = [1, 2]
#echo arr[5]
# Sequences
block:
echo "sequences"
var s = @[1, 2]
s.add(3)
echo s
#var nope: seq[int]
#echo nope[0]
var t: seq[int] = @[]
echo t
t.add(42)
echo t
import sequtils
block:
var t = newSeq[string](3)
t[0] = "hai"
echo t
t.add("oho")
echo t
echo t.filter(x => x != "")
echo t.filter(proc (x: string): bool = x != "")
# Control flow
block:
echo "control flow"
let yes = true
let three = if yes: 3 else: 2
echo three
case 3
of 3:
echo "it's 3!"
of 1:
echo "it's 1!"
else:
echo "oops"
type Stuff = object
size: int
iterator items(stuff: Stuff): int =
for i in 1..stuff.size:
yield i
for i in Stuff(size: 4):
echo $i
# Exceptions
block:
proc bar(a: int) =
raise newException(IOError, "zomg")
proc foo(x: int) =
bar(x + 2)
#foo(3)
try:
foo(3)
except:
echo("hey we got an error: ", getCurrentExceptionMsg())
# Objects
block:
type Person = object
name: string
age: int
proc grow(person: var Person) =
person.age += 1
proc show(person: Person) =
echo person
proc `=`(a: var Person, b: Person) {.error.} # Trying 'no copies'
block:
echo "values!"
var v = Person(name: "hai", age: 3)
#var u = v
v.show()
v.grow()
v.show()
block:
echo "refs!"
var v: ref Person = (ref Person)(name: "oho", age: 7)
echo v.name
var u = v
echo u.name
u.name = "hehe"
echo v.name
echo u.name
echo v[]
# Tuples
block:
type Point = tuple
x: int
y: int
let pos: Point = (x: 3, y: 4)
assert pos == (3, 4)
let (x, y) = pos
echo("x: ", x, ", y: ", y)
echo pos[0]
echo pos[1]
# Enums, sets
block:
type Color {.pure.} = enum
Red, Green, Blue
var cols = {Red, Green, Red}
echo cols.len
echo(Red in cols)
import sets
block:
var strs = toHashSet(["hello", "world", "hello"])
echo strs.len
echo "hello" in strs
strs.incl("world")
echo strs.len
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment