in reply to Elegantly map fizz to buzz
this is a Perl 6 solution using the reduction metaoperator, tested in the REPL interpreter:
or, as a subroutine, tested under the REPL:> say [+] grep {$_ %% 5 or $_ %% 3}, 1..10; 33
Another solution, using the reduce function:> sub sum3_5 (Int $max) { return [+] grep {$_ %% 5 or $_ %% 3}, 1..$ma +x}; sub sum3_5 (Int $max) { #`(Sub|320422744) ... } > say sum3_5 10; 33
Update: yet another one:> say reduce {$^a + $^b}, grep {$_ %% 5 or $_ %% 3}, 1..10; 33
> say reduce * + *, grep {$_ %% 5 or $_ %% 3}, 1..10; 33
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Elegantly map fizz to buzz
by raiph (Deacon) on May 17, 2016 at 05:40 UTC | |
by Laurent_R (Canon) on May 17, 2016 at 14:55 UTC | |
by raiph (Deacon) on May 22, 2016 at 20:30 UTC | |
by Laurent_R (Canon) on May 23, 2016 at 06:22 UTC | |
by BrowserUk (Patriarch) on May 17, 2016 at 15:37 UTC | |
by Laurent_R (Canon) on May 17, 2016 at 23:03 UTC | |
by BrowserUk (Patriarch) on May 17, 2016 at 23:21 UTC | |
|