Skip to content

Instantly share code, notes, and snippets.

View cjameshuff's full-sized avatar

Christopher James Huff cjameshuff

View GitHub Profile
struct VolumeVal {
uint16_t type;
float field;
};
using fieldfn_t = std::function<float(const vec3 &)>;
using volfn_t = std::function<VolumeVal(const vec3 &)>;
// Result is a function giving one of three possible results:
// the result of fnb (preexisting layers)
uint8_t duckWheel1 = {
0x1F, // #####
0x1F, // #####
0x15, // #_#_#
0x0E, // _###_
0x1B, // ##_##
0x15, // #_#_#
0x1B, // ##_##
0x0E // _###_
};
@cjameshuff
cjameshuff / gist:5903469
Created July 1, 2013 18:47
Pseudo-Smalltalk/named parameters syntax in Ruby
class Thing
def do_something(params)
puts "#{params[:a]}, #{params[:b]}, #{params[:c]}"
end # do_something()
end # class Thing
myThing = Thing.new
myThing.do_something(a: 3.14159, b: 2.71828, c: 1.618033)
#!/usr/bin/env ruby
# ./totext.rb FILENAME
require "rexml/document"
puts "Converting #{ARGV[0]}"
fin = File.new(ARGV[0])
doc = REXML::Document.new(fin)
template<typename cb_t>
void add_handler(const cb_t & cb) {
auto existing = globalEventHandlers[event];
globalEventHandlers[event] = (existing)? []() -> bool {
if(cb())
return true;
return existing();
} : cb;
}
#include <iostream>
#include <cstdlib>
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Text_Display.H>
#include <FL/Fl_Multiline_Input.H>
static void GenericCallback(Fl_Widget * widget, void * data) {
auto cb = static_cast<std::function<void(Fl_Widget *)> *>(data);
(*cb)(widget);
}
template<typename cb_t>
void cb(Fl_Widget * widget, const cb_t & cb) {
auto wrapped = new std::function<void(Fl_Widget *)>;
*wrapped = cb;
widget->callback(GenericCallback, wrapped);
@cjameshuff
cjameshuff / lambdafun.cpp
Last active November 14, 2015 16:32
Fun with lambdas
#include <cstdlib>
#include <iostream>
#include "boost/format.hpp"
using boost::format;
namespace iter {
template<typename T>
std::tuple<double, double, double, double> stat_test(int ntrials, const T & fn)
{
double m = 0, m2 = 0, s = 0, mn = DBL_MAX, mx = -DBL_MAX;
int n = 0;
for(int j = 0; j < ntrials; ++j)
{
double x = fn();
mn = std::min(mn, x);
static inline fstring operator%(const fstring & lhs, float4 rhs) {
std::string fs = lhs.next_fmt("f");
fs = (fstring("(%s, %s, %s, %s)")% fs % fs % fs % fs).str();
return lhs.append((fstring(fs)% rhs.x % rhs.y % rhs.z % rhs.w).str());
}