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
(import '[java.util.concurrent RejectedExecutionHandler])) | |
(def block-policy | |
"When the threadpool is maxed out, submit the runnable to the pool blockingly." | |
(reify RejectedExecutionHandler | |
(rejectedExecution [this runnable thread-pool] | |
(try | |
(.put (.getQueue thread-pool) runnable) | |
(catch InterruptedException e | |
(.interrupt (.currentThread Thread))))))) |
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
(defn throttle [c ms] | |
(let [c' (chan)] | |
(go | |
(while true | |
(>! c' (<! c)) | |
(<! (timeout ms)))) | |
c')) | |
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
(defn mapmap [f m] | |
"Map over an associative array using a function on the values. Returns a | |
new map where the keys are the same as in the original but the values are | |
the result of applying f to the original values." | |
(reduce (fn [acc-map [k v]] (assoc acc-map k (f v))) {} m)) | |
(mapmap inc {:a 1 :b 2 :c 3}) => {:a 2 :b 3 :c 4} |
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/perl | |
use autobox::Core; | |
use Test::More; | |
use Test::Warn; | |
my $hello = 'hello'; | |
is( $hello->center(7), ' hello ', |
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
brunov@lilita:~/lib/perl5i$ prove -l t/time_compat.t | |
t/time_compat.t .. 1/? | |
# Failed test 'localtime() honors TZ' | |
# at t/time_compat.t line 43. | |
# got: '18' | |
# expected: anything else | |
t/time_compat.t .. 3/? # Looks like you failed 1 test of 13. | |
t/time_compat.t .. Dubious, test returned 1 (wstat 256, 0x100) | |
Failed 1/13 subtests |
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 Benchmark 'cmpthese'; | |
use perl5i::latest; | |
my @things = map { rand_number() } (1 .. 100); | |
my @thangs = map { rand_number() } (1 .. 100); | |
sub ARRAY::diff_simple { | |
my ($this, $that) = @_; |
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 perl5i::latest; | |
use Test::More; | |
{ | |
package Object; | |
use Data::Dumper::Concise; | |
use Digest::MD5 qw(md5_hex); | |
sub new { |
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 perl5i::latest; | |
use Test::More; | |
use Test::Exception; | |
my @array = qw( foo bar baz ); | |
dies_ok { @array->grep("foo") } "Shouldn't accept scalars"; | |
dies_ok { @array->grep([qw(boo boy)]) } "Shouldn't accept array refs"; |
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
Embedding images in POD | |
Gabor's post lamenting the lack of image examples in image-related modules | |
(like charting) reminded me that once I bumped into a module that *did* show | |
images in its documentation. They weren't links to images, but actual images, | |
showing there int the CPAN search page. | |
I couldn't find that module again to see how it was done, but after a little | |
googling and talking to some freenode #perl folks, we came up with a way to do | |
it. |
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 Modern::Perl; | |
use Math::Primality 'next_prime'; | |
my $prime = 2; | |
my $sum; | |
while ( $prime < 2_000_000 ) { | |
$sum += $prime; | |
$prime = next_prime($prime); |
NewerOlder