Skip to content

Instantly share code, notes, and snippets.

@SeijiEmery
SeijiEmery / gist:cc143e415778aa0cfc57
Last active January 5, 2017 00:29
C++ example snippets for a logic sim project
// Created by Seiji Emery on 9/16/14.
// Copyright (c) 2014 Seiji Emery. All rights reserved.
//
// Base class for logic components (logic gates and I/O stuff like buttons and
// displays). Defines interface that all of these must implement (boolean internal
// state, multiple (0+) inputs, and one output value (retrieved via getState()).
// Implementations are stored as a list within the LogicSim class (hence the need
// for a uniform interface).
class LogicComponent {
@SeijiEmery
SeijiEmery / gist:c483196ea317eab3a328
Last active January 5, 2017 00:29
Lightweight js vec3 impl with experimental dynamic type checking
// Defines a better type querry function that enables object prototypes to define their own type repr
$typeof = function(value) {
return value.__proto__.type || typeof(value);
}
// Switch to 'release' (or any other value) to disable expensive typechecking.
releaseMode = 'debug';
@SeijiEmery
SeijiEmery / join.hpp
Last active August 29, 2015 14:06
C++11 Utility Functions
#include <sstream>
// Provides a generic python-inspired string join function that operates on iterators
// and containers.
template <typename Iterator>
string join (const std::string & delim, Iterator begin, Iterator end) {
std::ostringstream ss;
if (begin != end)
@SeijiEmery
SeijiEmery / string.hpp
Last active January 5, 2017 00:29
String implementation challenge
// C++ challenge: implement a string class - specifically this one, which provides most
// of basic string operations you'd need to interface w/ other C++ code.
// Note: using the stl is cheating (obviously), as much of the challenge here is in
// learning how to do low level memory management and pointer arithmetic.
class String {
public:
String (); // Default constructor (construct empty string)
String (const char *c_str); // Construct a String type from a c (null terminated) string
String (const String &); // Copy constructor
@SeijiEmery
SeijiEmery / truth_table_generator.py
Last active August 29, 2015 14:06
Truth table generator
import re
def bperm (n):
""" Returns the permutations of n boolean variables """
if n < 1:
return []
if n == 1:
return [[True], [False]]
vals = bperm(n-1)
return [ [True] + v for v in vals ] + [ [False] + v for v in vals ]
@SeijiEmery
SeijiEmery / gist:dff1cc2d28e13cfcd44b
Last active August 29, 2015 14:06
Snippet for a fast logic gate VM.
enum class NodeType {
InactiveNode,
AndGate,
OrGate,
XorGate,
NotGate,
NandGate,
NorGate,
Input, // Used for component I/O, and to implement more complicated logic components (buttons, displays, etc)
Output
@SeijiEmery
SeijiEmery / type.js
Last active August 29, 2015 14:06
Javascript utility library – implements a pythonic type system, a class creation function, some utility functions like map, reduce, join (nevermind that they're already part of Array.*), and a hacked together form of function overloading. Also contains my some of my notes on the language, which is worth a read if you're interested in language-de…
//
// type.js
// Created by Seiji Emery 5/5/2014
//
// Defines (among other things) a class creation facility, a simple type-system
// with strong reflective capabilities, and a mechanism for strongly-typed
// function overloading (using said type system).
//
// Disclaimer: None of these are very fast (overloading is implemented by stringifying
// types into a a lookup string that pulls stuff out of a dictionary of function
@SeijiEmery
SeijiEmery / gist:c781b140b60d38a4be7f
Last active August 29, 2015 14:07
Unit Converter (python)
class UnitConverter (object):
def __init__ (self):
self.conv = {}
def connect (self, a, b, k):
self.conv[a][b] = k
self.conv[b][a] = 1.0 / k
for c in self.conv[a].keys():
if c != b and c not in self.conv[b].keys():
@SeijiEmery
SeijiEmery / gist:19e980e3f95b851147bc
Last active January 5, 2017 00:27
C++ preprocessor idea
Use/implement something similar to lzz (http://www.lazycplusplus.com/), but with some extensions.
Main ideas:
- only one (.lzz?) file, instead of split header (.h) / implementation (.cpp) files
- class methods and friend functions always written inline inside the class (much cleaner/more minimalistic)
- use IDE code folding to hide stuff...
- script autogenerates the .h and .cpp files for you
- build system:
- IDEs could be tricky, but getting something to work with cmake or makefiles would be pretty easy...
- Alternatively, could implement own build system that autodetects imported local files, and generates makefiles/whatever to process and include them.
@SeijiEmery
SeijiEmery / even_fibonacci_sum.hs
Last active August 29, 2015 14:08
Project Euler Problems 1-3
-- Note: fib_inf generates an infinite list
fib_inf :: Int -> Int -> [Int]
fib_inf a b = a : fib_inf b (a+b)
-- Which is ok, since Haskell uses lazy evaluation
fib_to n = takeWhile (< n) (fib_inf 0 1)
is_even x = x `mod` 2 == 0
even_fib_sum = sum . (filter is_even) . fib_to