This file contains hidden or 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
import times | |
proc fib(n: int32): int32 = | |
if n <= 1: n | |
else: fib(n-1) + fib(n-2) | |
proc main = | |
let timeStart = epochTime() | |
var result: int32 = 0 |
This file contains hidden or 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
import os, times, future, math, sequtils, strutils | |
proc get_primes7(n: int32): seq[int32] = | |
if n < 2: | |
return @[] | |
result = @[2'i32] | |
if n == 2: | |
return |
This file contains hidden or 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
import base64, strutils, sha256, os | |
import libsodium.sodium | |
import libsodium.sodium_sizes | |
const | |
usage = """ | |
DDCATool - Tool to establish and manage a DDNet CA | |
usage: | |
ddcatool <command> | |
Commands: |
This file contains hidden or 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
template inReverse(x, y) = | |
y | |
x | |
inReverse do: | |
echo "Hello World" | |
do: | |
echo "Goodbye World" |
This file contains hidden or 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
import asyncdispatch | |
template doAsync(name: expr; body: untyped): untyped {.immediate.} = | |
proc name() {.async.} = | |
body | |
asyncCheck(name()) | |
doAsync(namething): | |
echo "hello" |
This file contains hidden or 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
# | |
# Search wikipedia dump for a string | |
# | |
# XML parse code credit: Rob Speer (https://github.com/rspeer/wiki2text) | |
# | |
import re, options, strutils, os, streams, parsexml | |
let mySearchRe = re"archive[.]org/w?e?b?/?[0-9]{1,14}/|[{][{][ ]?[Ww]ayback" |
This file contains hidden or 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
# Naive | |
proc containsAll(letters, text: string): bool = | |
for c in letters: | |
if c notin text: | |
return false | |
return true | |
# Only check each letter once | |
proc containsAll2(letters, text: string): bool = | |
var chars: set[char] |
This file contains hidden or 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
proc a(x: int): int = x + 1 | |
proc b(x: int): int = x + 2 | |
proc c(x: int): int = x + 3 | |
proc d(x: int): int = x + 4 | |
echo 5.d.c.b.a |
This file contains hidden or 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
var xs: seq[tuple[nr: int, name: string]] = @[(1, "foo"), (2, "bar"), (3, "foobar"), (0, "baz")] | |
# Imperatively: | |
var max = xs[0] | |
for i in 1 .. xs.high: | |
if xs[i].nr > max.nr: | |
max = xs[i] | |
echo max | |
# Functionally: |
This file contains hidden or 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
var a: ref int = new(int) # or just: var a = new int | |
a[] = 10 | |
var p = a | |
p[] = 20 |