Skip to content

Instantly share code, notes, and snippets.

@librasteve
Created May 18, 2024 16:51
Show Gist options
  • Save librasteve/36ca4a2876618ea426d30aa80667e923 to your computer and use it in GitHub Desktop.
Save librasteve/36ca4a2876618ea426d30aa80667e923 to your computer and use it in GitHub Desktop.
How Functional Is Raku?
#viz. https://rosettacode.org/wiki/100_doors
#`[ Elm for comparison
import List exposing (indexedMap, foldl, repeat, range)
import Html exposing (text)
import Debug exposing (toString)
type Door = Open | Closed
toggle d = if d == Open then Closed else Open
toggleEvery : Int -> List Door -> List Door
toggleEvery k doors = indexedMap
(\i door -> if modBy k (i+1) == 0 then toggle door else door)
doors
n = 100
main =
text (toString (foldl toggleEvery (repeat n Closed) (range 1 n)))
#]
#`[
enum Door <Closed Open>;
sub toggle(\d) { if d {Closed} else {Open} };
sub toggleEvery( Door @doors, Int \k --> Array[Door]() ) {
@doors.map: -> \door { if ++$ %% k { toggle door } else { door } };
}
my \n = 100;
my Door @doors = Closed xx n;
say reduce &toggleEvery, @doors, |(1..n);
#]
#`[
enum Door <Closed Open>;
sub toggle(\d) { Door(+not d) };
sub toggleEvery( @doors, \k ) {
@doors.map: { ++$ %% k ?? toggle $_ !! $_ };
}
my \n = 100;
say reduce &toggleEvery, [Closed xx 100], |(1..n);
#]
#`[
enum Door <Closed Open>;
sub toggle(\d) { Door(+not d) };
my @doors = Closed xx 100;
(.=&toggle for @doors[0, $_ ... 100]) for 1..100;
print <Closed Open>[ @doors[$_] ] for 1..100;
#]
#[
my \n = 100;
my @doors = False xx n;
(.=not for @doors[0, $_ ... n]) for 1..n;
print <Closed Open>[ @doors[$_] ] for 1..n;
#]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment