Skip to content

Instantly share code, notes, and snippets.

@sekia
sekia / meta.cc
Created October 7, 2016 15:13
C++ TMP exercise
template <typename S, typename T, typename F, S Init, T ...Values>
struct Fold {
template <T Car, T ...Cdr>
struct Fold1 {
constexpr static S Init_ = F::template Result<Init, Car>::Value;
constexpr static S Value = Fold<S, T, F, Init_, Cdr...>::Value;
};
constexpr static S Value = Fold1<Values...>::Value;
};
@sekia
sekia / typelist.cc
Created October 6, 2016 15:42
Parameter pack excercise
#include <cstddef>
using namespace std;
template <size_t N, typename Car, typename Cdr>
struct Nth {
static_assert(N > 0, "Index out of range.");
using Type = typename Cdr::template Nth<N - 1>::Type;
};
@sekia
sekia / coro.cc
Last active June 27, 2016 11:02
Concurrent program with libcoro (http://software.schmorp.de/pkg/libcoro.html)
#include <algorithm>
#include <cerrno>
#include <cstring>
#include <memory>
#include <queue>
#include <stdexcept>
#include "coro.h"
class Coro {
@sekia
sekia / simple-watcher.cc
Last active June 15, 2016 11:44
Tiny command runner using POCO library.
#include <iostream>
#include <memory>
#include <string>
#include "Poco/Delegate.h"
#include "Poco/DirectoryWatcher.h"
#include "Poco/Event.h"
#include "Poco/Exception.h"
#include "Poco/File.h"
#include "Poco/Mutex.h"
@sekia
sekia / compact.pl
Last active May 17, 2016 04:19
Perl version of PHP's compact() function.
#!/usr/bin/env perl
use v5.22;
use strict;
use warnings;
use Carp ();
use Data::Dumper;
use List::Util qw/first/;
use PadWalker qw/peek_my peek_our/;
@sekia
sekia / ConcurrentFetcher.pm
Last active April 12, 2016 06:10
Concurrent URL fetcher using AnyEvent.
package ConcurrentFetcher;
use strict;
use warnings;
use AE;
use AnyEvent::HTTP::Request;
use AnyEvent::HTTP::Response;
use Scope::Guard;
use Smart::Args;
@sekia
sekia / if_context.pl
Created April 1, 2016 09:46
if statement propagates its context to its blocks.
#!/usr/bin/env perl
use feature qw/say/;
use strict;
use warnings;
sub say_context {
my $wantarray = wantarray;
say do {
if ($wantarray) { 'list' }
@sekia
sekia / flipflop.h
Created January 21, 2016 20:19
Flip-flop predicate like Perl and Ruby, in C++11
#ifndef FLIPFLIOP_H__
#define FLIPFLIOP_H__
#include <functional>
#include <utility>
namespace flipflop {
template <typename Value>
class FlipFlop {
@sekia
sekia / flatten_behaviour.pl6
Created August 17, 2015 07:21
Perl 6's list flatten behaviour example
#!/usr/bin/env perl6
# cf. http://pmthium.com/2014/10/apw2014/
# "List assignment and the [ ] array constructor are unchanged; they continue
# to flatten their input elements. (Arrays are naturally flat.)"
my ($a, $b, $c) = 1, (2, 3), (4, 5, 6);
.say for $a, $b, $c; # 1, 2, 3
my $ary = [1, (2, 3), (4, 5, 6)];
@sekia
sekia / ubot.pl
Created July 10, 2015 06:03
An old twitter bot.
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use opts;
use AnyEvent;
use AnyEvent::Twitter;
use Coro;
use Coro::Channel;