Jump to page: 1 2
Thread overview
Obsecure problem 2
Aug 03, 2022
pascal111
Aug 03, 2022
jfondren
Aug 03, 2022
pascal111
Aug 03, 2022
jfondren
Aug 03, 2022
pascal111
Aug 03, 2022
jfondren
Aug 03, 2022
pascal111
Aug 03, 2022
Ruby The Roobster
Aug 04, 2022
pascal111
Aug 03, 2022
jfondren
Aug 04, 2022
pascal111
Aug 04, 2022
Ali Çehreli
August 03, 2022

I tried to make a template that receive lambda expression to apply it on a given range the user specifies, but I found non-understood problem:

'''D

module main;

import std.stdio;
import std.functional;

template foo(alias predicate)
if (is(typeof(unaryFun!predicate)))
{
    alias notfunny=unaryFun!predicate;

auto foo(Range)(Range range) if (isInputRange!(Unqual!Range)){

    foreach(x; range)
        notfunny(x);

    return range;}

}

int main()
{

    int[] lolo = [12, 66, 654, -98, 54];

    lolo.foo(a=>a+2);

    lolo.writeln;

    return 0;
}
'''
August 03, 2022

On Wednesday, 3 August 2022 at 16:59:53 UTC, pascal111 wrote:

>

I tried to make a template that receive lambda expression to apply it on a given range the user specifies, but I found non-understood problem:

Compare with:

