Skip to content

Instantly share code, notes, and snippets.

@abeaumont
Created December 16, 2011 19:18
Show Gist options
  • Save abeaumont/1487501 to your computer and use it in GitHub Desktop.
Save abeaumont/1487501 to your computer and use it in GitHub Desktop.
module: toandfro
define function group
(sequence :: <sequence>,
n :: <integer>)
=> (newseq :: <sequence>)
map(method(i) copy-sequence(sequence, start: n * i, end: n * (i + 1)) end,
range(below: floor/(sequence.size, n)));
end function group;
define function flip
(sequence :: <sequence>)
=> (newseq :: <sequence>)
local method flip-deep (sequence)
if (empty?(sequence))
make(type-for-copy(sequence));
else
map(method(i)
map(method(subseq)
first(choose-by(method(x) x = i end,
range(),
subseq));
end,
sequence);
end,
range(below: sequence.first.size));
end;
end;
apply(concatenate, as(<list>, flip-deep(sequence)));
end function flip;
define function map-index
(fn :: <function>,
collection :: <collection>)
=> (new-collection :: <collection>)
map(fn, collection, range(below: collection.size));
end function map-index;
define function toandfro
(sequence :: <sequence>,
n :: <integer>)
=> (toandfro :: <string>)
as(<string>,
flip(map-index(method(s, n)
if (odd?(n))
reverse(s);
else
s;
end;
end,
group(sequence, n))));
end function toandfro;
define function main(name, arguments)
format-out("%s\n", toandfro(arguments[0], string-to-integer(arguments[1])));
exit-application(0);
end function main;
main(application-name(), application-arguments());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment