Skip to content

Instantly share code, notes, and snippets.

@kevsmith
kevsmith / pteracuda demo
Created March 10, 2011 14:25
Snapshot of current
1> l(pteracuda_nifs).
{module,pteracuda_nifs}
2> {ok, Ctx} = pteracuda_nifs:new_context().
{ok,<<>>}
3> {ok, Buf} = pteracuda_nifs:new_buffer().
{ok,<<>>}
4> F = fun(_, _) -> random:uniform(100) > 49 end.
#Fun<erl_eval.12.113037538>
%% Randomize a list
5> N = lists:sort(F, lists:seq(1, 1000000)).
@kevsmith
kevsmith / gist:881645
Created March 22, 2011 17:35
smerl makes me giggle
Mod = smerl:new(maths),
{ok, Mod1} = smerl:add_func(Mod, "add(X, Y) -> X + Y."),
smerl:compile(Mod1),
5 = maths:add(2, 3),
{ok, Mod2} = smerl:add_func(Mod1, "mult(X, Y) -> X * Y."),
smerl:compile(Mod2),
5 = maths:add(2, 3),
42 = maths:mult(6, 7).
/*
* Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
@kevsmith
kevsmith / test.erl
Created July 31, 2011 18:25
Test source for doku -- A literate programming tool inspired by docco & marginalia
-module(test).
-include_lib("eunit/include/eunit.hrl").
%% A very silly define indeed.
-define(FOO, 1).
-export([test/0,
test/2]).
@kevsmith
kevsmith / multi_head.erl
Created August 25, 2011 02:23
Multi-headed anon function example
2> F = multi_head:make_fun().
#Fun<multi_head.0.128697214>
3> F(foo).
ok
4> F(bar).
less_ok
5> F(baz).
really_bad
6> F(quux).
** exception error: no function clause matching
@kevsmith
kevsmith / gist:1204590
Created September 8, 2011 20:34
Puttin' the hurt on Erlang w/mocks
2> code:unstick_mod(os).
true
3> meck:new(os).
ok
4> meck:expect(os, timestamp, fun() -> 123 end).
Crash dump was written to: erl_crash.dump
The code server called the unloaded module `os'
@kevsmith
kevsmith / example.erl
Created September 12, 2011 14:14
Example of gen_server template output
%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
%% ex: ts=4 sw=4 et
%% @author Kevin Smith <kevin@opscode.com>
%% @copyright 2011 Opscode, Inc.
-module(example).
-behaviour(gen_server).
-export([start_link/0]).
@kevsmith
kevsmith / gist:1473809
Created December 13, 2011 20:44
deadcode detecting escript
#!/usr/bin/env escript
%% -*- erlang -*-
%% Find unused exports in a given module
%% Example: deadcode mod_foo deps/foo/ebin deps/bar/ebin
%% Assumes this script is in a file named deadcode
main([Module0|Dirs]) ->
Module = list_to_atom(Module0),
{ok, _Pid} = xref:start(foo),
@kevsmith
kevsmith / igcc_session
Created April 15, 2012 14:08
Fun w/a C REPL
g++> #include <unistd.h>
g++> sizeof(pid_t);
g++> #include <stdio.h>
g++> printf("%d\n", sizeof(pid_t));
4
g++> typedef pid_t (*child_fun)(void);
g++> sizeof(child_fun);
g++> printf("%d\n", sizeof(child_fun));
8
g++>
@kevsmith
kevsmith / gist:3973703
Created October 29, 2012 13:56
Erlang docstrings for edoc descriptions and dialyzer specs
1> specs(test).
greeting(string(), integer()) -> [string()] | nil()
foo() -> ok
doit() -> ok
ok
2> specs({test, greeting, 2}).
greeting(string(), integer()) -> [string()] | nil()
ok
3> docs({test, greeting, 2}).
greeting/2: "This is another test"