Skip to content

Instantly share code, notes, and snippets.

@technicallyagd
technicallyagd / iteratorGen.nim
Created December 8, 2018 08:03
proc that returns iterator
let
ages = @[20, 25, 18, 32]
proc reader(lst: seq[int]): iterator(): int =
result = iterator (): int =
for n in lst:
yield n
proc main() =
let read = reader(ages) # How to pass an argument?
@technicallyagd
technicallyagd / main.nim
Created December 8, 2018 06:56
Nim iterator used like generator in python
let numbers = @[2, 6, 3, 8, 5]
iterator read(): int{.closure.} =
for n in numbers:
yield n
proc main() =
var
first = read
second = read
@technicallyagd
technicallyagd / longOuter.nim
Last active December 6, 2018 08:01
Minimal example to demonstrate performance difference between seq and array access
import times
template benchmark*(benchmarkName: string, code: untyped) =
block:
let timeStart = cpuTime()
code
echo benchmarkName
echo cpuTime() - timeStart, "s"
var seqBase = @[@[1, 2], @[322146413, 397146891], @[2100521352, 391739920], @[
@technicallyagd
technicallyagd / makeArrayGreat.nim
Last active December 4, 2018 16:29
Let's go crazy.
import macros
macro `&*`[J:static[int]](a: array[J, int], x: static[int]): untyped =
result = nnkBracket.newTree()
for i in 0 ..< x:
for j in 0 ..< J:
result.add a[j]
expandMacros:
let key = [1,2,3] &* 10
@technicallyagd
technicallyagd / vulkan.nim
Created November 1, 2018 10:05
vulkan.nim for Version 1.1.85
template vkMakeVersion*(major, minor, patch: untyped): untyped =
(((major) shl 22) or ((minor) shl 12) or (patch))
const
vkVersion10* = vkMakeVersion(1, 0, 0)
template vkVersionMajor*(version: untyped): untyped =
((uint32)(version) shr 22)
template vkVersionMinor*(version: untyped): untyped =
(((uint32)(version) shr 12) and 0x000003FF)
template vkVersionPatch*(version: untyped): untyped =
((uint32)(version) and 0x00000FFF)
@technicallyagd
technicallyagd / macrosTemp.tmpl
Last active October 18, 2018 09:04
The macro to generate template with SCF
#? stdtmpl | standard
# view(
# for i in 0..12:
# if true == true:
<h1>hello world $i</h1>
# end if
# end for
# )