Skip to content

Instantly share code, notes, and snippets.

(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}
#!/usr/bin/perl
use autobox::Core;
use Test::More;
use Test::Warn;
my $hello = 'hello';
is( $hello->center(7), ' hello ',
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
#!/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) = @_;
#!/usr/bin/env perl
use perl5i::latest;
use Test::More;
{
package Object;
use Data::Dumper::Concise;
use Digest::MD5 qw(md5_hex);
sub new {
#!/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";
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.
#!/usr/bin/env perl
use Modern::Perl;
sub is_prime {
my $n = shift;
$n <= 1 and return;
my $i = 1;
#!/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);
primes :: [Integer]
primes = 2 : filter isPrime [3, 5 ..]
where
-- only check divisibility of the numbers less than the square root of n
isPrime n = all (not . divides n) $ takeWhile (\p -> p*p <= n) primes
divides n p = n `mod` p == 0
result = sum $ takeWhile (< 2000000) primes
main = do putStrLn( show result )