Skip to content

Instantly share code, notes, and snippets.

View tobyink's full-sized avatar
🦈
hi

Toby Inkster tobyink

🦈
hi
View GitHub Profile
@tobyink
tobyink / functions.php
Last active June 2, 2020 09:18
Wordpress edit hotkey
<?php
# Add the following to your existing functions.php, if there is one...
add_action( 'wp_enqueue_scripts', function () {
$the_theme = wp_get_theme();
wp_enqueue_script( 'hotkeys', get_stylesheet_directory_uri() . '/js/hotkeys.js', [ 'jquery' ], $the_theme->get( 'Version' ), true );
} );
add_action( 'wp_head', function () {
@tobyink
tobyink / split_last.pl
Created March 3, 2020 08:19
splitting on last occurance
use strict;
use warnings;
use feature 'say';
my $input = 'foo:bar:baz:quux';
my ($fbb, $quux) =
reverse map scalar(reverse($_)),
split /:/, reverse($input), 2;
say $fbb;
package Geometry {
use MooX::Pression;
# PointND isn't a class itself, but a class generator!
# It generates classes to represent points in n dimensions.
class PointND (
IntRange[1,26] $n,
ArrayRef $labels = ['x'..'z', reverse('a'..'w')]
) {
my @letters = @{$labels}[0 .. $n-1];
@tobyink
tobyink / mxp_example1.pl
Last active January 25, 2020 10:10
MooX::Pression examples
package Bank {
use MooX::Pression;
class Account {
has balance (
type => Num,
default => 0,
handles_via => 'Number',
handles => { '_adjust_balance' => 'add' },
);
# Looks like overloaded &{} doesn't work with after_parse in either of Text::CSV_{PP,XS}
# so need to use \&{...} to get a real coderef. That's not a huge problem.
# This will die on any row that breaks the constraint...
after_parse => \&{ Tuple[Str, Str, Int, Bool, Optional[Num]] }
# This will skip on any row that breaks the constraint...
after_parse => sub {
state $type = Tuple[Str, Str, Int, Bool, Optional[Num]];
state $check = $type->compiled_check;
@tobyink
tobyink / Example.java
Created February 7, 2019 11:09
Example of using arrays in Java
/* I'm not very good at Java */
class Example {
public static void main(String[] args) {
exampleWithoutArray();
exampleWithArray();
}
public static void exampleWithoutArray () {
@tobyink
tobyink / Point.pm
Last active September 23, 2018 08:39
use strict; use warnings;
package Point {
sub new {
bless {
x => 0,
y => 0,
@_,
}, shift;
}
use strict; use warnings;
package Point {
sub new {
bless {
x => 0,
y => 0,
@_,
}, shift;
use strict;
use warnings;
use Benchmark qw(cmpthese);
package Local::Moose::Native {
use Moose;
has foo => (is => 'rw', isa => 'Int');
}
package Local::Moose::TypeTiny {
@tobyink
tobyink / perldocsort.pl
Created June 28, 2018 12:12
Better sorting for perl docs
use v5.14;
use strict;
use warnings;
use version;
use List::MoreUtils qw(part);
my @packages = map [split / - /], map { chomp; $_ } <DATA>;
my ($perl, $delta, $other) = part {
$_->[0] =~ /^perl.*delta$/ ? 1 :