www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 1541] New: std.bind is broken?

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1541

           Summary: std.bind is broken?
           Product: D
           Version: 2.004
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: rayerd.wiz gmail.com


import std.bind, std.stdio;
bool less(int a, int b) {
    return a < b;
}
void main() {
    writefln("10 < 20 =? ", less(10, 20));

    auto less10 = bind(&less, _0, 10);
    writefln(" 5 < 10 =? ", less10(5));
}

dmd a.d
C:\D\dmd\bin\..\src\phobos\std\bind.d(839): declaration _D3std4bind9EmptySlot6__ initZ forward declaration -- Thank you for your works! --
Sep 29 2007
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1541






Hi.
I propose to apply a patch to "dmd\src\phobos\std\bind.d" file for this
problem.


--- bind.d      Wed Oct 03 20:39:18 2007
+++ ~bind.d     Wed Oct 03 20:38:20 2007
   -263,7 +263,7   
        }


-       string toString() {
+       char[] toString() {
                return "()";
        }
 }
   -657,7 +657,7   
 bind(&foo, tuple(23, 45))
 ---
 */
-typeof(new BoundFunc!(FT, EmptySlot, Tuple!(ArgList))) bind(FT, ArgList...)(FT
fp, ArgList args) {
+typeof(new BoundFunc!(FT, NullAlias, Tuple!(ArgList))) bind(FT, ArgList...)(FT
fp, ArgList args) {
        auto res = new DerefFunc!(ReturnType!(bind));
        res.fp = fp;
        extractBoundArgs!(0, 0, ArgList)(res.boundArgs, args);


========= main.d for experimental code
import std.stdio, std.bind;
void main()
{
    writefln("-less");
    bool less(int a, int b){return a<b;}
    auto less5 = bind(&less, _0, 5);
    foreach (i; 0..7)
    {
        writefln(i, "<5 ", less5(i));
    }

    writefln("-greater");
    auto greater = bind(&less, _1, _0);
    auto greater3 = bind(greater.ptr, _0, 3);
    foreach (i; 0..7)
    {
        writefln(i, ">3 ", greater3(i));
    }
}

=============
dmd main
C:\D\dmd\bin\..\..\dm\bin\link.exe main,,,user32+kernel32/noi;
main
-less 0<5 true 1<5 true 2<5 true 3<5 true 4<5 true 5<5 false 6<5 false -greater 0>3 false 1>3 false 2>3 false 3>3 false 4>3 true 5>3 true 6>3 true --
Oct 03 2007
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1541






Created an attachment (id=188)
 --> (http://d.puremagic.com/issues/attachment.cgi?id=188&action=view)
For std.bind in dmd2.004

Hi.
I am Haruki Shigemori.
Because I fixed std.bind for dmd2.004, please apply this patch to bind.d!

=========================================
I:\D\projects\bind>type main.d 
import std.stdio, std.bind;
void main()
{
    writefln("-less (bind)");
    bool less(int a, int b){return a<b;}
    auto less5 = bind(&less, _0, 5);
    foreach (i; 0..7)
    {
        writefln(i, "<5 ", less5(i));
    }

    writefln("-greater (bind)");
    auto greater = bind(&less, _1, _0);
    auto greater3 = bind(greater.ptr, _0, 3);
    foreach (i; 0..7)
    {
        writefln(i, ">3 ", greater3(i));
    }

    writefln("-less (bindAlias)");
    auto less2 = bindAlias!(less)(_0, 2);
    foreach (i; 0..7)
    {
        writefln(i, "<2 ", less2(i));
    }

    writefln("-greater (bindAlias)");
    auto greater4 = bindAlias!(greater)(_0, 4);
    foreach (i; 0..7)
    {
        writefln(i, ">4 ", greater4(i));
    }

    writefln("-function composition");
    int foo(int i) {return i*2;}
    int bar(int i) {return i*3;}
    auto fooBar = bind(&foo, bind(&bar, _0));
    writefln(fooBar(5));
}

I:\D\projects\bind>dmd main 
C:\D\dmd\bin\..\..\dm\bin\link.exe main,,,user32+kernel32/noi;

I:\D\projects\bind>main
-less (bind)
0<5 true
1<5 true
2<5 true
3<5 true
4<5 true
5<5 false
6<5 false
-greater (bind)
0>3 false
1>3 false
2>3 false
3>3 false
4>3 true
5>3 true
6>3 true
-less (bindAlias)
0<2 true
1<2 true
2<2 false
3<2 false
4<2 false
5<2 false
6<2 false
-greater (bindAlias)
0>4 false
1>4 false
2>4 false
3>4 false
4>4 false
5>4 true
6>4 true
-function composition
30


-- 
Oct 04 2007
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1541






Created an attachment (id=195)
 --> (http://d.puremagic.com/issues/attachment.cgi?id=195&action=view)
Fixed std.bind for dmd2.006


-- 
Oct 17 2007
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1541






When I apply the patches then try compiling the example code, I get:

std\bind.d(991): Error: EmptySlot is not an lvalue


-- 
Dec 09 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1541


rayerd.wiz gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------

           obsolete|                            |

           obsolete|                            |





Created an attachment (id=283)
 --> (http://d.puremagic.com/issues/attachment.cgi?id=283&action=view)
Fixed std.bind for dmd2.022 within limitations

import std.stdio, std.bind;
void main()
{
        writefln("(bind(&less, _0, 5))");
        bool less(int a, int b){return a<b;}
        auto less5 = bind(&less, _0, 5);
        for (int i=0; i<7; ++i)
        {
                writefln(i, "<5 ", less5(i));
        }

        writefln("(bind(&less, 5, _0))");
        auto not_less5 = bind(&less, 5, _0);
        for (int i=0; i<7; ++i)
        {
                writefln(i, ">=6 ", not_less5(i));
        }

        writefln("(bind(&less, 3, 5))");
        auto less_3_5 = bind(&less, 3, 5);
        writefln("3<5 ", less_3_5());

        writefln("(bind(&less, 5, 3))");
        auto less_5_3 = bind(&less, 5, 3);
        writefln("5<3 ", less_5_3());

        writefln("(bind(bind(&less, _1, _0).ptr, _0, 3)");
        auto greater = bind(&less, _1, _0);
        auto greater3 = bind(greater.ptr, _0, 3);
        for (int i=0; i<7; ++i)
        {
                writefln(i, ">3 ", greater3(i));
        }

        writefln("(bindAlias!(less)(_0, 2))");
        auto less2 = bindAlias!(less)(_0, 2);
        for (int i=0; i<7; ++i)
        {
                writefln(i, "<2 ", less2(i));
        }

        writefln("(bind(&foo, bind(&bar, _0)))");
        int foo(int i) {return i*2;}
        int bar(int i) {return i*3;}
        auto fooBar = bind(&foo, bind(&bar, _0));
        writefln(fooBar(5));

        //
        // Limitations
        //

//      writefln("(bindAlias!(greater)(_0, 4))");
//      auto greater4 = bindAlias!(greater)(_0, 4);
//      for (int i=0; i<7; ++i)
//      {
//              writefln(i, ">4 ", greater4(i));
//      }

//      auto fooFooBar = bindAlias!(foo)(bindAlias!(fooBar)(_0));
//      auto fooFooBar = bindAlias!(foo)(bind(fooBar.ptr, _0));
//      writefln(fooFooBar(5));
}

I:\bind_repair>main
(bind(&less, _0, 5))
0<5 true
1<5 true
2<5 true
3<5 true
4<5 true
5<5 false
6<5 false
(bind(&less, 5, _0))
0>=6 false
1>=6 false
2>=6 false
3>=6 false
4>=6 false
5>=6 false
6>=6 true
(bind(&less, 3, 5))
3<5 true
(bind(&less, 5, 3))
5<3 false
(bind(bind(&less, _1, _0).ptr, _0, 3)
0>3 false
1>3 false
2>3 false
3>3 false
4>3 true
5>3 true
6>3 true
(bindAlias!(less)(_0, 2))
0<2 true
1<2 true
2<2 false
3<2 false
4<2 false
5<2 false
6<2 false
(bind(&foo, bind(&bar, _0)))
30


-- 
Dec 18 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1541


rayerd.wiz gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |site.puremagic.com brianguer
                   |                            |tin.com





*** Bug 2602 has been marked as a duplicate of this bug. ***


-- 
Jan 22 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1541


rayerd.wiz gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------

        description|within limitations          |within limitations




-- 
Jan 22 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1541


Andrei Alexandrescu <andrei metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |andrei metalanguage.com
         Resolution|                            |WONTFIX



14:34:46 PST ---
std.bind has been eliminated from Phobos. Sorry, Haruki!

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 08 2011
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1541




PST ---

 std.bind has been eliminated from Phobos. Sorry, Haruki!
Don't worry, we have closure. std.bind is already unnecessary one! -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jan 08 2011