Skip to content

Instantly share code, notes, and snippets.

@0racle
0racle / where-is-grep.md
Last active March 23, 2020 00:54
where is grep

One of Larry's purported factors in the design of Camelia (and by extension, Perl 6) was to appeal to 7 year old girls (and by extension, people new to programming in general).

In addition, another design principle of Perl6 was to make it "less Unix centric". Whereas Perl had s/// and tr///, Perl 6 retains these but adds subst and trans, which is friendlier to new programmers, and self-documenting to some degree.

It seems odd, then, that we still have the grep operator named so. Even in Perl, grep - as in globally search a regular expression and print - bears little resemblance to what it actually does. It's more of a filter, filtering elements out that do not evaluate True to a given expression.

What then would be it's alternative friendly name? Taking into account these design principles...

> my @bots = <Optimus Bluestreak Ironhide Jazz Ratchet Sideswipe Sunstreaker Trailbreaker>
[Optimus Bluestreak Ironhide Jazz Ratchet Sideswipe Sunstreaker Trailbreaker]

> @bots.first(/streak/)  
Bluestreak

> @bots.first(/streak/, :end)
Sunstreaker
@0racle
0racle / sort-gotcha.md
Last active July 28, 2024 06:39
Sort Gotcha

Sort Gotcha

> <matthew mark luke john>.unique(as => *.chars).sort(by => *.chars)
(mark matthew)

Ahh good, my code works... except it's not really sorting by chars here because unlike unique, sort takes a code block not a named parameter. However there is no error to indicate this is not DWIM. It just silently sorts Naturally.

If your data happens to sort Naturally in the same order your by option would have sorted it - and you don't notice - you now have a bug laying dormant in your code that could go unnoticed indefinitely.

Sort should either handle this syntax, or otherwise throw up a warning;

@0racle
0racle / Listy-Eqv.md
Last active April 26, 2016 12:34
Listy eqv
use Test;

plan 22;

sub infix:<Meqv> { @^a == @^b && all @a Zeqv @b }

my @a =  1 ,  2 ,  3 ;
my @b = '1', '2', '3';
@0racle
0racle / read-only.md
Last active May 27, 2016 07:07
Read Only
> my $a = 10
10
> my $x := $a
10
> $x = 5
5

😞

#If WinActive("ahk_class VanDyke Software - SecureCRT") || WinActive("ahk_class PuTTY")

; KEY MAPS

#IfWinActive

or...

@0racle
0racle / minimal-vimrc.vim
Created September 8, 2016 13:39
Minimal vimrc
set nocompatible
set encoding=utf-8
set fileencodings=utf-8
set number
set wildmenu
filetype plugin indent on
@0racle
0racle / magic-squares.md
Last active January 8, 2018 05:14
Magic Squares

Script to find all the Magic Squares for the numbers 3-11 and the sum 21.

my $boxes = (3..11).permutations;
my $magic = 21; 

for $boxes.grep(&magic-square) -> @box {
    for @box.rotor(3) -> @row {
        say @row.fmt('%2d');
    }   
@0racle
0racle / csv-parse.md
Last active April 19, 2017 02:06
CSV Parse

Given that dis.csv looks like this

Name|Age|DOB|Favourite Color
John|17|12/12/1998|Blue
David|31|15/07/1985|Orange
Michael|11|02/08/2005|Red

Open the file called dis.csv and assign the filehandle to the variable csvfile

@0racle
0racle / zshrc
Last active June 4, 2019 01:23
zshrc
# load my .aliases file if it exists
[[ -f ~/.aliases ]] && . ~/.aliases
# enable some features
autoload -U compinit && compinit
autoload -U colors && colors
autoload -U promptinit && promptinit
autoload -U up-line-or-beginning-search
autoload -U down-line-or-beginning-search
autoload -Uz vcs_info