Skip to content

Instantly share code, notes, and snippets.

View randrews's full-sized avatar

Ross Andrews randrews

View GitHub Profile
-- Summing the sequence 1..n in as many ways as I can think of!
-- With the closed form
function sum(n)
return ((n+1) * n) / 2
end
print(sum(6)) -- Had better be 21
-- With tail recursion
@randrews
randrews / telegram.lua
Last active April 18, 2023 04:32
Split a telegram string into component words
function load_dictionary(filename)
local dictionary = {}
for line in io.lines(filename) do
if line:match("^%l%l+$") then
insert(dictionary, line .. "$")
end
end
-- We're cutting all single-letter words, but
@randrews
randrews / bouncy.rs
Created March 27, 2023 04:57
Bouncing circle in Rust
use std::arch::aarch64::float32x4x4_t;
use std::f32::consts::PI;
use std::thread::sleep;
use std::time::Duration;
use pixels::{Pixels, PixelsBuilder};
use pixels::wgpu::TextureFormat;
use raqote::{DrawOptions, DrawTarget, Path, PathBuilder, SolidSource, Source};
use winit::dpi::LogicalSize;
use winit::event::{Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use std::f32::consts::PI;
use pixels::{Pixels, PixelsBuilder};
use pixels::wgpu::TextureFormat;
use raqote::{DrawOptions, DrawTarget, Path, PathBuilder, SolidSource, Source};
use winit::dpi::LogicalSize;
use winit::event::{Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::WindowBuilder;
fn main() -> ! {
@randrews
randrews / lua_map.c
Created April 23, 2011 22:16
Example of embedding Lua in C
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <stdlib.h>
int map_create(lua_State *lua);
int map_slice(lua_State *lua);
int main(int argc, char **argv){
lua_State *lua = lua_open();
@randrews
randrews / facts.lua
Created April 1, 2011 05:46
Make Lua look like Prolog!
----------------------------------------------------------------------------------------------------
--- Making Lua look like Prolog:
---
--- Let's use metatables for something other than emulating prototype-based OO. By making the
--- __index metamethod create values for undefined things, we can make Lua look like Prolog!
--- We create empty tables for anything starting with a capital letter, functions that populate
--- those tables for lowercase things (to assign relationships) and if a name begins with "is_"
--- then it becomes a function that queries those tables.
----------------------------------------------------------------------------------------------------
@randrews
randrews / hello.lua
Created August 8, 2011 05:04
Embedding Lua in C
-- Pack this into an object file with ld: ld -r -b binary -o hello.o hello.lua
print "Hello, World!"
@randrews
randrews / array.lua
Created July 24, 2011 05:34
Array library for Lua
function string.ordinalize(str)
local num = tonumber(str)
local endings = {"st", "nd", "rd"}
if num >= 11 and num <= 13 then
return num .. "th"
elseif num % 10 > 0 and num % 10 <= #endings then
return num .. endings[num % 10]
else
return num .. "th"
end
@randrews
randrews / hoc4.lua
Created June 21, 2015 20:53
Toy calculator in Lua, version 4
setmetatable(_ENV, { __index=lpeg })
Scopes = { {} }
function eval_expr(expr)
local accum = eval(expr[2]) -- because 1 is "expr"
for i = 3, #expr, 2 do
local operator = expr[i]
local num2 = eval(expr[i+1])
Three arguments: dest, src, mode
Mode is a word of three bytes, separated into three one-byte sub-args
The sub-args are:
- High byte: mode arg
- Mid / low bytes are a 16-bit limit arg
- (Some end modes only use the low byte)
Mode arg is three fields:
- how the src is interpreted / incremented