Skip to content

Instantly share code, notes, and snippets.

my Str $test = q{ULL
RRDDD
LURDL
UUUUD
};
subset DirectionList of List where { .map({$_ ~~ Str and $_ ~~ 'U'|'D'|'L'|'R'|"\n"}) ==> reduce { $^a and $^b} }
say $test.comb ~~ DirectionList;
sub dl(DirectionList $d) is export {
for $d.List {
say $_;
multi sub formatted($node, ()) { $node; }
multi sub formatted($node, List @codes) {
@codes.head ~ formatted(@codes[1..*-1]);
}
say formatted "END", ('A', 'B', 'C');
@ahalbert
ahalbert / cwr.p6
Last active August 19, 2016 01:17
use v6;
use Test;
sub combinations_with_replacement(@iterable, $r) {
gather {
cwr(@iterable, [], $r);
}
}
sub cwr(@iterable, @state, $r) {
my $place = @state.elems;
sub groupby(@elements is copy, $key={ $_ }) is export {
gather {
my @rest = @elements.Array;
while ?@rest {
my $head = $key(shift @rest);
@rest = (@rest ==> grep { $head ne $key($_)});
take (@rest ==> grep { $head eq $key($_)}, $head);
}
}
#CORRECT:
say ("SSdtIGtpb".comb #Removes first element and last element, which are empty
==> map ( { @base64Table.grep($_, :k).Str.Int.base(2).Str }) #Maps each hex item to the corresponding binary string. grep returns Seq, so I have to convert it into a string to get the value.
==> map { binaryLeftPad($_, 6) } #Pads the binary elements
).join; #Turns @r into a single string
#Incorrect:
say ("SSdtIGtpb".comb #Removes first element and last element, which are empty
==> map { @base64Table.grep($_, :k).Str.Int.base(2).Str } #Maps each hex item to the corresponding binary string. grep returns Seq, so I have to convert it into a string to get the value.
==> map { binaryLeftPad($_, 6) } #Pads the binary elements
use v6;
use Text::CSV;
class Periodic {
has @.elements;
method new() { die; }
}
{ BEGIN
Periodic.elements = csv(in => "../data/pt-data1.csv");
use v6;
unit module Units;
class Unit {
has Rat $.magnitude;
has %.units;
multi method mergeUnits(Unit $u) {
my %unitm = $u.units;
my $op = &[+];
for keys %!units -> $unit {
multi sub infix:<+>(Unit $x, Unit $y) {
if (not $x.isSameUnit($y)) {die "Units do not match.";}
return Unit.new(magnitude => $x.magnitude + $y.magnitude, units => $x.units);
}
my $a = Units::Unit.new(magnitude => 1.2, units => ("m" => 1));
my $b = Units::Unit.new(magnitude => 5.0, units => ("m" => 1));
say $a + $b;
import qualified Data.Map as Map
import qualified Data.Text as Text
import qualified Data.List.Split as Split
import Text.Regex.PCRE
--Units represent
--Adding and subtraction can only be done on the same unit
--Multiplication combines units
--Exponentiation mult/div units
--Can only Ord things of the same unit
type UnitMap = Map.Map String Double
showUnitString :: Unit -> String
showUnitString Unit m u
| (null posUnits) && (null negUnits) = ms
| null posUnits = ms ++ " 1/" ++ (showall negUnits)
| null negUnits = ms ++ (showall posUnits)
| otherwise = ms ++ (showall negUnits) ++ "/" ++ (showall negUnits)
where negUnits = Map.map (abs) (Map.filter (\ x -> x < 0.0) u)
posUnits = Map.filter (\ x -> x > 0.0) u
ms = show m