Skip to content

Instantly share code, notes, and snippets.

A metatable can be defined like

local t = setmetatable({}, {
  __tostring = function() return 'custom tostring behavior!' end
})

Here are the metamethods that you can define, and their behavior

Operators

// [src] https://benchmarksgame-team.pages.debian.net/benchmarksgame/performance/toosimple.html
#include <stdio.h>
int main() {
double sum = 0.0, flip = -1.0;
for (long i = 1; i <= 10000000; ++i) {
sum += (flip *= -1.0) / (2*i - 1);
}
printf("%.24f\n", sum*4.0);
}
@r-lyeh
r-lyeh / astar.h
Created January 28, 2023 00:12 — forked from mmozeiko/astar.h
generic A* in C
// generic A* pathfinding
//
// INTERFACE
//
// mandatory macros
#ifndef ASTAR_POS_TYPE
#error ASTAR_POS_TYPE should specify position type
dumpbin /exports any.dll > any.def
lib /def:any.def /out:any.lib /machine:x64
// useful code snippets gathered from the network (via phillip trudeau)
static float euclidean_mod(float x, float y) { return x - floorf(x / y) * y; }
static bool rect_is_onscreen(RECT rect) { return (MonitorFromRect(&rect, MONITOR_DEFAULTTONULL) != NULL); }
static bool window_is_onscreen(HWND hwnd) { return (MonitorFromWindow(hwnd, MONITOR_DEFAULTTONULL) != NULL); }
// Win32: Set the window icon for every window in your app (including MessageBox() calls and assertion failures) instead of just your primary window.
static LRESULT set_window_icon_callback(int type, WPARAM wparam, LPARAM lparam) {
if (type == HCBT_CREATEWND) {
HANDLE instance_hdl = GetModuleHandleA(NULL);
/* This file defines all 2136 Japanese Jōyō kanji (常用漢字)
with the unicode codespoints formatted to fit Nuklear's font config.
#include it as part of the nk_runes definition as shown below.
Don't forget the 0 at the end of the array, as this file doesn't define it.
*/
/* Usage example, incased in #if 0 to allow comments inside comments */
#if 0
static
@r-lyeh
r-lyeh / neural-network-from-scratch.py
Created October 20, 2022 19:51 — forked from svpino/neural-network-from-scratch.py
An implementation of a neural network from scratch
import numpy as np
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def neural_network(X, y):
learning_rate = 0.1
W1 = np.random.rand(2, 4)
W2 = np.random.rand(4, 1)
@r-lyeh
r-lyeh / gdb.md
Last active December 17, 2024 19:29

how to debug with gdb

  1. cd /your-folder && chmod a+x ./your-binary
  2. gdb --args ./your-binary [args...]. Or gdb -tui --args ./your-binary [args...] for terminal UI.
  3. gdb -ex r --args ./your-binary [args...] to run it automatically.
  4. b function or b file:line or b class:function to set breakpoints
  5. r to run or restart execution
  6. s to step in execution
  7. n to step out execution
  8. c to continue execution
@r-lyeh
r-lyeh / stuns
Created April 15, 2020 10:44 — forked from zziuni/stuns
STUN server list
# source : http://code.google.com/p/natvpn/source/browse/trunk/stun_server_list
# A list of available STUN server.
stun.l.google.com:19302
stun1.l.google.com:19302
stun2.l.google.com:19302
stun3.l.google.com:19302
stun4.l.google.com:19302
stun01.sipphone.com
stun.ekiga.net

QueryData (QD)

A Query Data specification that resembles files and directories.

C API

char *result = qd_query(doc,"query");           // Return a single value from your data
array(char*) result = qd_queries(doc,"query");  // Return a result set from your data (array)

Operators