#!/usr/bin/perl
#
# eg11: separate functions example, second version..
#

use strict;
use warnings;
use Function::Parameters qw(:strict);


#
# %freq = build_freq_hash( @array ):
#	Build a frequency hash of the elements of @array, i.e. a hash
#	mapping each element (key) to the frequency of that element in @array,
#
fun build_freq_hash( @array )
{
	my %freq; $freq{$_}++ for @array;	 # array element -> frequency
	return %freq;
}

#
# @uniq = unique_values( %freq ):
#	Deliver all non-repeated values from a %freq hash
#	in an undetermined order
#
fun unique_values( %freq )
{
	my @uniq = grep { $freq{$_} == 1 } keys %freq;
	return @uniq;
}

#
# @nonuniq = distinct_nonunique_values( %freq ):
#	Deliver all repeated (non-unique) values from %freq
#	in an undetermined order
#
fun distinct_nonunique_values( %freq )
{
	my %seen;				 # elements we've already seen
	my @nonuniq = grep 			 # distinct non-unique elements
		{ $freq{$_} > 1 && ! $seen{$_}++ } keys %freq;
	return @nonuniq;
}

#
# @distinct = distinct_values( %freq ):
#	Deliver all distinct values from %freq 
#	in an undetermined order
#
fun distinct_values( %freq )
{
	return keys %freq;
}

# main program

my @array = ( 17, 5, 3, 17, 2, 5, 7, 6, 6, 10, 3 );

my %freq = build_freq_hash( @array );
my @uniq = unique_values(%freq);
my @nonuniq = distinct_nonunique_values(%freq);
my @distinct = distinct_values(%freq);

my $str = join(',',@uniq); print qq(uniq: $str\n);
$str = join(',',@nonuniq); print qq(nonuniq: $str\n);
$str = join(',',@distinct); print qq(distinct: $str\n);
