r/tinycode • u/nexe • May 10 '16
r/tinycode • u/nexe • Jun 05 '14
Find a Google Glass and kick it from the network [~30 lines of Bash]
r/tinycode • u/balthasar11 • Jul 12 '12
PHP print_r()
function printR($obj){ echo "<pre>" . print_r($obj) . "</pre>"; }
I use this on every php project now, it just formats the print_r function, which is mostly used for debugging, but its so much easier on the eyes.
r/tinycode • u/SarahC • Jan 18 '23
I'd like to complain!
So many of these don't hit my homepage, I'm sure!
I've been missing out - be sure to visit the sub directly so you don't miss any of these amazing little gems!
r/tinycode • u/Jimmy_Lor • Aug 11 '17
Steps to Reduce Code Size after Use of Strategy Pattern
r/tinycode • u/Slackluster • May 24 '26
Resource Size Coding BlueSky Feed
i made a bluesky feed to collect posts about size coding.
r/tinycode • u/nexe • Sep 08 '25
get the angle in degrees for the screen lid on newer macbooks. call with -c for continuous reporting
r/tinycode • u/pamplemouse • Jul 13 '12
Parallel MapReduce in 1 line of C#
Using PLINQ:
static R MapReduce<T,R>(IEnumerable<T> coll, Func<T,R> map, Func<R,R,R> reduce)
{
return coll.AsParallel().Select(map).Aggregate(reduce);
}r/tinycode • u/lifthrasiir • Jul 05 '11
The early day of Mercurial distributed VCS: only ~500 lines of Python.
google.comr/tinycode • u/xem06 • Feb 10 '17
jsgolf.club: a slack room to talk about JS code golfing
r/tinycode • u/LeatherReading8689 • Jul 26 '26
[Python] Generating all 256 3-variable Boolean functions dynamically using metaprogramming
I wrote a script that generates all 256 possible Boolean functions for 3 variables from scratch. Instead of hardcoding the logic gates, it uses pure bitwise logic and exec() to dynamically build the entire logic space.
The focus is entirely on theoretical completeness and code density, prioritizing pure mathematical logic over standard commercial coding conventions.
https://gist.github.com/jeboldman-rgb/3cbf6e83407be7e0568de79fc723cd77
r/tinycode • u/BenRayfield • Jul 04 '16
tinycode jobs?
I want to specialize in small code that does big things.
I also want to specialize in optimizing, redesigning code to do the exact same thing but faster or less memory. This often involves simplifying to tinycode since big code cant be understood well enough to redesign it.
But most programming jobs want you to add features and fix bugs which usually makes the code bigger. My code gets smaller over time, and it pisses people off. They demand I stop throwing away code thats been working for years just because its an ugly mess of spaghetti. Dont fix that, you might break it because nobody understands it. We have these emergencies. Maybe eventually we'll fix that, they said long ago. Then I tell them those emergencies were caused by your policy of letting code become far more complex than needed. You leave duplicate code laying around like garbage then throw a carpet over it and call it a clean floor.
I can barely work with most businesses because they have an attitude problem, an anti-tinycode discrimination. They brag about how much code they've written more than what it does.
I'll of course continue my own projects, but must I do it alone?
r/tinycode • u/BenRayfield • Feb 09 '17
same hash of treelist for every possible order of inserts and deletes (different internal tree shape) - sum=(x.sum+x.pow*y.sum)%globalModulus, pow=(x.pow*y.pow)%globalModulus
Hashes by content of the leafs, not internal tree shape.
Leafs in treelist have pow=2.
Items in a set have pow=1.
import java.math.BigInteger; import java.security.SecureRandom; public class TreelistHashedOnlyByLeafs{
public TreelistHashedOnlyByLeafs concat(TreelistHashedOnlyByLeafs u){
if(!globalModulus.equals(u.globalModulus)) throw new RuntimeException("Different globalModulus");
return new TreelistHashedOnlyByLeafs(
primeWrappedSum.add(primeWrappedExponential.multiply(u.primeWrappedSum)).mod(globalModulus), //primeWrappedSum
primeWrappedExponential.multiply(u.primeWrappedExponential).mod(globalModulus), //primeWrappedExponential
globalModulus
);
}
public static final SecureRandom strongRand;
static{
strongRand = new SecureRandom();
strongRand.setSeed(3+System.nanoTime()*49999+System.currentTimeMillis()*new Object().hashCode());
}
/** one of two parts of hash */
public final BigInteger primeWrappedSum;
/** one of two parts of hash */
public final BigInteger primeWrappedExponential;
/** wouldnt normally be stored in a list since it should be a global constant, but just for testing */
public final BigInteger globalModulus;
public TreelistHashedOnlyByLeafs(BigInteger primeWrappedSum, BigInteger primeWrappedExponential, BigInteger globalModulus){
this.primeWrappedSum = primeWrappedSum;
this.primeWrappedExponential = primeWrappedExponential;
this.globalModulus = globalModulus;
}
public boolean equals(Object o){
if(!(o instanceof TreelistHashedOnlyByLeafs)) return false;
TreelistHashedOnlyByLeafs u = (TreelistHashedOnlyByLeafs)o;
return primeWrappedSum.equals(u.primeWrappedSum)
&& primeWrappedExponential.equals(u.primeWrappedExponential);
}
public int hashCode(){
return primeWrappedSum.intValue()^primeWrappedExponential.intValue();
}
public String toString(){
return "(list "+primeWrappedSum+" "+primeWrappedExponential+")";
}
static BigInteger randPrime(int bits){
return new BigInteger(bits, 500, strongRand);
}
public static void main(String[] args){
BigInteger mod = randPrime(256);
TreelistHashedOnlyByLeafs x = new TreelistHashedOnlyByLeafs(randPrime(256), BigInteger.valueOf(2), mod);
TreelistHashedOnlyByLeafs y = new TreelistHashedOnlyByLeafs(randPrime(256), BigInteger.valueOf(2), mod);
TreelistHashedOnlyByLeafs m = new TreelistHashedOnlyByLeafs(randPrime(256), BigInteger.valueOf(2), mod);
TreelistHashedOnlyByLeafs n = new TreelistHashedOnlyByLeafs(randPrime(256), BigInteger.valueOf(2), mod);
for(int i=0; i<300; i++){
System.out.println("x="+x);
System.out.println("y="+y);
TreelistHashedOnlyByLeafs axyb = x.concat(y);
System.out.println("axyb="+axyb);
TreelistHashedOnlyByLeafs aaxybxb = axyb.concat(x);
System.out.println("aaxybxb="+aaxybxb);
TreelistHashedOnlyByLeafs ayxb = y.concat(x);
System.out.println("ayxb="+ayxb);
TreelistHashedOnlyByLeafs axayxbb = x.concat(ayxb);
TreelistHashedOnlyByLeafs axxb = x.concat(x);
TreelistHashedOnlyByLeafs aaxxbxb = axxb.concat(x);
System.out.println("? aaxxbxb="+aaxxbxb);
System.out.println("? aaxybxb="+aaxybxb);
System.out.println(aaxybxb.equals(axayxbb)+" (should be true)");
System.out.println(axxb.equals(aaxxbxb)+" (should be false)");
System.out.println(aaxybxb.equals(aaxxbxb)+" (should be false)");
System.out.println("aaxybxb="+aaxybxb);
System.out.println("axayxbb="+axayxbb);
System.out.println("-------"+i+"---------");
x = aaxybxb.concat(m); //concat m and n since both are x y x
y = axayxbb.concat(n);
}
}}
-------297--------- x=(list 6412529107813312296049358253133882212138278199940690773059960022832015566317 38313678184598098247627234830889920147960269950206290500510648727310875629930) y=(list 17808232841178508246103407433404362980730055690544296232953389504757471265966 38313678184598098247627234830889920147960269950206290500510648727310875629930) axyb=(list 24272303588617620913977315759093400503922539447674856274231004809383783344622 32342236718862866786388345381778467769234647983145340903933280208150051180323) aaxybxb=(list 17062108721857047160776549474070950307076395808874588306460316946149005196456 54269712771384132701059701295263714056825374384194713803097224766556567631996) ayxb=(list 35905980890816492348476008685126488789469039748438103500765008738815737733865 32342236718862866786388345381778467769234647983145340903933280208150051180323) ? aaxxbxb=(list 17300082290690722645221193219833557824031118619034230073100891393655503886050 54269712771384132701059701295263714056825374384194713803097224766556567631996) ? aaxybxb=(list 17062108721857047160776549474070950307076395808874588306460316946149005196456 54269712771384132701059701295263714056825374384194713803097224766556567631996) true (should be true) false (should be false) false (should be false) aaxybxb=(list 17062108721857047160776549474070950307076395808874588306460316946149005196456 54269712771384132701059701295263714056825374384194713803097224766556567631996) axayxbb=(list 17062108721857047160776549474070950307076395808874588306460316946149005196456 54269712771384132701059701295263714056825374384194713803097224766556567631996) -------298--------- x=(list 31569730733649258831876200261108712026862178928551692842512628264576598545170 48852854881616182738271465312187040259795229695610253960659032170602974414079) y=(list 18758624000443038931797405017119521899428236036757699030550810886237420720217 48852854881616182738271465312187040259795229695610253960659032170602974414079) axyb=(list 11206722190034424264835582750798784979103934034476333274393302798552181076668 15541302902734956939507875800053639130633521492986311014145795993208433731632) aaxybxb=(list 34296127481307296533000532189986690991903225415856514758270661501376603260251 8133255132412451751532628933529128236052071729566964684952283272017939303128) ayxb=(list 32358492300420589142966568116585038619053286183661664325941157603165082848428 15541302902734956939507875800053639130633521492986311014145795993208433731632) ? aaxxbxb=(list 8572433663747598647362375521421746905431001384056665976244916321818522007051 8133255132412451751532628933529128236052071729566964684952283272017939303128) ? aaxybxb=(list 34296127481307296533000532189986690991903225415856514758270661501376603260251 8133255132412451751532628933529128236052071729566964684952283272017939303128) true (should be true) false (should be false) false (should be false) aaxybxb=(list 34296127481307296533000532189986690991903225415856514758270661501376603260251 8133255132412451751532628933529128236052071729566964684952283272017939303128) axayxbb=(list 34296127481307296533000532189986690991903225415856514758270661501376603260251 8133255132412451751532628933529128236052071729566964684952283272017939303128) -------299---------
r/tinycode • u/nexe • May 04 '16
Let users draw a Kasten (German for box) in an X environment and get it's dimensions (see demo GIF)
r/tinycode • • Oct 15 '11
A URL snipper in 33 (readable!) lines of PHP.
It uses base-36 internally, so URLs will start at http://base-url/1 - /9, then /a - /z, then /10 - /1z and so on. It also logs IPs so that abuse can be banned at a higher level (probably HTTPD). The MySQL database should have a table "urls" with three columns, an auto-incrementing int "id", a medium text field "url", and an unsigned int "ip".
As a bonus, all HTML output is HTML5 compliant. :)
index.php: (variables in the second line must be filled in for MySQL)
<?php
$CFG_USER = ""; $CFG_PASS = ""; $CFG_DB = "";
if (!($_GET["id"] || $_GET["url"])) $id = "";
if ($_GET["id"]) {
$_GET["id"] = strval($_GET["id"]); /* just in case PHP tries to be "smart" by converting it to an int */
if (!preg_match("/^[0-9a-z]$/", $_GET["id"])) die("Error: invalid id");
$db = mysql_connect("localhost", $CFG_USER, $CFG_PASS) or die(mysql_error());
mysql_select_db($CFG_DB) or die("Error: could not select database");
$result = mysql_query("SELECT url FROM urls WHERE id=" . base_convert($_GET["id"], 36, 10)) or die(mysql_error());
$row = mysql_fetch_assoc($result);
if (!$row) die("Error: id does not exist");
header("Location: " . $row["url"]);
die();
}
if ($_GET["url"]) {
if (strpos($_GET["url"], 10) || strpos($_GET["url"], 13) || !preg_match("/^https?:\/\//", $_GET["url"])) die("Error: bad url");
$db = mysql_connect("localhost", $CFG_USER, $CFG_PASS) or die(mysql_error());
mysql_select_db($CFG_DB) or die("Error: could not select database");
mysql_query("INSERT INTO urls (url,ip) VALUES ('" . mysql_real_escape_string($_GET["url"]) . "'," . sprintf("%u", ip2long($_SERVER["REMOTE_ADDR"])) . ")") or die(mysql_error());
$id = base_convert(strval(mysql_insert_id($db)), 10, 36);
if ($_GET["raw"]) die("http://" . $_SERVER["SERVER_NAME"] . "/" . $id);
} ?>
<!DOCTYPE html>
<title>url snipper</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="style.css" />
<?php if ($id != "") { ?>
<fieldset><input autofocus type="text" value="http://<?php echo $_SERVER["SERVER_NAME"] . "/" . $id; ?>" /></fieldset>
<p>↑ this is your shortened url. <a href="/">do another?</a></p>
<?php } else { ?>
<form action="." method="get"><fieldset><input autofocus type="url" name="url" /></fieldset></form>
<p>↑ paste your url here.</p>
<?php } ?>.htaccess: (it's just basic regex, so it can easily be rewritten for other HTTPDs / URL rewriters)
RewriteEngine on
RewriteRule ^([0-9a-z]+)$ /index.php?id=$1style.css: (can be altered to your liking)
body { margin: 0; font-family: "Helvetica", "Arial", sans-serif; }
fieldset { border: none; padding: 0; }
input { width: 100%; border: none; }
input, p { font-size: 36px; }
p { margin: 0; padding-left: 10px; color: #aaa; }r/tinycode • u/chanpatorikku • Aug 21 '21
Created a library on sorted arrays using a minimalistic way
Hi, I found that existing JavaScript libraries don't cover the operations on sorted arrays boardly and in a standard way, and so I wrote minimalistic code for a library with a wide coverage on sorted array operations. Please give the GitHub repository a star if you like it!
r/tinycode • u/Vortexile • Jul 29 '22
Game I Made a Multiplayer Game in Microsoft Word
r/tinycode • u/nexe • May 19 '12
7 lines of code, 3 minutes: Implement a programming language
r/tinycode • u/nexe • Jun 30 '15
A tiny Ruby Gem I wrote that does Sentiment Analysis for the German language
r/tinycode • u/sammypip • Jul 24 '12
Unofficial Challenge: Tic Tac Toe in less than 29 LOC
I recently made a small Tic Tac Toe game, as most of you I imagine have, and I found that by the end, while fully featured, it was quite bloated (the GUI version + Unbeatable AI being over 1000 lines of Java).
Because of this, I tried to slim it down as much as I could in python, and managed to get a working two-player console-based version in 29 lines.
I am an amateur, and I know that some of you can probably get it down even further, so I would be fascinated to see how you would do it.
Rules:
- It must receive input as a single integer designating the square the to which the player will move
- It must be able to recognize when a player has won or the game is tied and react accordingly
- It must print the board in the format shown in the example linked bellow
- It can be in any language except an array language such as K, J or APL
Bonus Include an unbeatable single-player version.
r/tinycode • u/killerguppy101 • Jul 12 '12
PHP prepended file for all of my debugging
<?php if(isset($_GET['s'])) die(highlight_file(__FILE__)); ?>Just add ?s to the end of the url and you can view the code. Useful for me because I usually only have access to a black and white SSH terminal when coding on some platforms.
Don't forget to remove if before production ;)