#!/usr/bin/ruby

#
## https://rosettacode.org/wiki/Permutations_by_swapping
#

func perms(n) {
   var perms = [[+1]]
   n.times { |x|
      var sign = -1;
      perms = gather {
        for s,*p in perms {
          var r = (0 .. p.len);
          take((s < 0 ? r : r.flip).map {|i|
            [sign *= -1, p[0..i-1], x, p[i..p.end]]
          }...)
        }
      }
   }
   perms;
}

var n = 4;
for p in perms(n) {
    var s = p.shift
    s > 0 && (s = '+1')
    say "#{p} => #{s}"
}
