Implementation of longest common subsequence in Perl 6
Switch branches/tags
Nothing to show
Clone or download
hoelzro Merge pull request #5 from samcv/SPDX-license
Use SPDX identifier in license field of META6.json
Latest commit 602dae1 May 4, 2017
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib/Algorithm GLR fixes Sep 1, 2015
t Fix up done with done-testing Sep 1, 2015
.travis.yml Add Travis support Jul 9, 2015
LICENSE Add license file Aug 8, 2014
META6.json Use SPDX identifier in license field of META6.json Apr 28, 2017 Add README Aug 8, 2014




  use Algorithm::LCS;

   # regular usage
   say lcs(<A B C D E F G>, <A C F H J>); # prints T<A C F>

   # custom comparator via :compare
   say lcs(<A B C>, <D C F>, :compare(&infix:<eq>));

   # extra special custom comparison via :compare-i
   my @a        = slurp('one.txt'); 
   my @b        = slurp('two.txt'); 
   my @a-hashed ={ hash-algorithm($_) });
   my @b-hashed ={ hash-algorithm($_) });
   say lcs(@a, @b, :compare-i({ @a-hashed[$^i] eqv @b-hashed[$^j] }));


This module contains a single subroutine, lcs, that calculates the longest common subsequence between two sequences of data. lcs takes two lists as required parameters; you may also specify the comparison function (which defaults to eqv) via the &compare named parameter). Sometimes you may want to maintain a parallel array of information to consult during calculation (for example, if you're comparing long lines of a file, and you'd like a speedup by comparing their hashes rather than their contents); for that, you may use the &compare-i named parameter.