Skip to content

Instantly share code, notes, and snippets.

@holli-holzer
Last active July 21, 2021 00:07
Show Gist options
  • Save holli-holzer/7a6adba6f89b19360bc343007b19d655 to your computer and use it in GitHub Desktop.
Save holli-holzer/7a6adba6f89b19360bc343007b19d655 to your computer and use it in GitHub Desktop.
# https://docs.raku.org/language/typesystem#subset
# A subset gives us a thing we can smartmatch ( ~~ ) against
# at which point the where condition gets executed on the thing we
# are smartmatching.
#
# For example:
# subset StartsWithA of Str where *.starts-with("A");
# say "Brussels Sprouts" ~~ StartsWithA; # False
# say "Antonio Banderas" ~~ StartsWithA; # True
#
# Here we test that an incoming String is a valid path to an existing file
subset ExistingFile of Str where *.IO.f;
# https://docs.raku.org/type/Junction
# A Junction is a thing that represents one or more values.
# Here we have an "all" junction. There are also "none", "one", and "any".
# In many cases junctions show a kind of "implict loop" behaviour, for example
# when smartmatched against. The return value of the match depends on the contents and the kind of junction.
#
# For example:
# (True, True, True).all ~~ True; # True
# (True, True, False).all ~~ True; # False
# (True, True, False).any ~~ True; # True
# (True, True, False).one ~~ True; # False
#
# Here we match a junction made of the incoming file names
# against the ExistingFile subset.
# If not all the entries in @*ARGS are existing filenames
# then the code dies with error message 404 - Not found
die "404" unless @*ARGS.all ~~ ExistingFile;
# Define a subroutine that expects an iterable of
# lines of text as argument.
# Filter out directives and comments and return
# the result as a string.
#
# The directives filter is trivial,
# we are just grepping for lines that
# do not match the regex. But the comments filter is
# a little bit trickier, since it uses the flipflop operator.
#
# Fliplop smartmatches the left side to $_.
# It returns False until that match returns True.
# From that point it returns True until the right side gets True.
#
# Examples:
#
# grep { 2 ff 4 }, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5
# which will return 2, 3, 4, 2, 3, 4
#
# grep { not 2 ff 4 }, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5
# which gives 1, 5, 1, 5
#
sub uncomment( \lines ) {
join "\n",
# Because the flipflop gives us the exact opposite of what
# we are looking for, namely the comments, we need to negate
# the filter.
grep { not /'/*'/ ff /'*/'/ },
# skip directives
grep { not /^("//<" | "//>")/ },
# incoming data
lines }
# write the uncommented version
# for each input file
spurt .path, uncomment .lines
# turn each filename in @*ARGS into an IO object
for map *.IO, @*ARGS;
@holli-holzer
Copy link
Author

holli-holzer commented Jul 20, 2021

Feel free to ask if something is still unclear. @heyajulia

@heyajulia
Copy link

Thank you! Your comments were very helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment