www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - D2 map trouble

reply "Nick Sabalausky" <a a.a> writes:
Trying to convert some D1 code to D2:

On 2.047, I'm trying to do this:

import std.string;
void foo(string str)
{
str =
  std.algorithm.map!(
   (char a) { return inPattern(a, [digits, letters])? a : '_'; }
  )(str);
}

And I'm getting:

delegate std.algorithm.__dgliteral1 cannot access frame of function 
__dgliteral1

What's going on? How do I do it right? I figure I probably have some sort of 
problem with strings being immutable(char)[] instead of char[], but it 
doesn't look like that's the issue it's complaining about. Also, in this 
particular case, I'm not concerned about multi-unit UTF-8 characters.
Jul 27 2010
next sibling parent reply "Nick Sabalausky" <a a.a> writes:
"Nick Sabalausky" <a a.a> wrote in message 
news:i2no7g$euv$1 digitalmars.com...
 Trying to convert some D1 code to D2:

 On 2.047, I'm trying to do this:

 import std.string;
 void foo(string str)
 {
 str =
  std.algorithm.map!(
   (char a) { return inPattern(a, [digits, letters])? a : '_'; }
  )(str);
 }

 And I'm getting:

 delegate std.algorithm.__dgliteral1 cannot access frame of function 
 __dgliteral1

 What's going on? How do I do it right? I figure I probably have some sort 
 of problem with strings being immutable(char)[] instead of char[], but it 
 doesn't look like that's the issue it's complaining about. Also, in this 
 particular case, I'm not concerned about multi-unit UTF-8 characters.

In my particular case, I've just switched to regex: import std.regex; str = replace(str, regex("[^a-zA-Z0-9]"), "_"); But I am still curious to hear what exactly was going on with map.
Jul 27 2010
parent Dmitry Olshansky <dmitry.olsh gmail.com> writes:
On 28.07.2010 3:06, Nick Sabalausky wrote:
 "Nick Sabalausky"<a a.a>  wrote in message
 news:i2no7g$euv$1 digitalmars.com...
    
 Trying to convert some D1 code to D2:

 On 2.047, I'm trying to do this:

 import std.string;
 void foo(string str)
 {
 str =
   std.algorithm.map!(
    (char a) { return inPattern(a, [digits, letters])? a : '_'; }
   )(str);
 }

 And I'm getting:

 delegate std.algorithm.__dgliteral1 cannot access frame of function
 __dgliteral1

 What's going on? How do I do it right? I figure I probably have some sort
 of problem with strings being immutable(char)[] instead of char[], but it
 doesn't look like that's the issue it's complaining about. Also, in this
 particular case, I'm not concerned about multi-unit UTF-8 characters.


      

import std.regex; str = replace(str, regex("[^a-zA-Z0-9]"), "_"); But I am still curious to hear what exactly was going on with map.

the problem with map http://www.digitalmars.com/d/archives/digitalmars/D/Improving_std.algorithm.find_113545.html#N113558 -- Dmitry Olshansky
Jul 27 2010
prev sibling next sibling parent reply Philippe Sigaud <philippe.sigaud gmail.com> writes:
--001636c5b4b8a67aea048c727a3f
Content-Type: text/plain; charset=ISO-8859-1

On Wed, Jul 28, 2010 at 01:06, Nick Sabalausky <a a.a> wrote:

 "Nick Sabalausky" <a a.a> wrote in message
 news:i2no7g$euv$1 digitalmars.com...
 Trying to convert some D1 code to D2:

 On 2.047, I'm trying to do this:

 import std.string;
 void foo(string str)
 {
 str =
  std.algorithm.map!(
   (char a) { return inPattern(a, [digits, letters])? a : '_'; }
  )(str);
 }

 And I'm getting:

 delegate std.algorithm.__dgliteral1 cannot access frame of function
 __dgliteral1

 What's going on? How do I do it right? I figure I probably have some sort
 of problem with strings being immutable(char)[] instead of char[], but it
 doesn't look like that's the issue it's complaining about. Also, in this
 particular case, I'm not concerned about multi-unit UTF-8 characters.

In my particular case, I've just switched to regex: import std.regex; str = replace(str, regex("[^a-zA-Z0-9]"), "_"); But I am still curious to hear what exactly was going on with map.

Either returning a map with an anonymous function or using it as you do. I gather the Map template in std.algo is unable to have access to your closure literal, as it is inside foo. A possible workaround is having the anonymous function as a standard, named, free function and use this inside foo. But in that case, why have anonymous functions in D? Another is to use 'string functions', I think. I can test right now, but something like might work: void foo(string str) { str = std.algorithm.map!q{ inPattern(a, [digits, letters])? a : '_'; } (str); } But then I guess inPattern must be visible from std.algorithm. So my current conclusion it that it's more a limitation of anonymous closures than a limitation in map. Philippe --001636c5b4b8a67aea048c727a3f Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_quote">On Wed, Jul 28, 2010 at 01:06, Nick Sabalausky <= span dir=3D"ltr">&lt;a a.a&gt;</span> wrote:<br><blockquote class=3D"gmail_= quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1= ex;"> &quot;Nick Sabalausky&quot; &lt;a a.a&gt; wrote in message<br> news:i2no7g$euv$1 digitalmars.com...<br> <div><div></div><div class=3D"h5">&gt; Trying to convert some D1 code to D2= :<br> &gt;<br> &gt; On 2.047, I&#39;m trying to do this:<br> &gt;<br> &gt; import std.string;<br> &gt; void foo(string str)<br> &gt; {<br> &gt; str =3D<br> &gt; =A0std.algorithm.map!(<br> &gt; =A0 (char a) { return inPattern(a, [digits, letters])? a : &#39;_&#39;= ; }<br> &gt; =A0)(str);<br> &gt; }<br> &gt;<br> &gt; And I&#39;m getting:<br> &gt;<br> &gt; delegate std.algorithm.__dgliteral1 cannot access frame of function<br=

