Functions that return references to values
that may be modified by their callers must not be memoized.
use Memoize;
sub iota {
my $n = shift;
return [1 .. $n];
}
memoize 'iota';
$i10 = iota(10);
$j10 = iota(10);
pop @$i10;
print @$j10;
2012/07/24
2012/07/23
Unique values (Perl)
use List::MoreUtils qw( uniq );
@arr = qw( 4 4 2 2 );
@arr = uniq @arr;
print "@arr\n";
# =====
my @uniq = unique( @in );
sub unique
{
my %seen;
return grep {!$seen{$_}++} @_;
}
@arr = qw( 4 4 2 2 );
@arr = uniq @arr;
print "@arr\n";
# =====
my @uniq = unique( @in );
sub unique
{
my %seen;
return grep {!$seen{$_}++} @_;
}
2012/07/20
max len key (Perl6::Form)
my $max_len = max( map { length } keys %{$form} );
#!/usr/bin/perl
use List::Util qw( max );
use Perl6::Form;
use Data::Dumper;
my $form = {
'name' => 'mike',
'surname' => 'shmike',
'region' => 'kiev',
};
my @body;
my $max_len = max( map { length } keys %{$form} ) - 2;
foreach my $key ( keys %{$form} )
{
push @body, form(
'{' . ('>' x $max_len) . '}: {' . ('<' x $max_len) . '}',
$key, $form->{$key},
);
# print form(
# '{' . ('>' x $max_len) . '}: {' . ('<' x $max_len) . '}',
# $key, $form->{$key},
# );
}
print Dumper( \@body );
#!/usr/bin/perl
use List::Util qw( max );
use Perl6::Form;
use Data::Dumper;
my $form = {
'name' => 'mike',
'surname' => 'shmike',
'region' => 'kiev',
};
my @body;
my $max_len = max( map { length } keys %{$form} ) - 2;
foreach my $key ( keys %{$form} )
{
push @body, form(
'{' . ('>' x $max_len) . '}: {' . ('<' x $max_len) . '}',
$key, $form->{$key},
);
# print form(
# '{' . ('>' x $max_len) . '}: {' . ('<' x $max_len) . '}',
# $key, $form->{$key},
# );
}
print Dumper( \@body );
2012/07/06
shrink @arr1 by @arr2
#!/usr/bin/perl
use List::Compare;
my @arr1 = qw( free bsd shmsd );
my @arr2 = qw( shmsd yo );
my $lc = List::Compare->new( \@arr1, \@arr2 );
# intersection
my @intersection = $lc->get_intersection;
print "@intersection\n";
# union
my @union = $lc->get_union;
print "@union\n";
# outersection
my @outersection = $lc->get_symmetric_difference;
print "@outersection\n";
############
#!/usr/bin/perl
use Data::Dumper;
use List::MoreUtils qw( none );
@arr1 = qw( free bsd shmsd );
@arr2 = qw( shmsd );
# shrink @arr 1 by @arr2
@arr1 = grep {
my $arr1 = $_;
none { $_ eq $arr1 } @arr2
} @arr1;
print Dumper( \@arr1 );
( prints [free bsd] )
############
#!/usr/bin/perl
use Data::Dumper;
use List::MoreUtils qw( any );
@arr1 = qw( free bsd shmsd );
@arr2 = qw( shmsd );
# find common between @arr1 and @arr2
@arr1 = grep {
my $arr1 = $_;
any { $_ eq $arr1 } @arr2
} @arr1;
print Dumper( \@arr1 );
( prints [shmsd] )
use List::Compare;
my @arr1 = qw( free bsd shmsd );
my @arr2 = qw( shmsd yo );
my $lc = List::Compare->new( \@arr1, \@arr2 );
# intersection
my @intersection = $lc->get_intersection;
print "@intersection\n";
# union
my @union = $lc->get_union;
print "@union\n";
# outersection
my @outersection = $lc->get_symmetric_difference;
print "@outersection\n";
############
#!/usr/bin/perl
use Data::Dumper;
use List::MoreUtils qw( none );
@arr1 = qw( free bsd shmsd );
@arr2 = qw( shmsd );
# shrink @arr 1 by @arr2
@arr1 = grep {
my $arr1 = $_;
none { $_ eq $arr1 } @arr2
} @arr1;
print Dumper( \@arr1 );
( prints [free bsd] )
############
#!/usr/bin/perl
use Data::Dumper;
use List::MoreUtils qw( any );
@arr1 = qw( free bsd shmsd );
@arr2 = qw( shmsd );
# find common between @arr1 and @arr2
@arr1 = grep {
my $arr1 = $_;
any { $_ eq $arr1 } @arr2
} @arr1;
print Dumper( \@arr1 );
( prints [shmsd] )
2012/07/03
2012/07/02
jQuery autocomplete
<script type="text/javascript">
$(function() {
var cache = {};
var last_xhr;
$("#search_input").autocomplete({
minLength: 3,
source: function (request, response) {
var term = request.term;
if ( term in cache ) {
response(cache[term]);
return;
}
last_xhr = $.getJSON("/server-side/search.php", request, function(data, status, xhr) {
cache[term] = data;
if ( xhr === last_xhr ) {
response(data);
}
});
}
}).autocomplete("widget").addClass('ui-search-results');
$('#search_input').data("autocomplete")._renderItem = function(ul, item) {
return createElement(item)
.data("item.autocomplete", item)
.appendTo(ul);
};
});
</script>
$(function() {
var cache = {};
var last_xhr;
$("#search_input").autocomplete({
minLength: 3,
source: function (request, response) {
var term = request.term;
if ( term in cache ) {
response(cache[term]);
return;
}
last_xhr = $.getJSON("/server-side/search.php", request, function(data, status, xhr) {
cache[term] = data;
if ( xhr === last_xhr ) {
response(data);
}
});
}
}).autocomplete("widget").addClass('ui-search-results');
$('#search_input').data("autocomplete")._renderItem = function(ul, item) {
return createElement(item)
.data("item.autocomplete", item)
.appendTo(ul);
};
});
</script>
Subscribe to:
Posts (Atom)