Skip to content

Instantly share code, notes, and snippets.

View Egor-Skriptunoff's full-sized avatar

Egor-Skriptunoff

View GitHub Profile
__=2;print(1/(
- ~ - ~ - ~ - ~ - ~ - ~ - ~ -
~ __/ ~ - ~ - ~ - ~ - ~ - ~ - ~
- __/ - ~ - __/ - ~ __/ ~ - __/__/ ~ -
~ __/ ~ - ~ __/ ~ - __/ - ~ - ~ __/ ~
- __/ - ~ - __/ - ~ __/ ~ - __/__/__/ -
~ __/ ~ - ~ __/ ~ - __/ - __/ - ~ __/ ~
- __/__/__/__/ ~ __/__/__/ ~ - __/__/__/ -
~ - ~ - ~ - ~ - ~ - ~ - ~ - ~
DEC HEX CHR %d %x %a %l %u %w %p %g(5.2+) %s %c
0 00 - - - - - - - - - cntrl
1 01 - - - - - - - - - cntrl
2 02 - - - - - - - - - cntrl
3 03 - - - - - - - - - cntrl
4 04 - - - - - - - - - cntrl
5 05 - - - - - - - - - cntrl
6 06 - - - - - - - - - cntrl
7 07 - - - - - - - - - cntrl 7 "\a" = bell
----------------------------------------------------------------------------------------------------------------
-- AUTOMAGIC TABLES
----------------------------------------------------------------------------------------------------------------
-- There is a well-known "standard" implementation of automagic tables at http://lua-users.org/wiki/AutomagicTables
--
-- Unfortunately, that implementation behaves weirdly in some situations:
-- local a = AutomagicTable()
-- local x = a.b.c
-- local y = a.b.c
-- assert(x == y) -- assertion fails !
------------------------------------------------------------------------------------
-- Module: null.lua
------------------------------------------------------------------------------------
-- How to insert nils in an array?
-- Traditional approach is to appoint some Lua value as nil-replacement.
-- A function null(...) that swaps nils with itself may be a perfect candidate:
-- 1) It represents nil values inside an array
-- long_unsigned_integers.lua
---------------------------------------------------------------------------------------------------------
-- LONG ARITHMETIC (non-negative integers of arbitrary length)
---------------------------------------------------------------------------------------------------------
-- Compatible with Lua 5.1, Lua 5.2, Lua 5.3, LuaJIT
-- THIS MODULE WAS NOT OPTIMIZED FOR VERY LONG NUMBERS (multiplication has quadratic time complexity)
-- Usage:
-- local L = require("long_unsigned_integers")
local append, concat, floor, abs = table.insert, table.concat, math.floor, math.abs
local num = {'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven',
'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'}
local tens = {'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'}
local bases = {{floor(1e18), ' quintillion'}, {floor(1e15), ' quadrillion'}, {floor(1e12), ' trillion'},
{floor(1e9), ' billion'}, {1000000, ' million'}, {1000, ' thousand'}, {100, ' hundred'}}
local insert_word_AND = false -- 101 = "one hundred and one" / "one hundred one"
local function IntegerNumberInWords(n)
--------------------------------------------------------------------------------------------------------
-- This code snippet implements pseudo-random number generator
-- Output: 32-bit integers 0..4294967295
-- Internal state (seed): 53 bits, can be read or write at any time
-- Good statistical properties of PRN sequence:
-- uniformity,
-- long period of 255 * 2^45 (approximately 2^53),
-- unpredictability
@Egor-Skriptunoff
Egor-Skriptunoff / demo.lua
Created July 23, 2017 13:11
bugfix for lua.org demo page
local write,D,L,select,tostring,ipairs,concat,execute,sub,gsub=io.write,{},8192,select,tostring,ipairs,table.concat,os.execute,string.sub,string.gsub
local T,E,I
T=io.read"*a"
T=string.match(T,"=(.-)$") or ""
T=gsub(T,"+"," ")
T=gsub(T,"%%(%x%x)",function (x) return string.char(tonumber(x,16)) end)
T=gsub(T,"^%s*=%s*","return ")
write(sub(T,1,L))
write[[</TEXTAREA>
@Egor-Skriptunoff
Egor-Skriptunoff / make_globals_backslashed.lua
Created July 16, 2018 19:46
Prefix all global identifiers in your Lua script with backslash
#!/usr/bin/env lua
---------------------------------------------------------------------------------------------------------------------------------------
-- Converter from "source text" to "globals-backslashed source text"
---------------------------------------------------------------------------------------------------------------------------------------
-- Reads Lua program from STDIN
-- Writes converted program to STDOUT (if the program is syntactically correct)
-- Writes parsing error to STDERR (if the program is not syntactically correct)
-- Usage (both Windows and Linux):
@Egor-Skriptunoff
Egor-Skriptunoff / sha256_for_LuaJIT.lua
Created September 8, 2018 17:01
SHA256 benchmark for LuaJIT
--~ How to run the benchmark:
--~ $ luajit sha256_for_LuaJIT.lua;luajit -O-narrow sha256_for_LuaJIT.lua
local table_concat, byte, char, string_rep, sub, floor, ceil, min, max =
table.concat, string.byte, string.char, string.rep, string.sub, math.floor, math.ceil, math.min, math.max
local b = require"bit"
local AND = b.band
local OR = b.bor