Skip to content

Instantly share code, notes, and snippets.

@timo
timo / mutator-tunnel-operator.p6
Last active September 17, 2018 19:34
A little operator that lets you mutate an object in a one-liner without needing to use the mutator's return value!
"foobarbaz".comb.Array.splice(3,1,"X").join.say;
# Output: b
# Issue: splice returns what was cut out, not the result.
# Solution: Keep the array of characters around for later.
my @a = "foobarbaz".comb; @a.splice(3,1,"X"); @a.join.say;
# Output: fooXarbaz
# Don't want to introduce a local variable for this?
@timo
timo / pass-host-and-port-on-listen.patch
Created July 20, 2018 00:12
pass host and port to a "ListenSocket" in IO::Socket::Async
diff --git a/src/core/IO/Socket/Async.pm6 b/src/core/IO/Socket/Async.pm6
index 87dcf39a5..a86a9f08f 100644
--- a/src/core/IO/Socket/Async.pm6
+++ b/src/core/IO/Socket/Async.pm6
@@ -195,23 +195,32 @@ my class IO::Socket::Async {
$p
}
+ class ListenSocket is Supply {
+ has Promise $.socket-host;
@timo
timo / shlomifish_euler_287.nqp
Created June 3, 2018 01:59
nqp and perl6 optimized versions of shlomi fish's (rindolf's) euler 287 solution.
#!/usr/bin/env perl6
# The Expat License
#
# Copyright (c) 2018, Shlomi Fish
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#!/usr/bin/env perl6
# inspired by https://narimiran.github.io/2018/05/10/python-numpy-nim.html
use nqp;
constant N = 10_000;
constant sigma = 0.1e0;
constant f = (2 / N).Num;
constant mu = 0.001e0;
@timo
timo / result.txt
Created September 8, 2017 11:05 — forked from Skarsnik/result.txt
timo@schmand /tmp> perl6 --profile=heap testshellmem.p6 shell
Before shell call : 101.664 kb
html is 230457 chars
After shell call : 210.332 kb
After forced gc: 249.704 kb
html is 230457 chars
After shell call : 323.016 kb
After forced gc: 354.232 kb
html is 230457 chars
After shell call : 401.640 kb
@timo
timo / trace_spesh_optimize.gdb
Created June 28, 2017 14:55
make a spesh dump after every step of optimization, check 'em all into git, display a log with diffs afterwards
define trace_spesh_optimize
dont-repeat
python
import tempfile
import os
import gdb
spesh_trace_target_folder = tempfile.mkdtemp("", "moar-spesh-trace")
spesh_trace_target_file = os.path.join(spesh_trace_target_folder, "speshdump.txt")
os.system("git init " + spesh_trace_target_folder)
grammar g{
token garble {<[2]>};
token alpha1 {<[2]>};
token beta { <[q]> };
token delta {<+garble +beta>};
token delta1 {<+garble>};
token delta2 {<+alpha1 +beta>}
}
say so "2" ~~ /<g::delta1>/; # OK
say so "2" ~~ /<g::delta2>/; # OK
@timo
timo / cwr.p6
Last active August 18, 2016 23:18 — forked from ahalbert/cwr.p6
use v6;
use Test;
sub combinations_with_replacement(@iterable, $r) {
gather {
cwr(@iterable, [], $r);
}
}
sub cwr(@iterable, @state, $r) {
my $place = @state.elems;
# this is my solution to GRAPH.
# it offers an interactive mode and some test cases
use v6;
class Graph {
has @!nodes;
has %!neighbour;
method connect($a, $b) {
@timo
timo / 01_notable_holes.c
Created March 30, 2016 00:34
partial pahole output for MoarVM's libmoar.so
struct MVMCompUnitBody {
MVMuint8 * data_start; /* 0 8 */
MVMuint32 data_size; /* 8 4 */
/* XXX 4 bytes hole, try to pack */
MVMObject * * coderefs; /* 16 8 */
MVMuint32 num_frames; /* 24 4 */
MVMuint32 orig_frames; /* 28 4 */
MVMStaticFrame * main_frame; /* 32 8 */