#!/usr/bin/ruby

#
## https://rosettacode.org/wiki/Ordered_Partitions#Sidef
#

func part(_,    {.is_empty}) { [[]] }
func partitions({.is_empty}) { [[]] }

func part(s, args) {
  var res = [];
  s.combinations(args[0], { |*c|
    part(s - c, args.slice(1)).each{|r| res << ([c] + r)}
  })
  res
}

func partitions(args) {
  part(@(1 .. args.sum), args)
}

[[],[0,0,0],[1,1,1],[2,0,2]].each { |test_case|
  say "partitions #{test_case.dump}:"
  partitions(test_case).each{|part| say part.dump }
  print "\n"
}