auto foo(Range)(Range range) { // remove isInputRange!T test
...
         lolo.foo!(a=>a+2); // call foo!(f)() rather than foo!()(f)
August 03, 2022

On Wednesday, 3 August 2022 at 17:09:11 UTC, jfondren wrote:

>

On Wednesday, 3 August 2022 at 16:59:53 UTC, pascal111 wrote:

>

I tried to make a template that receive lambda expression to apply it on a given range the user specifies, but I found non-understood problem:

Compare with:

auto foo(Range)(Range range) { // remove isInputRange!T test
...
         lolo.foo!(a=>a+2); // call foo!(f)() rather than foo!()(f)

Ok! the program ran and gives "[12, 66, 654, -98, 54]" without adding "2" as I requested!! why?!

August 03, 2022

On Wednesday, 3 August 2022 at 17:33:40 UTC, pascal111 wrote:

>

On Wednesday, 3 August 2022 at 17:09:11 UTC, jfondren wrote:

>

On Wednesday, 3 August 2022 at 16:59:53 UTC, pascal111 wrote:

>

I tried to make a template that receive lambda expression to apply it on a given range the user specifies, but I found non-understood problem:

Compare with:

auto foo(Range)(Range range) { // remove isInputRange!T test
...
         lolo.foo!(a=>a+2); // call foo!(f)() rather than foo!()(f)

Ok! the program ran and gives "[12, 66, 654, -98, 54]" without adding "2" as I requested!! why?!

You call notfunny(x) in a loop, discarding the result of the addition.

August 03, 2022

On Wednesday, 3 August 2022 at 18:25:50 UTC, jfondren wrote:

>

On Wednesday, 3 August 2022 at 17:33:40 UTC, pascal111 wrote:

>

On Wednesday, 3 August 2022 at 17:09:11 UTC, jfondren wrote:

>

On Wednesday, 3 August 2022 at 16:59:53 UTC, pascal111 wrote:

>

I tried to make a template that receive lambda expression to apply it on a given range the user specifies, but I found non-understood problem:

Compare with:

auto foo(Range)(Range range) { // remove isInputRange!T test
...
         lolo.foo!(a=>a+2); // call foo!(f)() rather than foo!()(f)

Ok! the program ran and gives "[12, 66, 654, -98, 54]" without adding "2" as I requested!! why?!

You call notfunny(x) in a loop, discarding the result of the addition.

I changed it to "x=notfunny(x);" and has the same result.

August 03, 2022

On Wednesday, 3 August 2022 at 18:33:37 UTC, pascal111 wrote:

>

I changed it to "x=notfunny(x);" and has the same result.

Now you are changing the value of the temporary loop variable that is still immediately discarded afterwards.

You should return a new range that has the values you want, not try to mutate the range you're given.

August 03, 2022

On Wednesday, 3 August 2022 at 18:53:35 UTC, jfondren wrote:

>

On Wednesday, 3 August 2022 at 18:33:37 UTC, pascal111 wrote:

>

I changed it to "x=notfunny(x);" and has the same result.

Now you are changing the value of the temporary loop variable that is still immediately discarded afterwards.

You should return a new range that has the values you want, not try to mutate the range you're given.

You should provide a code instead of describing my mistakes!!

August 03, 2022

On Wednesday, 3 August 2022 at 19:11:51 UTC, pascal111 wrote:

>

On Wednesday, 3 August 2022 at 18:53:35 UTC, jfondren wrote:

>

On Wednesday, 3 August 2022 at 18:33:37 UTC, pascal111 wrote:

>

I changed it to "x=notfunny(x);" and has the same result.

Now you are changing the value of the temporary loop variable that is still immediately discarded afterwards.

You should return a new range that has the values you want, not try to mutate the range you're given.

You should provide a code instead of describing my mistakes!!

No need for a code. See, there is a keyword called ref, that can be used both in function parameters and in foreach loops, and it is the equivalent of a pointer to the type specified, but it is automatically dereferenced. As arrays are themselves reference types, you don't need to change the parameter from Range range to ref Range range.

On the other hand, int in it of itself isn't a reference type, meaning that to modify the actual value, you need to change the foreach loop to be:

foreach(ref x; range)
    x = notfunny(x);

Proof: I tested it, and it works.

August 03, 2022

On Wednesday, 3 August 2022 at 19:11:51 UTC, pascal111 wrote:

>

On Wednesday, 3 August 2022 at 18:53:35 UTC, jfondren wrote:

>

On Wednesday, 3 August 2022 at 18:33:37 UTC, pascal111 wrote:

>

I changed it to "x=notfunny(x);" and has the same result.

Now you are changing the value of the temporary loop variable that is still immediately discarded afterwards.

You should return a new range that has the values you want, not try to mutate the range you're given.

You should provide a code instead of describing my mistakes!!

I agree to the extent that I wanted to do so, and might still have if a delivery hadn't interrupted by post. But one way to encourage others to put in more effort is put in effort yourself. Here you have your first post, where you didn't use 'preview' to check that your formatting worked, or review when linked Markdown guide to see what the problem was when it didn't work. In that post you also didn't describe what the problem was or ask any question, or say what you wanted to accomplish. It wasn't until your second post that it was even clear to me that you wanted to reimplement map. I thought the addition was a placeholder. The thread title is also a placeholder title for a thread.

In your second and third posts, it didn't come across at all that you'd put much thought into your questions before asking them. The second post--just look at your code. There's a function call and data going nowhere. That should be pretty easy to see. For the third post, if you thought that 'foreach' aliased the members of a range to the degree that simple assignment would update the range, you could test that thought in an instant:

$ rdmd --eval 'auto a = [1, 2, 3]; foreach (n; a) n = 0; writeln(a)'
[1, 2, 3]

It seemed to me that with these quickly asked questions that you're treating the thread like an ongoing interpersonal dialogue where questions are cheap. That's fine, really. I didn't mind that you didn't put in a lot of effort because it also didn't require a lot of effort to help you. But if you're going to complain about it, at least put the effort into asking for what you want at the outset.

The last thing that discouraged me from writing a full example is that I wasn't sure that I'd really be helping you. If you're trying to do this, don't you have a range tutorial open? That should already be pretty well-written already. Now that I've helped you with some incidental problems you should get back to the tutorial and see where you got things mixed up. My guess was that you didn't realize you should be returning a new range, and not trying to modify the range you were given.

If you don't have a tutorial open, the first link at https://dlang.org/phobos/std_range.html is pretty good: http://ddili.org/ders/d.en/ranges.html You won't find the exact thing you want to do, but if you put that aside and thoroughly go through that chapter, the only way you won't be able to do what you want to do is if you get caught up again by some other more fundamental issue with D. The worst likely outcome is that you'll get something that works but that isn't a great way to do it. You should post that code here for feedback.

August 04, 2022

On Wednesday, 3 August 2022 at 21:37:50 UTC, jfondren wrote:

>

On Wednesday, 3 August 2022 at 19:11:51 UTC, pascal111 wrote:

>

[...]

I agree to the extent that I wanted to do so, and might still have if a delivery hadn't interrupted by post. But one way to encourage others to put in more effort is put in effort yourself. Here you have your first post, where you didn't use 'preview' to check that your formatting worked, or review when linked Markdown guide to see what the problem was when it didn't work. In that post you also didn't describe what the problem was or ask any question, or say what you wanted to accomplish. It wasn't until your second post that it was even clear to me that you wanted to reimplement map. I thought the addition was a placeholder. The thread title is also a placeholder title for a thread.

[...]

I don't see my post.

« First   ‹ Prev
1 2