Skip to content

Instantly share code, notes, and snippets.

# counting point mutations
# count all mutations where C != C, G !=G, A != A, and T != T
import algorithm
# define string variables
var s: string = "GAGCCTACTAACGGGAT"
var t: string = "CATCGTAATGACGGCCT"
# define count variable for number of mutations
var mutations: int = 0
@def-
def- / dna.nim
Last active February 29, 2016 15:13
import algorithm
# Complementing a strand of DNA
# I want to reverse the string "s" and then replace
# all A's with T's
# all T's with A's
# all C's with G's
# all G's with C's
# define my string variable
@def-
def- / fails.nim
Last active February 29, 2016 14:51
import tables
type X = ref object
case kind*: bool
of false:
discard
of true:
fields*: Table[int, int]
var t = X(kind: true, fields: initTable[int, int]())
const data = "AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC"
var A, C, G, T = 0
for letter in data:
case letter
of 'A': inc A
of 'C': inc C
of 'G': inc G
of 'T': inc T
@def-
def- / x.nim
Created February 23, 2016 02:07
proc f =
raise newException(ValueError, "foo")
proc g {.raises: [].} =
f()
# Fails with a nice error:
# x.nim(4, 18) template/generic instantiation from here
# x.nim(2, 3) Error: can raise an unlisted exception: ref ValueError
iterator `..`[T](s: Slice[T], step = 1): T {.inline.} =
var pos: T = int(s.a)
while pos <= int(s.b):
yield T(pos)
pos += step
for i in 1..10..2:
echo i
@def-
def- / five.nim
Last active February 15, 2016 14:45
type FiveBytes = array[5, byte]
var x: FiveBytes
echo sizeof(x)
var xs: array[10, FiveBytes]
echo sizeof(xs)
type Foo = object
proc `()`(foo: Foo) =
echo "foo"
var x: Foo
x()
type
Container[T] = concept c
c.len is Ordinal
items(c) is T
for value in c:
type(value) is T
proc foo(x: Container[int]) =
for item in x:
echo item
import posix, os, streams, marshal
const fileName = "file.mem"
var x = ("", @[0])
proc store =
let result = fork()
if result < 0:
raise newException(ValueError, "Fork failed")