&gt;<br> &gt; What&#39;s going on? How do I do it right? I figure I probably have so= me sort<br> &gt; of problem with strings being immutable(char)[] instead of char[], but= it<br> &gt; doesn&#39;t look like that&#39;s the issue it&#39;s complaining about.= Also, in this<br> &gt; particular case, I&#39;m not concerned about multi-unit UTF-8 characte= rs.<br> &gt;<br> &gt;<br> <br> </div></div>In my particular case, I&#39;ve just switched to regex:<br> <br> import std.regex;<br> str =3D replace(str, regex(&quot;[^a-zA-Z0-9]&quot;), &quot;_&quot;);<br> <br> But I am still curious to hear what exactly was going on with map.<br> <br> <br></blockquote><div>=A0</div></div>It&#39;s an error I get on a weekly ba= sis :-(<div>Either returning a map with an anonymous function or using it a= s you do. I gather the Map template in std.algo is unable to have access to= your closure literal, as it is inside foo.<br> <div><br><div>A possible workaround is having the anonymous function as a s= tandard, named, free function and use this inside foo. But in that case, wh= y have anonymous functions in D?</div><div>Another is to use &#39;string fu= nctions&#39;, I think. I can test right now, but something like might work:= </div> <div><br></div><div>void foo(string str)<br>{<br>=A0str =3D<br>=A0std.algor= ithm.map!q{<br>=A0=A0inPattern(a, [digits, letters])? a : &#39;_&#39;;=A0</= div><div>}<br>=A0=A0(str);<br>}</div></div></div><div><br></div><div>But th= en I guess inPattern must be visible from std.algorithm.</div> <div><br></div><div>So my current conclusion it that it&#39;s more a limita= tion of anonymous closures than a limitation in map.</div><div><br></div><d= iv>Philippe</div><div><br></div> --001636c5b4b8a67aea048c727a3f--
Jul 28 2010
parent "Nick Sabalausky" <a a.a> writes:
"Philippe Sigaud" <philippe.sigaud gmail.com> wrote in message 
news:mailman.32.1280323222.13841.digitalmars-d-learn puremagic.com...
 void foo(string str)
 {
 str =
 std.algorithm.map!q{
  inPattern(a, [digits, letters])? a : '_';
 }
  (str);
 }

 But then I guess inPattern must be visible from std.algorithm.

 So my current conclusion it that it's more a limitation of anonymous
 closures than a limitation in map.

*sigh*, If D allowed something like this: mixin string map() { return "code here"; } map(); As an alternative to or intead of this: string map() { return "code here"; } mixin(map()); As I've suggested before (it only got accepted for template mixins, not string mixins), then map's string-style predicates could be made to be evaluated in the proper context without screwing up map's standard interface by requiring it to be called with "mixin(...)".
Jul 28 2010
prev sibling next sibling parent reply Pelle <pelle.mansson gmail.com> writes:
On 07/28/2010 12:57 AM, Nick Sabalausky wrote:
 Trying to convert some D1 code to D2:

 On 2.047, I'm trying to do this:

 import std.string;
 void foo(string str)
 {
 str =
    std.algorithm.map!(
     (char a) { return inPattern(a, [digits, letters])? a : '_'; }
    )(str);
 }

 And I'm getting:

 delegate std.algorithm.__dgliteral1 cannot access frame of function
 __dgliteral1

 What's going on? How do I do it right? I figure I probably have some sort of
 problem with strings being immutable(char)[] instead of char[], but it
 doesn't look like that's the issue it's complaining about. Also, in this
 particular case, I'm not concerned about multi-unit UTF-8 characters.

auto fn = (char a) { ... }; str = map!fn(str);
Jul 28 2010
parent bearophile <bearophileHUGS lycos.com> writes:
Philippe Sigaud:
 Is it on bugzilla?

If not present you can add it here: http://d.puremagic.com/issues/show_bug.cgi?id=4264 Bye, bearophile
Jul 28 2010
prev sibling parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
--00032555a42e93c4c6048c740d48
Content-Type: text/plain; charset=ISO-8859-1

On Wed, Jul 28, 2010 at 16:51, Pelle <pelle.mansson gmail.com> wrote:

 This is a compiler bug. Easy workaround:

 auto fn = (char a) { ... };
 str = map!fn(str);

Is it on bugzilla? Philippe --00032555a42e93c4c6048c740d48 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_quote">On Wed, Jul 28, 2010 at 16:51, Pelle <span dir= =3D"ltr">&lt;<a href=3D"mailto:pelle.mansson gmail.com">pelle.mansson gmail= .com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"ma= rgin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"> <div><div></div><div class=3D"h5">This is a compiler bug. Easy workaround:<= /div></div> <br> auto fn =3D (char a) { ... };<br> str =3D map!fn(str);<br> </blockquote></div><br><div>Is it on bugzilla?</div><div><br></div><div>Phi= lippe</div><div><br></div> --00032555a42e93c4c6048c740d48--
Jul 28 2010