Skip to content

Instantly share code, notes, and snippets.

View singularperturbation's full-sized avatar

Sloane Simmons singularperturbation

View GitHub Profile
@singularperturbation
singularperturbation / create_typedesc_regression.nim
Last active April 1, 2017 20:53
Error with create / createShared in current devel (5fdd03ad4d2a) of Nim
type Foo = object
fooInt*: int
proc main() =
# Error: type mismatch: got (ptr Foo) but expected 'ptr None'
var f = create(Foo)
f.fooInt = 3
echo $f
discard realloc(f, 0)
## Example of using linalg library with ref-counting GC. If *just* have ptr DMatrix64, then
## it will get GC'd since the DMatrix64 is a ref to an object - the GC doesn't keep track of ptrs (duh)
## so this will be collected on the next cycle.
##
## Solution is as the example in the manual for the string in the 'Data' tuple. Ideal solution would be
## automatically-managed memory container with destructor that deallocates the matrix, but the Matrix
## definition might have to be managed to support that.
import linalg
import threadpool
@singularperturbation
singularperturbation / test_controlc.nim
Created October 30, 2016 23:37
setControlCHook with lambda referencing local variables
import asyncdispatch
# Compiles if declared as global here:
#
# var toChange = false
proc main() =
var toChange = false
iterator doStuff*(str: string, endpos = int.high): int =
# let strlen = if endpos == int.high: str.len else: endpos+1
# yield strlen
if endpos == int.high:
yield str.len
else:
yield endpos+1 #ERROR - Will not compile
for num in doStuff("HEY"):
echo num
@singularperturbation
singularperturbation / gist:ec150255a8be199f8e80
Created January 5, 2015 22:13
Pry output for debugging safe_yaml Psych::Nodes issues (https://github.com/dtao/safe_yaml/issues/72)
[1] pry(SafeYAML::PsychResolver)> show-source -a Psych
Found 2 candidates for `Psych` definition:
Candidate 1/2: /home/singularperturbation/.rbenv/versions/2.2.0/lib/ruby/2.2.0/psych/syntax_error.rb @ line 3:
Number of lines: 19
module Psych
class SyntaxError < Psych::Exception
attr_reader :file, :line, :column, :offset, :problem, :context
@singularperturbation
singularperturbation / testslice.nim
Last active August 29, 2015 14:10
Creating new sequence from a slice
#import macros
## TODO: Convert to a template/macro
#dumpTree:
# proc newSeq[T](s: Slice): seq[T] =
# var retvar = newSeq[T](len=(s.b-s.a)+1)
# for i in s.a..s.b:
# retvar[i-(s.a)] =i
# return retvar
#
# var input = newSeq[int](-8..8)
@singularperturbation
singularperturbation / gist:b48c2c6234a17c188aaa
Last active August 29, 2015 14:07
Nimrod using libmarkdown
import os
type
MKD_FLAG = cuint
PMMIOT = pointer
proc mkd_in(inputFile: TFile,
mflag: MKD_FLAG) :
PMMIOT {.importc: "mkd_in",
dynlib: "libmarkdown.so(.2|.2.1.3)".}