This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function truncate(str, size, suffix) { | |
if (!str) str = ''; | |
if (!size) size = 32; | |
if (!suffix) suffix = '...'; | |
var b = 0; | |
for (var i = 0; i < str.length; i++) { | |
b += str.charCodeAt(i) <= 255 ? 1 : 2; | |
if (b > size) { | |
return str.substr(0, i) + suffix; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function modalWord(words) { | |
// words = words.map(function(s) { return s.toLowerCase(); }); // nomalize | |
var counter = {}; | |
words.forEach(function(str){ | |
if(counter.hasOwnProperty(str)) { | |
counter[str]++; | |
} else { | |
counter[str] = 1; | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use utf8; | |
use Encode qw/encode_utf8/; | |
sub truncates { | |
my ($str, $size, $suffix) = @_; | |
$str = '' unless $str; | |
$size = 32 unless $size; | |
$suffix = "..." unless $suffix; | |
my $b = 0; | |
for (my $i = 0; $i < length $str; $i++) { | |
$b += length(encode_utf8 substr($str, $i, 1)) == 1 ? 1 : 2; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package MyCache; | |
use strict; | |
use warnings; | |
use Class::Accessor::Lite ( | |
rw => [qw/cache_dir/], | |
); | |
use Encode; | |
use Data::MessagePack; | |
use Digest::MD5 qw/md5_hex/; | |
use Path::Class; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!perl | |
use strict; | |
use warnings; | |
use Perl6::Say; | |
use Path::Class; | |
use Pod::Parser; | |
use List::MoreUtils qw/uniq/; | |
use Regexp::Assemble; | |
my @perllocals = do { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use URI; | |
use Web::Scraper; | |
sub scrape { | |
my ($class, $config) = @_; | |
my $url = "http://b.hatena.ne.jp/" | |
. $config->username | |
. "/favorite?threshold=" | |
. $config->threshold; | |
my $scraper = scraper { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Array.prototype.slice.apply(document.querySelectorAll('ul.hotentry > li')).forEach(function(s) { if(s.querySelector('li.category').innerText !== 'コンピュータ・IT' ) { s.style.display = 'none'; } }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use Perl6::Say; | |
use List::MoreUtils qw/uniq/; | |
use Regexp::Assemble; | |
# Settings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Scalar::Lazy qw/lazy/; | |
# 遅延評価 | |
my $var1 = lazy { 1 }; | |
print $var1; #=> 1 | |
# シングルトン化することもできる | |
my $var2 = lazy { $something_heavy->() } 'init'; | |
print $var2; | |
print $var2; # $something_heavy->() が一度しか実行されない |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!perl | |
use strict; | |
use warnings; | |
use Plack::Builder; | |
use Plack::Request; | |
use Plack::Middleware::WebSocket; | |
use AnyEvent; | |
use AnyEvent::Handle; | |
use Coro; |