#!/usr/bin/env perl

# Copyright (c) 2015 Christian Jaeger, copying@christianjaeger.ch
# This is free software. See the file COPYING.md that came bundled
# with this file.

use strict; use warnings; use warnings FATAL => 'uninitialized';
#use Function::Parameters qw(:strict);
#use Sub::Call::Tail;

# find modules from functional-perl working directory (not installed)
use Cwd 'abs_path';
our ($mydir, $myname); BEGIN {
    my $location= (-l $0) ? abs_path ($0) : $0;
    $location=~ /(.*?)([^\/]+?)_?\z/s or die "?";
    ($mydir, $myname)=($1,$2);
}
use lib "$mydir/../lib";


sub usage {
    print "usage: $myname ...
";
    exit 1;
}

use Getopt::Long;
our $verbose=0;
GetOptions("verbose"=> \$verbose,
           "help"=> sub{usage},
           ) or exit 1;
usage if @ARGV;


# Perl code from:
# http://perl6maven.com/from-iterative-to-functional-perl6-code

# use v6;
#
# my @paths    = < /tmp /var/tmp >;
# my $filename = 'temp123';
# my @ext      = <pod pm pl>;
# my @existing-files := grep *.IO.e,
#                   (@paths X~ "/$filename." X~ @ext);

use FP::Stream ":all";
use FP::List qw(list list_to_string);
use FP::Ops qw(the_method unary_operator);

{
    my $paths    = stream(qw< /tmp /var/tmp >);
    my $filename = 'temp123';
    my $ext      = list(qw<pod pm pl>);
    # `the_method` corresponds to Perl 6's `*`, but we don't have an
    # IO::e method, and cartesian_product doesn't do the string join,
    # thus resort to a manual closure:
    my $existing_files = stream_filter sub { -e join("",@_) },
      stream_cartesian_product($paths, list("/$filename."), $ext);
}

# or

my $paths    = stream(qw< /tmp /var/tmp >);
my $filename = 'temp123';
my $ext      = list(qw<pod pm pl>);
my $all_paths=
  $paths->cartesian_product(list("/$filename."), $ext)
        ->map(*list_to_string);
my $existing_files = $all_paths->filter(unary_operator "-e");



use FP::Repl::Trap;
use FP::Repl;
repl;
# at the repl, enter 'F $all_paths', 'F $existing_files'
