Skip to content

Instantly share code, notes, and snippets.

iterator foobar: int =
yield 1
yield 2
yield 3
for i in foobar():
echo i
proc split_string(str): auto =
result = iterator: string =
var start = 0
for i in 0..str.high:
if str[i] == ' ':
yield str[start..i]
start = i+1
yield str[start..str.high]
var c = split_string("Hello World")
/* Generated by Nim Compiler v0.10.3 */
/* (c) 2015 Andreas Rumpf */
/* The generated code is subject to the original license. */
/* Compiled for: Linux, amd64, gcc */
/* Command for C compiler:
gcc -c -w -O3 -fno-strict-aliasing -I/media/nim/lib -o /home/deen/nimcache/asd.o /home/deen/nimcache/asd.c */
#define NIM_INTBITS 64
#include "nimbase.h"
#include <stdio.h>
total executions of each stack trace:
Entry: 1/418 Calls: 913/483 = 1.9e+02% [sum: 913; 913/483 = 1.9e+02%]
newObjRC1 3001/483 = 6.2e+02%
copyTree 7561/483 = 1.6e+03%
paramTypesMatchAux 4032/483 = 8.3e+02%
paramTypesMatch 4032/483 = 8.3e+02%
matchesAux 9109/483 = 1.9e+03%
matches 10181/483 = 2.1e+03%
pickBestCandidate 11237/483 = 2.3e+03%
resolveOverloads 11061/483 = 2.3e+03%
import posix, os
var pid = fork()
if pid == 0:
sleep(3000)
quit(3)
else:
var status: cint = 1
var ret = waitpid(pid, status, 0)
echo status
var test = cstring("foobar")
test[0] = 'g'
proc toString(s: set[char]): string =
result = ""
for c in s:
result.add c
echo toString({'a'..'c'})
var i = 20
while true:
if i mod 19 == 0 and i mod 18 == 0 and i mod 17 == 0 and i mod 16 == 0 and
i mod 15 == 0 and i mod 14 == 0 and i mod 13 == 0 and i mod 12 == 0 and
i mod 11 == 0:
echo "result: ", i
break
i += 20
@def-
def- / pe24.nim
Last active August 29, 2015 14:14
import algorithm
proc nextPermutation[T](x: var openarray[T]): bool {.discardable.} =
if x.len < 2:
return false
var i = x.high
while i > 0 and x[i-1] >= x[i]:
dec i
@def-
def- / ctrlc.nim
Created February 2, 2015 14:18
ctrl-c exceptions
import os
type CtrlCException = object of Exception
proc handler() {.noconv.} =
raise new CtrlCException
setControlCHook(handler)
try: