Skip to content

Instantly share code, notes, and snippets.

@Timbus
Timbus / morereadme.adoc
Last active August 5, 2018 00:54
Slightly simpler crystal extensions

Proposal for crystal-lang extension methods

Table of Contents

This is a simplified version of my previous proposal, here: https://gist.github.com/Timbus/1f278441e7ecffbb67b664a49e18adcb

Rationale for this feature

With this document I propose two new features that work together to provide lexically scoped "extension methods" to crystal objects, with the intent of providing a safer, cleaner way to both create and use libraries.

@Timbus
Timbus / dontreadme.adoc
Last active July 24, 2018 09:05
crystal-extensions

Proposal for crystal-lang extension methods (lexical imports and UFCS)

Table of Contents

Rationale for this feature

With this document I propose two new features that work together to provide lexically scoped "extension methods" to crystal objects, with the intent of providing a safer, cleaner way to both create and use libraries.

I define "extension methods" as methods that don’t (typically) modify the state of the object they are

require "openssl"
require "socket"
server = TCPServer.new("localhost", 3900)
certfile = "cert.pem"
keyfile = "key.pem"
unless File.exists?(certfile) && File.exists?(keyfile)
openssl_params = [
@Timbus
Timbus / sieve.p6
Created May 3, 2017 10:44
*In angry Megatron voice*: PRIME!!!
sub primes-sieve3(Int $max) {
my int @sieve;
my int $effective-max = ($max-1) div 2;
lazy gather for 1 .. $effective-max -> int $i {
FIRST take 2;
next if @sieve[$i];
my int $prime = 2 * $i + 1;
take $prime;
my $fill-range = ($effective-max - $i) div $prime;
for 1 .. $fill-range -> int $j {
<?
$doc = new DOMDocument;
$doc->loadHTML(file_get_contents("http://radio.7chan.org:8000/status.xsl"));
$xp = new DOMXpath($doc);
$nl = $xp->query("//div[@class='streamheader'][table[1]//h3 = 'Mount Point /radio']/following-sibling::table//tr[td = 'Content Type:']/td[@class='streamdata']/text()");
echo $nl->item(0)->textContent;
@Timbus
Timbus / gist:5989671
Created July 13, 2013 06:35
Currently working
class My::Model {
use GTKBind::Model;
attach 'text' => (
to => [ 'entry', 'label' ],
default => 'derp',
);
attach 'reset_active' => (
to => { id => 'resetbutton', property => 'sensitive' },
@Timbus
Timbus / Late.pm
Last active December 18, 2015 12:59
Tardy attributes. Fashionably late defaults.
{ package Late;
use Moose ();
use Moose::Exporter;
Moose::Exporter->setup_import_methods(
class_metaroles => {
class => ['Late::MetaRole::Class'],
attribute => ['Late::MetaRole::Attribute'],
},
);
1;}
@Timbus
Timbus / gist:1404124
Created November 29, 2011 09:14
Think I got it...
my @q;
sub async(&f){
push @q, $(gather {f({take $_})} )
};
sub yield {
for @q -> $l {
push @q, $l if $l.shift
}
}
@Timbus
Timbus / gist:860141
Created March 8, 2011 10:57
A more perl5 IO::Socket::INET
class IO::Socket::INET2 does IO::Socket {
has $.peeraddr;
has $.peerport = 80;
has $.localaddr;
has $.localport;
has $.listen;
has $.proto = PIO::PROTO_TCP;
has $.type = PIO::SOCK_STREAM;
method new (*%args is copy) {
@Timbus
Timbus / eval.pl
Created September 28, 2010 00:47
#!/usr/bin/perl
use strict;
my $name = "EvalScript";
my $version = '0.1';
Xchat::register($name, $version);
Xchat::print "$name version $version loaded\n";
my $__evalerror = 1;
my ($a, $b, @arr, %hash);