Skip to content

Instantly share code, notes, and snippets.

@mwgamera
mwgamera / gist:5114294
Last active December 14, 2015 16:19
Simple generators for Perl using Coro::Channel and closures (unlike Coro::Generator which manipulates the state explicitly).
#!/usr/bin/env perl
use strict;
sub gen(&) {
use Coro;
my $fun = shift;
my $arg = new Coro::Channel;
my $out = new Coro::Channel;
my $yield = sub(@) {
$out->put([@_]);
@mwgamera
mwgamera / jquery.eoime.js
Created March 24, 2013 06:08
Plena Ek!-stila klavarilo en Javascripto kun jQuery. Tajpu ĉapelajn leterojn en la x- aŭ h-sistemo kaj ripeto sufikson por forigi supersignon. Aŭ/eŭ estas aŭtomate supersignata.
// klg, 2013. WTFPL 2.
(function($) {
$.fn.eoime = function() {
return this.each(function(next, element, table) {
function replace(len, str, p, s) {
p = element.selectionStart;
s = element.value;
element.value = s.substr(0, p - len) + str + s.substr(p);
element.setSelectionRange(p -= len - 1, p);
@mwgamera
mwgamera / gist:5448977
Created April 24, 2013 01:48
Reshape multidimensional Perl array like with APL's dyadic ⍴ function.
#!/usr/bin/env perl
use strict;
# reshape: A ⍴ B → rho [A], B
sub rho($@) {
my $s = 1;
my @shape = @{+shift};
'ravel' while $#_+1 < (@_ = map { ref eq 'ARRAY' ? @$_ : $_ } @_);
$s *= $_ for @shape;
@_ = @_[0 .. $s-1];
#!/bin/sh
# fadeout.sh length [pid]...
# Slowly lower the volume along the half sine slope then kill
# some processes and restore original volume at the end.
# Requires sleepenh and amixer.
# klg, Jun 2013
CTL=PCM
LEN=${1:-0}
shift
#!/usr/bin/env perl
use strict;
use Tk;
my $E = MainWindow->new->Entry->pack;
$E->bind('<Key-Return>', sub {
my $e = shift;
my $v = $e->get;
$e->delete(0, length $v);
@mwgamera
mwgamera / Base58Check.pm
Created August 23, 2013 10:51
Validate pubkey hash address for main Bitcoin network in Perl
#!/usr/bin/env perl
# check if string looks like a valid pubkey hash address for main Bitcoin network
# klg, Aug 2013
package Base58Check;
use strict;
use bytes;
use Carp;
use Digest::SHA 'sha256';
use Math::BigInt;
// Small Promise with implicit state compatible with Promises/A+.
// klg, Sep 2013, WTFPL 2.
(function() {
"use strict";
/**
* Create new Promise using given function to schedule resolution
* of the Promise. Continuations that respectively fulfill and
* reject the Promise are passed to the setup function.
*
#!/bin/sh
# Run mcabber with dedicated gpg-agent with preset passphrase
# klg, Sep 2013
KEYGRP=AB91925C3C10DB24EDCDB821A294A4F0248DE9A1
gpg_agent=gpg-agent
gpg_connect_agent=gpg-connect-agent
gpg_preset_passphrase=/usr/lib/x86_64-linux-gnu/gnupg2/gpg-preset-passphrase
pinentry=pinentry
#!/usr/bin/env perl
# Encrypt the penguin with AES-ECB. I/O in PPM (P6) format.
# klg, Oct 2013
use strict;
use bytes;
use Crypt::Rijndael;
my $src = do { local $/; <> };
my $key = pack 'H*', '6b6c67323031332cecceab7fffe4391d';
function X = impeval(X, fun, varargin)
%IMPEVAL Evaluate point-wise transformation of multichannel image.
%
% function X = impeval(X, fun, ...)
%
% Given multidimensional matrix and a function, impeval calls the
% function with vectors along the last dimension of the matrix
% rearranged into columns of 2-D matrix as function's first argument.
% The function must return a matrix with the same number of columns
% which is then rearranged back into the shape of the original matrix