Skip to content

Instantly share code, notes, and snippets.

Hunter McMillen mcmillhj

Block or report user

Report or block mcmillhj

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View floyds-triangle.p6
multi sub floyds-triangle(1 --> List) { 1 .. 1, }
multi sub floyds-triangle(Int:D $n where * > 1 --> List) {
my $nth-triangle-number = $n * ($n + 1) div 2;
return floyds-triangle2($n - 1), $nth-triangle-number - $n ^.. $nth-triangle-number;
}
say floyds-triangle(5);
# (((((1..1) 1^..3) 3^..6) 6^..10) 10^..15
@mcmillhj
mcmillhj / stack.p6
Last active May 4, 2018
Missing Block error on line 20
View stack.p6
#!perl6
class Stack {
has Int @!store;
method push(Int:D $elem --> Nil) {
push @!store, $elem;
}
method pop(--> Int) {
@mcmillhj
mcmillhj / junction.p6
Last active Apr 30, 2018
Why does the below junction differ in result to the loop?
View junction.p6
#!perl6
my @dates = <May 15>, <May 16>, <May 19>, <June 17>, <June 18>, <July 14>, <July 16>, <August 14>, <August 15>, <August 17>;
sub day (@date) { @date[1] }
sub month (@date) { @date[0] }
sub tell($data) {
@dates.grep({ day($_) eq $data or month($_) eq $data });
}
@mcmillhj
mcmillhj / seq.p6
Created Apr 27, 2018
Why does this return a Seq? I expected a List.
View seq.p6
sub search(Int $x, Int $y, Int $max-depth is copy = 25000 --> List) {
state %cache{Set};
my @queue = (($x, $y, Nil, Nil),);
while @queue and --$max-depth > 0 {
my ($x, $y, $px, $py) = @queue.shift();
%cache{Set($x, $y)} = [$px, $py];
if $x == $y {
my @path;
@mcmillhj
mcmillhj / repl.bash
Created Apr 26, 2018
Array as Hash key
View repl.bash
> my %h{Array};
{}
> %h{[1,2]} = [3,4];
Type check failed in binding to parameter 'key'; expected Array but got Int (1)
in block <unit> at <unknown file> line 1
@mcmillhj
mcmillhj / convert.p6
Created Apr 26, 2018
Sharable Signatures?
View convert.p6
#!perl6
my @alphabet = ('0' .. '9', 'A' .. 'Z', 'a' .. 'z').flat;
sub to-digits(Int $n is copy, Int $b where $b >= 2 && $b <= 62 --> Str) {
my @digits;
while $n > 0 {
@digits.push(@alphabet[$n % $b]);
$n = $n div $b;
}
View inverse_captcha.sml
fun inverse_captcha_2 input = let
val digits = List.map (fn c => (Char.ord c) - 48) (explode input)
fun halve [] = ([], [])
| halve xs = let
val length = (List.length xs) div 2
val firstHalf = List.take(xs, length)
val secondHalf = List.drop(xs, length)
in
(xs, secondHalf @ firstHalf)
View srcAccumulate.idr
module Accumulate
export
accumulate : (a -> b) -> List a -> List b
accumulate f [] = []
accumulate f (x::xs) = f x :: accumulate f xs
View example.sml
(* foldl type
fn: ('a * 'b -> 'b) -> 'b -> 'a list -> 'b
*)
fun fromBase base = let
fun f (x, NONE ) = NONE
| f (x, SOME n) =
if x >= 0 andalso x < base then SOME(n * base + x)
else NONE
in
View tests.sml
fun run_tests _ [] = []
| run_tests f (x :: xs) =
let
fun aux { description, is_correct } =
let
val expl = description ^ ": " ^
(if is_correct then "PASSED" else "FAILED") ^ "\n"
in
(print (expl); is_correct)
end
You can’t perform that action at this time.