#!/usr/bin/ruby

func permutations(callback, set, perm=[]) {
    set.is_empty && callback(perm);
    set.range.each { |i|
        __FUNC__(callback, [set[(0 ..^ i)..., (i+1 ..^ set.len)...]], [perm..., set[i]]);
    }
};

var list = [1,2,3];
permutations({|set| say set.join}, list);
