Re: How to use sub/method 'dir'

2018-11-25 Thread Brad Gilbert
The reason `dir('a', test => { .IO.d })` doesn't work like you expect
is that it is passed strings.

> dir('a', test => {.say});
.
c
b
..
("a/.".IO "a/c".IO "a/b".IO "a/..".IO)

So `.IO.d` is looking to see if the CWD directory had a directory of
that name, not the `a` directory.

To get it to work append the `a`

> dir('a', test => { "a/$_".IO.d })
("a/.".IO "a/b".IO "a/..".IO)

---

I would argue that inside of the `test` that `$*CWD` should be set to
`a`, and/or the values should be IO objects.
(They are going to become IO objects eventually anyway.)
This would make it so your code would just work.

sub dir ( $path, :&test ){
my @paths = &CORE::dir($path);

indir $path, { # set $*CWD

my @results = @paths.grep: {
test( $_.IO ) # use IO object, not Str
}
}
@results
}
On Sat, Nov 24, 2018 at 3:19 PM Fernando Santagata
 wrote:
>
> Hello,
>
> I think that I don't understand how the 'test' argument of 'dir' works.
> I have a directory 'a', which contains a subdirectory 'b' and a file 'c'; I 
> want to select only the subdirectories of 'a'.
>
> Using the REPL I tried to ask the content of the directory 'a':
>
> > my @dirs = dir('a')
> ["a/c".IO "a/b".IO]
> > my @dirs = dir('a', test => { .IO.d })
> ["a/.".IO "a/..".IO]
> Why omitting the test the code returns the right list, while adding the test 
> it returns just '.' and '..'?
>
> If I do the same thing for the top level directory '.' the behavior is 
> different:
>
> > my @dirs = dir('.', test => { .IO.d })
> [".".IO "a".IO "..".IO]
>
> Now I can see the directory 'a'.
> If I descend a level, doing a 'cd a', the behavior is consistent with what I 
> see at the previous level.
> I'm confused.
>
> I'm using version 2018.10.
>
> --
> Fernando Santagata


Re: How to use sub/method 'dir'

2018-11-25 Thread Fernando Santagata
To further clarify, what I did to prepare this test is:

mkdir -p test/a/b
cd test
echo > a/c

On Sun, Nov 25, 2018 at 11:11 AM Fernando Santagata <
nando.santag...@gmail.com> wrote:

> Here's output of 'a/b'.IO.d from the REPL:
>
> > 'a/b'.IO.d
> True
>
> On Sun, Nov 25, 2018 at 1:52 AM Timo Paulssen  wrote:
>
>> The dir method gives you entries in the directory you pass. If you don't
>> pass a test it'll use the default test which is none(".", ".."), i.e.
>> "anything except . and ..".
>>
>> I'm not sure why using { .IO.d } as the test would not give you b,
>> though. Can you check what "a/b".IO.d outputs? Maybe that can give us a
>> clue.
>>
>> HTH
>>   - Timo
>> On 24/11/2018 22:18, Fernando Santagata wrote:
>>
>> Hello,
>>
>> I think that I don't understand how the 'test' argument of 'dir' works.
>> I have a directory 'a', which contains a subdirectory 'b' and a file 'c';
>> I want to select only the subdirectories of 'a'.
>>
>> Using the REPL I tried to ask the content of the directory 'a':
>>
>> > my @dirs = dir('a')
>> ["a/c".IO "a/b".IO]
>> > my @dirs = dir('a', test => { .IO.d })
>> ["a/.".IO "a/..".IO]
>> Why omitting the test the code returns the right list, while adding the
>> test it returns just '.' and '..'?
>>
>> If I do the same thing for the top level directory '.' the behavior is
>> different:
>>
>> > my @dirs = dir('.', test => { .IO.d })
>> [".".IO "a".IO "..".IO]
>>
>> Now I can see the directory 'a'.
>> If I descend a level, doing a 'cd a', the behavior is consistent with
>> what I see at the previous level.
>> I'm confused.
>>
>> I'm using version 2018.10.
>>
>> --
>> Fernando Santagata
>>
>>
>
> --
> Fernando Santagata
>


-- 
Fernando Santagata


Re: How to use sub/method 'dir'

2018-11-25 Thread Fernando Santagata
Here's output of 'a/b'.IO.d from the REPL:

> 'a/b'.IO.d
True

On Sun, Nov 25, 2018 at 1:52 AM Timo Paulssen  wrote:

> The dir method gives you entries in the directory you pass. If you don't
> pass a test it'll use the default test which is none(".", ".."), i.e.
> "anything except . and ..".
>
> I'm not sure why using { .IO.d } as the test would not give you b, though.
> Can you check what "a/b".IO.d outputs? Maybe that can give us a clue.
>
> HTH
>   - Timo
> On 24/11/2018 22:18, Fernando Santagata wrote:
>
> Hello,
>
> I think that I don't understand how the 'test' argument of 'dir' works.
> I have a directory 'a', which contains a subdirectory 'b' and a file 'c';
> I want to select only the subdirectories of 'a'.
>
> Using the REPL I tried to ask the content of the directory 'a':
>
> > my @dirs = dir('a')
> ["a/c".IO "a/b".IO]
> > my @dirs = dir('a', test => { .IO.d })
> ["a/.".IO "a/..".IO]
> Why omitting the test the code returns the right list, while adding the
> test it returns just '.' and '..'?
>
> If I do the same thing for the top level directory '.' the behavior is
> different:
>
> > my @dirs = dir('.', test => { .IO.d })
> [".".IO "a".IO "..".IO]
>
> Now I can see the directory 'a'.
> If I descend a level, doing a 'cd a', the behavior is consistent with what
> I see at the previous level.
> I'm confused.
>
> I'm using version 2018.10.
>
> --
> Fernando Santagata
>
>

-- 
Fernando Santagata