Lorem Ipsum: Source Code Highlighting
Source code syntax highlighting test subset of /lorem
Code
No syntax highlighting:
module Main where
main :: IO ()
main = print "Hello World!"
Syntax highlighting:
-- Hello world example:
module Main where
data = ()
type = String
year = 2020 / 1.0 * 1
studio = 10 + 10.0 + 10e0 + 0o10 + 0x10 -- → 54
main :: IO ()
main = print "Hello World!"
Highlighting in blockquotes:
module Main where main :: IO () main = print "Hello World!"
Stress-test all syntax-highlighting classes:
Additional JS include test-case.
Floating code blocks:
module Main where
main :: IO ()
main = print "Hello World!"
module Main where
main :: IO ()
main = print "Hello World!"
module Main where
main :: IO ()
main = print "Hello World!"
Triptych code blocks:
module Main where
main :: IO ()
main = print "Hello World!"
module Main where
main :: IO ()
main = print "Hello World!"
module Main where
main :: IO ()
main = print "Hello World!"
Highlighting in collapse:
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
#define ROUND_LIMIT 300
#define MONEY_LIMIT 25000
#define MONEY_START 2500
#define MONEY_FACTOR 100.0f
// #define DATATYPE float
// #define F(X) X ## f
#define DATATYPE double
#define F(X) X
#define THREADS 8
// MEMOIZED METHOD
typedef struct {
bool known;
DATATYPE ev;
} CacheEntry;
static DATATYPE decide(CacheEntry * const table, int money, int round) {
if (round < 0) return money;
if (money == 0 || money == MONEY_LIMIT) return money;
int index = round * (MONEY_LIMIT + 1) + money;
// int index = money * ROUND_LIMIT + round;
if (table[index].known) return table[index].ev;
DATATYPE best_bet_score = -1;
for (int bet = 0; bet <= money; bet++) {
DATATYPE winres = decide(table, (money + bet > MONEY_LIMIT) ? MONEY_LIMIT : (money + bet), round - 1);
DATATYPE loseres = decide(table, money - bet, round - 1);
DATATYPE combined = F(0.6) * winres + F(0.4) * loseres;
best_bet_score = F(fmax)(best_bet_score, combined);
}
table[index] = (CacheEntry) { true, best_bet_score };
return best_bet_score;
}
void method1() {
CacheEntry *table = calloc(sizeof(CacheEntry), ROUND_LIMIT * (MONEY_LIMIT + 1));
printf("running.\n");
int best_bet = 0;
DATATYPE best_bet_score = -1;
for (int bet = 0; bet <= MONEY_START; bet++) {
DATATYPE winres = decide(table, MONEY_START + bet, ROUND_LIMIT - 1);
DATATYPE loseres = decide(table, MONEY_START - bet, ROUND_LIMIT - 1);
DATATYPE combined = F(0.6) * winres + F(0.4) * loseres;
if (combined > best_bet_score) {
best_bet = bet;
best_bet_score = combined;
}
}
printf("first round: bet %f for expected total reward of %f\n", best_bet / MONEY_FACTOR, best_bet_score / MONEY_FACTOR);
int count_known = 0;
for (int i = 0; i < ROUND_LIMIT * (MONEY_LIMIT + 1); i++) {
if (table[i].known) count_known ++;
}
printf("known: %i of %i\n", count_known, ROUND_LIMIT * (MONEY_LIMIT + 1));
}
// BOTTOM-UP METHOD
// given pass n in "from", compute pass n-1 in "to"
static void propagate(DATATYPE *from, DATATYPE *to) {
// for each money state...
#pragma omp parallel for num_threads(THREADS)
for (int a = 0; a < THREADS; a++) {
// distribute workload so that inner loop passes are approximately equal
// f(x) = 2x F(x) = x^2 = a/THREADS x = sqrt(a / THREADS)
int low = (int) (MONEY_LIMIT * sqrtf((float)a / THREADS));
int high = (int) (MONEY_LIMIT * sqrtf((a + 1.0f) / THREADS));
if (a == THREADS - 1) high = MONEY_LIMIT + 1; // precise upper border
for (int m = low; m < high; m++) {
DATATYPE max_score = 0.0;
// determine the bet that maximizes our expected earnings
// to enable vectorization we break the loop into two so that each half only gets one branch of the conditional
// DATATYPE winval = from[(m + b > MONEY_LIMIT) ? MONEY_LIMIT : (m + b)];
int high_inner = (m + m > MONEY_LIMIT) ? (MONEY_LIMIT - m) : m;
for (int b = 0; b <= high_inner; b++) {
DATATYPE winval = from[m + b];
DATATYPE loseval = from[m - b];
DATATYPE combined = F(0.6) * winval + F(0.4) * loseval;
max_score = F(fmax)(max_score, combined);
}
for (int b = high_inner + 1; b <= m; b++) {
DATATYPE winval = from[MONEY_LIMIT];
DATATYPE loseval = from[m - b];
DATATYPE combined = F(0.6) * winval + F(0.4) * loseval;
max_score = F(fmax)(max_score, combined);
}
to[m] = max_score;
}
}
}
void method2() {
DATATYPE *buf = malloc(sizeof(DATATYPE) * (MONEY_LIMIT + 1));
DATATYPE *buf_to = malloc(sizeof(DATATYPE) * (MONEY_LIMIT + 1));
// set up base case: making no bet, we have money of i
for (int i = 0; i <= MONEY_LIMIT; i++) buf[i] = i;
for (int i = ROUND_LIMIT - 1; i >= 0; i--) {
propagate(buf, buf_to);
DATATYPE *temp = buf;
buf = buf_to;
buf_to = temp;
}
int best_bet = 0;
DATATYPE best_bet_score = -1;
for (int b = 0; b <= MONEY_START; b++) {
DATATYPE winval = buf[MONEY_START + b];
DATATYPE loseval = buf[MONEY_START - b];
DATATYPE combined = 0.6 * winval + 0.4 * loseval;
if (combined > best_bet_score) {
best_bet_score = combined;
best_bet = b;
}
}
printf("first round: bet %f for expected total reward of %f\n", best_bet / MONEY_FACTOR, best_bet_score / MONEY_FACTOR);
}
int main() {
method2();
return 0;
}
import json
import os
import sys
print(r'Hello world!') # verbatim string
name = "Eric"
age = 74
f"Hello, {name}. You are {age}." # F-string
# 'Hello, Eric. You are 74.'
def split_by_book(poems):
prev_gid = -1
r = []
for entry in poems:
gid = entry['gid']
if gid != prev_gid:
if prev_gid != -1 and len(r) > 0:
yield r
r = []
prev_gid = gid
r += [entry]
if len(r) > 0:
yield r
def split_by_transition(poems):
prev_line = -2
r = []
for entry in poems:
line = entry['line']
if line != prev_line + 1:
if prev_line != -2 and len(r) > 0:
yield r
r = []
prev_line = line
r += [entry]
if len(r) > 0:
yield r
def extract(entries):
return '\n'.join(list(map(lambda x: x['s'], entries)))
def final(poems):
for book in split_by_book(poems):
yield '<|endoftext|>'
for stanza in split_by_transition(book):
if len(stanza) > 5:
yield '\n'
for entry in stanza:
line = entry['s'] # {
line = line.rstrip('} ')
yield line
def output(poems, fname):
with open(fname, "w") as f:
for line in final(poems):
f.write(line + '\n')
def main():
print('Loading poems...')
with open("gutenberg-poetry-v2.ndjson") as f:
poems=[json.loads(line) for line in f]
print('Loaded.')
output(poems, "corpus.txt")
if __name__ == "__main__":
main()