#!/usr/bin/ruby

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

func calculator({.is_one} ) { 1 }
func calculator(n {_ %% 2}) { n / 2 }
func calculator(n         ) { 3*n + 1 }

func succ(this {_{:value}.is_one}, _) {
    return this
}

func succ(this, get_next) {
    this{:value} = get_next(this{:value})
    this{:count}++
    return this
}

var enviornments = (1..12 -> map {|i| Hash(value => i, count => 0) });

while (!enviornments.map{ _{:value} }.all { .is_one }) {
    say enviornments.map {|h| "%4s" % h{:value} }.join;
    enviornments.range.each { |i|
        enviornments[i] = succ(enviornments[i], calculator);
    }
}

say 'Counts';
say enviornments.map{ |h| "%4s" % h{:count} }.join;
