Jump to page: 1 2
Thread overview
Mapping with partial
Mar 30, 2015
matovitch
Mar 30, 2015
matovitch
Mar 30, 2015
Adam D. Ruppe
Mar 30, 2015
matovitch
Mar 30, 2015
Adam D. Ruppe
Mar 30, 2015
matovitch
Mar 30, 2015
anonymous
Mar 30, 2015
matovitch
Mar 30, 2015
anonymous
Mar 30, 2015
matovitch
Mar 30, 2015
anonymous
Mar 30, 2015
matovitch
Mar 30, 2015
matovitch
Mar 30, 2015
anonymous
Mar 30, 2015
matovitch
Mar 31, 2015
ketmar
March 30, 2015
Hi again,

I have this simple toy code :

import point;
import std.random;
import std.algorithm;
import std.functional;

void getRandomPoint(R)(R randVar, ref Point p)
{
      p.x = randVar;
      p.y = randVar;
      p.z = randVar;
}

void main()
{
      Point[500] points;
      auto randVar = uniform(0.0f, 1.0f);

      alias test = partial!(getRandomPoint, randVar);

      points.map!(test);
}

And get the following error :

kmeans_example.d(79): Error: template std.algorithm.iteration.map
cannot deduce function from argument types
!(partial)(Point[500]), candidates are:
/usr/include/dmd/phobos/std/algorithm/iteration.d(434):
std.algorithm.iteration.map(fun...) if (fun.length >= 1)

Btw, I don't understand why :

auto test = partial!(getRandomPoint, randVar);

dont work...partial doesnt return a delegate it seems...:/
March 30, 2015
(it's not on line 79 obviously, you got me :D)
March 30, 2015
On Monday, 30 March 2015 at 18:07:18 UTC, matovitch wrote:
> kmeans_example.d(79): Error: template std.algorithm.iteration.map

That error is easy: use points[].map!(test) instead of points.map.

Since points is a static array, it isn't a range. Static arrays can't be popped through. But if you slice it, then it yields a usable range for map.


The other problem though is the partial!(). It expects a template argument for the thing so it can make a new function right there at compile time... which doesn't work with a runtime variable.

The way I'd do it is just with a little hand written delegate. This will compile, for example:

      auto test = (ref Point p) => getRandomPoint(randVar, p);
      points[].map!(test);


and should do what you need.
March 30, 2015
On Monday, 30 March 2015 at 18:23:32 UTC, Adam D. Ruppe wrote:
> On Monday, 30 March 2015 at 18:07:18 UTC, matovitch wrote:
>> kmeans_example.d(79): Error: template std.algorithm.iteration.map
>
> That error is easy: use points[].map!(test) instead of points.map.
>
> Since points is a static array, it isn't a range. Static arrays can't be popped through. But if you slice it, then it yields a usable range for map.
>
>
> The other problem though is the partial!(). It expects a template argument for the thing so it can make a new function right there at compile time... which doesn't work with a runtime variable.
>
> The way I'd do it is just with a little hand written delegate. This will compile, for example:
>
>       auto test = (ref Point p) => getRandomPoint(randVar, p);
>       points[].map!(test);
>
>
> and should do what you need.

Nice ! Thanks for the tip ! I tried importing std.range and points.array works too.
March 30, 2015
On Monday, 30 March 2015 at 18:29:32 UTC, matovitch wrote:
> I tried importing std.range and points.array works too.

Aye, that would work too, but the slice I think is more efficient as I'm pretty sure... not completely sure, but I think .array makes a copy of static arrays, whereas the slice doesn't.
March 30, 2015
On Monday, 30 March 2015 at 18:34:19 UTC, Adam D. Ruppe wrote:
> On Monday, 30 March 2015 at 18:29:32 UTC, matovitch wrote:
>> I tried importing std.range and points.array works too.
>
> Aye, that would work too, but the slice I think is more efficient as I'm pretty sure... not completely sure, but I think .array makes a copy of static arrays, whereas the slice doesn't.

I was going to ask you the question does it just add the range shell or does it make a copy ? :/ Maybe someone else know.
March 30, 2015
Well I have a bit of a similar problem with foreach.

If I use classic T[] range, I can do :

foreach(int i, auto t, myRange)...

But if I use an Array!T (from std.container) I get :

cannot infer argument types, expected 1 argument, not 2

Even if I add the brackets []. Any idea ? Thanks for your help ! :)
March 30, 2015
On Monday, 30 March 2015 at 18:37:53 UTC, matovitch wrote:
> On Monday, 30 March 2015 at 18:34:19 UTC, Adam D. Ruppe wrote:
[...]
>> Aye, that would work too, but the slice I think is more efficient as I'm pretty sure... not completely sure, but I think .array makes a copy of static arrays, whereas the slice doesn't.
>
> I was going to ask you the question does it just add the range shell or does it make a copy ? :/ Maybe someone else know.

Let's check the documentation. http://dlang.org/phobos/std_array.html#array says: "Allocates an array and initializes it with copies of the elements of range r." Documentation says copy.

Let's check the actual behaviour.
----
void main()
{
	int[1] a = [1];
	import std.array: array;
	a.array[0] = 2;
	import std.stdio: writeln;
	writeln(a[0]);
}
----
(also at <http://dpaste.dzfl.pl/1191144a9acf>)
This program prints "1". That's the output we'd expect when `array` makes a copy. Actual behaviour says copy.

So, copy.
March 30, 2015
On Monday, 30 March 2015 at 19:08:24 UTC, anonymous wrote:
> On Monday, 30 March 2015 at 18:37:53 UTC, matovitch wrote:
>> On Monday, 30 March 2015 at 18:34:19 UTC, Adam D. Ruppe wrote:
> [...]
>>> Aye, that would work too, but the slice I think is more efficient as I'm pretty sure... not completely sure, but I think .array makes a copy of static arrays, whereas the slice doesn't.
>>
>> I was going to ask you the question does it just add the range shell or does it make a copy ? :/ Maybe someone else know.
>
> Let's check the documentation. http://dlang.org/phobos/std_array.html#array says: "Allocates an array and initializes it with copies of the elements of range r." Documentation says copy.
>
> Let's check the actual behaviour.
> ----
> void main()
> {
> 	int[1] a = [1];
> 	import std.array: array;
> 	a.array[0] = 2;
> 	import std.stdio: writeln;
> 	writeln(a[0]);
> }
> ----
> (also at <http://dpaste.dzfl.pl/1191144a9acf>)
> This program prints "1". That's the output we'd expect when `array` makes a copy. Actual behaviour says copy.
>
> So, copy.

That settle the point for array as for [] ? I guess the documentation should have something to say about it too. ;)
March 30, 2015
On Monday, 30 March 2015 at 19:11:10 UTC, matovitch wrote:
> That settle the point for array as for [] ?

I though that was clear. [] doesn't copy.

> I guess the documentation should have something to say about it too. ;)

hopefully
« First   ‹ Prev
1 2