Skip to content

Instantly share code, notes, and snippets.

@colomon
Last active August 29, 2015 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colomon/8689734 to your computer and use it in GitHub Desktop.
Save colomon/8689734 to your computer and use it in GitHub Desktop.
sub simple-moving-average(Int $P where * > 0) {
-> Real $x {
state @a;
@a.push($x);
@a.shift if @a > $P;
([+] @a) / @a;
}
}
my &ma3 = simple-moving-average(3);
say &ma3(1);
say &ma3(1);
say &ma3(2);
say &ma3(3);
@smls
Copy link

smls commented Jan 29, 2014

You don't even need the & sigil to invoke the sub -- this works too:

say ma3(1);

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