Created
November 25, 2019 18:14
-
-
Save madprops/f86b8ba85283ef6247f32bb9ebc5d0ee to your computer and use it in GitHub Desktop.
Used for a video tutorial on Nim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Mutable variable | |
var a = "hello" | |
# Immutable variable | |
let a = "hello" | |
# Specify a type | |
var age: int | |
var name: string | |
echo "Hello World!" | |
# Functions | |
proc sum(x:int, y:int): int = | |
return x + y | |
# If return is not used | |
# It will return the | |
# last expression | |
proc sum(x:int, y:int): int = | |
x + y | |
# Using a function | |
sum(1, 4) | |
# If the result of a function | |
# is not used, the 'discard' | |
# keyword is needed | |
discard sum(1, 4) | |
# Functions can be called | |
# without parenthesis | |
import strformat | |
proc greet(name:string) = | |
echo &"Hello {name}!" | |
greet "John" | |
# Functions can have | |
# default parameters | |
import strformat | |
proc greet(name:string, title="dude") = | |
echo &"Hi {name}, you {title}" | |
greet("Jones") | |
greet("Mike", "crazy dude") | |
# Case | |
# Results like this | |
# can be assigned | |
# to variables | |
let ans = case kind | |
of "globe": 1 | |
of "water": 2 | |
of "tree": 3 | |
else: 0 | |
# If else | |
if x > 10: | |
echo "Yeah!" | |
elif x > 5: | |
echo "Ok" | |
else: | |
echo "Oh no!" | |
# Inline If | |
if x > 0: echo "Yeah!" | |
# Objects | |
type Person = object | |
name: string | |
age: int | |
let p = Person(name:"Rudy", age:22) | |
echo p.name | |
# Enums | |
type Color = enum | |
Red | |
Blue | |
Green | |
proc say_color(c:Color) = | |
let cs = case c | |
of Red: "red!!" | |
of Blue: "blue!!" | |
of Green: "green!!" | |
echo cs | |
# Variables are case insensitive | |
# These are the same | |
start_count() | |
startCount() | |
startcount() | |
# Stuff needs to be declared | |
# up in the code to be usable | |
# This won't work | |
say_hi() | |
proc say_hi() = | |
echo "hi" | |
# This will work | |
proc say_hi() = | |
echo "hi" | |
say_hi() | |
# Increment/Decrement | |
var n = 10 | |
inc(n) | |
# n is now 11 | |
dec(n) | |
# n is now 10 | |
# Sequences | |
# These are dynamic lists | |
var names: seq[string] | |
for person in people: | |
names.add(person.name) | |
for name in names: | |
echo name | |
# Making an empty seq | |
var names = newSeq[string]() | |
# Contains | |
import strutils | |
let word = "Movies" | |
if not word.contains("vi"): | |
echo "Hmm" | |
# Making things public | |
# Just add an * | |
proc myPublicFunc*() = | |
echo "Ha!" | |
var config* = 123 | |
# Importing | |
import os | |
import strformat | |
import myModule | |
# Globals | |
var myGlobal* = 0 | |
# Put this in a file | |
# Whatever imports that file | |
# gets that global | |
# References | |
# You can use ptr and addr | |
# for manual references but | |
# using 'ref' is recommended | |
type Car = ref object | |
engine: string | |
color: string | |
# Instances of this | |
# will be sent as references | |
# Multiline strings | |
let s = """ | |
Hello there, | |
this is another line | |
and this is tabbed | |
haha""" | |
# Concatenating strings | |
var s = "Goobu" | |
s.add("goobo") | |
# Formatting strings | |
# This is done using & | |
import strformat | |
let place = "somewhere" | |
echo &"I'm from {place}" | |
# Tables | |
# These are like hashmaps | |
import tables | |
var stuff = {"kind":"nice", "category":"woomy"}.toTable | |
echo stuff["kind"] | |
stuff["category"] = "fun" | |
# Read/Write files | |
import os | |
var data = readFile("data.txt") | |
data.add("\nVibe Checked") | |
writeFile("data.txt", data) | |
# Iterators | |
for line in text.splitLines: | |
echo line | |
# This is a way to | |
# include the index | |
for i, name in names: | |
echo name.strip() | |
# Join | |
let army = names.join(" - ") | |
# Regex | |
import nre | |
let s = "All There" | |
let m = s.find(re"^All\s") | |
if m.isSome: | |
echo m.get | |
# Exceptions | |
try: | |
readFile("jokes.txt") | |
except: | |
echo "You're not funny!" | |
# Break specific blocks | |
block myblock: | |
for name in names: | |
for c in name.items: | |
if c == 'a': | |
break myblock | |
#Running a program | |
nim compile --run src/myprogram.nim | |
# Making a release binary | |
nim compile -d:release -o=bin/myprogram src/myprogram.nim | |
# Nimble is like npm | |
# Starting a nimble project | |
nimble init | |
# Installing dependencies... | |
# These are specified | |
# in myprogram.nimble | |
nimble install | |
# A lot of how things work | |
# internally are handled | |
# by Nim itself. | |
# Including garbage collection. | |
# It's easy to write code, | |
# just try to have a basic | |
# idea of what is happening. | |
# To see how some specific | |
# things work search google | |
# for the documentation |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Concatenating strings is
"foo" & "bar"
.Appending strings is
"foo".add "bar"
.