Skip to content

Instantly share code, notes, and snippets.

@dex4er
Last active January 19, 2018 21:08
Show Gist options
  • Select an option

  • Save dex4er/1602949 to your computer and use it in GitHub Desktop.

Select an option

Save dex4er/1602949 to your computer and use it in GitHub Desktop.
10 Perl One Liners to Impress Your Friends
global import require 'prelude-ls'
# or "lsc -d" command
# 1. Multiple Each Item in a List by 2
list = [1 to 10] |> map (* 2)
list.for-each (n) !-> console.log n
# 2. Sum a List of Numbers
n = sum [1 to 1000]
console.log n
# 3. Verify if Exists in a String
word-list = ['groovy', 'akka', 'grails framework', 'spock', 'typesafe']
tweet = 'This is an example tweet talking about groovy and spock.'
verify = word-list |> map (tweet.indexOf _) |> any (> -1)
console.log verify
# 4. Read in a File
content = fs.readFileSync '/etc/passwd', 'utf-8'
console.log content
content_lines = content |> lines
console.log content_lines
# 5. Happy Birthday to You!
[1 to 4].for-each (n) !-> console.log 'Happy birthday ' + (if n == 3 then 'dear Name' else 'to You')
use 5.12.0;
# 1. Multiple Each Item in a List by 2
my @list = map { $_ * 2 } 1..10;
say foreach @list;
# 2. Sum a List of Numbers
use List::Util qw(sum);
my $sum = sum 1..1000;
say $sum;
# 3. Verify if Exists in a String
my @wordList = ('groovy', 'akka', 'grails framework', 'spock', 'typesafe');
my $tweet = 'This is an example tweet talking about groovy and spock.';
my @verify = grep { rindex($tweet, $_) >= 0 } @wordList;
say @verify ? 'yes' : 'no';
# 4. Read in a File
my $content = do { undef $/; open my $fh, "10_oneliners.pl"; <$fh> };
say $content;
my @lines = do { open my $fh, "10_oneliners.pl"; <$fh> };
say @lines;
# 5. Happy Birthday to You!
for my $i (1..4) { say 'Happy Birthday ', (($i == 3) ? 'dear Name' : 'to You') };
# 6. Filter list of numbers
my (@passed, @failed);
foreach my $n (49, 58, 76, 82, 88, 90) { my $aref = $n > 60 ? \@passed : \@failed; push @$aref, $n };
say join ",", @passed;
say join ",", @failed;
# 7. Fetch and Parse an XML web service
use HTTP::Tiny;
use XML::Simple;
use Data::Dumper;
print Dumper XMLin(HTTP::Tiny->new->get('http://search.twitter.com/search.atom?&q=perl')->{content});
# 8. Find minimum (or maximum) in a List
my ($min, $max);
for my $n (14, 35, -7, 46, 98) { $min=$n if $n<$min };
for my $n (14, 35, -7, 46, 98) { $max=$n if $n>$max };
say $min;
say $max;
# or
use List::Util qw(min max);
my ($min, $max);
$min = min (14, 35, -7, 46, 98);
$max = max (14, 35, -7, 46, 98);
say $min;
say $max;
# 9. Parallel Processing
use Parallel::Loops;
my @values;
my $pl = Parallel::Loops->new(4);
$pl->share(\@values);
$pl->foreach( [0..4], sub { push @values, exp($_) } );
say foreach @values;
# 10. Sieve of Eratosthenes
sub p { $_[0], @_ > 1 ? p(grep { $_ % $_[0] } @_) : () }
my @primes = p(2 .. 100);
say foreach @primes;
# 11. FizzBuzz
my @fizzbuzz = map { ($_ % 3 == 0 ? "Fizz" : "") . ($_ % 5 == 0 ? "Buzz" : "") || $_ } 1..100;
say foreach @fizzbuzz;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment