Re: How to instantiate a map with multiple functions

2015-12-27 Thread karthikeyan via Digitalmars-d-learn

On Sunday, 27 December 2015 at 02:21:11 UTC, Ali Çehreli wrote:

On 12/26/2015 05:26 PM, Karthikeyan wrote:

> if I need to map on a array of tuples will that work with the
tuple being
> unpacked or do I need to get it as single element and do
unpacking myself?

Unfortunately, there is no automatic unpacking of tuples.

The only exception that I know is when tuples are elements of a 
range (but not a proper slice, in which case the first element 
is the automatic element index).


import std.stdio;
import std.typecons;
import std.range;
import std.algorithm;

void main() {
auto range = 5.iota.map!(i => tuple(2 * i, i * i));

// automatic tuple expansion:
foreach (twice, square; range) {
writefln("twice: %s, square: %s", twice, square);
}
}

Prints:

twice: 0, square: 0
twice: 2, square: 1
twice: 4, square: 4
twice: 6, square: 9
twice: 8, square: 16

The problem is when the same elements are inside a slice:

import std.stdio;
import std.typecons;
import std.range;
import std.algorithm;

void main() {
auto range = [ tuple(0, 0), tuple(2, 1) ];

foreach (twice, square; range) {
writefln("twice: %s, square: %s", twice, square);
}
}

Now 'twice' is the automatic index, and 'square' is the entire 
element (i.e. the tuple):


twice: 0, square: Tuple!(int, int)(0, 0)
twice: 1, square: Tuple!(int, int)(2, 1)

Ali


I took a local copy of the std.algorithm.iteration as 
myiteration.d and used it for debugging. Commenting out the 
following two lines make this work 
https://github.com/D-Programming-Language/phobos/blob/master/std/algorithm/iteration.d#L461-L463. I don't know why that fails. I filed an issue at the bug tracker.


Re: basic interactive readf from stdin

2015-12-26 Thread Karthikeyan via Digitalmars-d-learn

On Sunday, 27 December 2015 at 02:08:05 UTC, Ali Çehreli wrote:

On 12/26/2015 05:15 PM, Karthikeyan wrote:

>> The answer is nine chapters later. :) (Use readln() and
strip() (or
>> chomp())).
>>
>>   http://ddili.org/ders/d.en/strings.html
>>
>> Ali
>
> Many thanks Ali. The book says ctrl + D to end input. But I
used two
> enters to get the output. Any idea why?

I guess that means that my understanding was not portable. It 
requires Ctrl-D on my console environment on Linux. No matter 
how many Enters I enter :p they become parts of the same string.


Ali


:) I was on zsh with gnome terminal alike on Linux Mint 15. 
Thanks for clearing that up.




Re: How to instantiate a map with multiple functions

2015-12-26 Thread Karthikeyan via Digitalmars-d-learn

On Sunday, 27 December 2015 at 00:27:12 UTC, Ali Çehreli wrote:

On 12/26/2015 11:46 AM, karthikeyan wrote:

> Thanks but the following returns an error for me
>
>import std.algorithm.comparison : equal;
>import std.range : chain;
>int[] arr1 = [ 1, 2, 3, 4 ];
>int[] arr2 = [ 5, 6 ];
>auto dd = map!(z => z * z, c => c * c * c)(chain(arr1,
arr2));
>writeln(dd);
>
> Error :
>
> /usr/include/dmd/phobos/std/meta.d(546): Error: template
instance
> F!(__lambda2) cannot use local '__lambda2' as parameter to
non-global
> template AppliedReturnType(alias f)

That looks like a bug to me. Please report here:

  https://issues.dlang.org/

You can call tuple() as a workaround:

import std.stdio;
import std.algorithm;
import std.range;
import std.typecons;

void main() {
int[] arr1 = [ 1, 2, 3, 4 ];
int[] arr2 = [ 5, 6 ];
auto dd = map!(z => tuple(z * z, z * z * z))(chain(arr1, 
arr2));

writeln(dd);
}

Ali


Thanks Ali. I think it's been around like this for a long time. I 
searched issue tracker but no issues of this type. Also if I need 
to map on a array of tuples will that work with the tuple being 
unpacked or do I need to get it as single element and do 
unpacking myself? Sorry on mobile so couldn't check.


Re: basic interactive readf from stdin

2015-12-26 Thread Karthikeyan via Digitalmars-d-learn

On Sunday, 27 December 2015 at 00:20:51 UTC, Ali Çehreli wrote:

On 12/26/2015 12:11 PM, karthikeyan wrote:

> I read http://ddili.org/ders/d.en/input.html and inserted a
space before %s
> but still no use. Am I missing something here with the latest
version?

The answer is nine chapters later. :) (Use readln() and strip() 
(or chomp())).


  http://ddili.org/ders/d.en/strings.html

Ali


Many thanks Ali. The book says ctrl + D to end input. But I used 
two enters to get the output. Any idea why? The book was great. 
Thanks a lot.




Re: basic interactive readf from stdin

2015-12-26 Thread karthikeyan via Digitalmars-d-learn
On Saturday, 26 December 2015 at 19:52:15 UTC, Adam D. Ruppe 
wrote:
On Saturday, 26 December 2015 at 19:40:59 UTC, Jay Norwood 
wrote:

Simple VS console app in D.


If you are running inside visual studio, you need to be aware 
that output will be block buffered, not line buffered, because 
VS pipes the output making the program think it is talking to 
another program instead of to an interactive console (well, 
because it is!)


Add a stdout.flush(); after writing to force it to show 
immediately. I really think the read functions ought to flush 
output too because this is such a FAQ. (indeed, my terminal.d 
does flush output when you request input)


I experience the same as the OP on Linux Mint 15 with dmd2.069 
and 64 bit machine.  I have to press enter twice to get the 
output. I read http://ddili.org/ders/d.en/input.html and inserted 
a space before %s but still no use. Am I missing something here 
with the latest version?


Code

import std.stdio;

int main(string[] argv)
{
  string nm;
  readf(" %s\n",&nm);
  writeln("nm:",nm);
  // readf(" %s\n",&nm);
  // writeln("nm:",nm);
  return 0;
}


Output

56
2
nm:56


Re: How to instantiate a map with multiple functions

2015-12-26 Thread karthikeyan via Digitalmars-d-learn

On Saturday, 26 December 2015 at 19:38:16 UTC, Fusxfaranto wrote:
On Saturday, 26 December 2015 at 19:30:24 UTC, karthikeyan 
wrote:
How to instantiate a map with multiple functions. I looked 
into the docs at 
http://dlang.org/phobos/std_algorithm_iteration.html#map. They 
contain a string which I suppose is a mixin and when I change 
"a" to some other name it results in an error for me. Are 
there any ways to use lambda functions directly instead of 
strings and any explanation of the strings used in the map 
example and why a is used will be helpful.


I tried reading the source 
https://github.com/D-Programming-Language/phobos/blob/master/std/algorithm/iteration.d#L520 .Some hint that "a" should be used at https://github.com/D-Programming-Language/phobos/blob/master/std/functional.d#L101 but how do I change that since map doesn't allow me any params to specify the parameter name. Also how can I map an array of tuples with two or more elements with a function of two or more params like unpack the tuple into a function like that. I am D beginner so any will insights will be very helpful for me.


I am using dmd version 2.069 on Linux Mint 15 - 64bit


You should be able to just use any function (including lambdas) 
as a template argument to map.  The string version is from 
before the more concise "=>" lambda syntax was developed, and 
generally isn't what you want to use nowadays.


Thanks but the following returns an error for me

  import std.algorithm.comparison : equal;
  import std.range : chain;
  int[] arr1 = [ 1, 2, 3, 4 ];
  int[] arr2 = [ 5, 6 ];
  auto dd = map!(z => z * z, c => c * c * c)(chain(arr1, arr2));
  writeln(dd);

Error :

/usr/include/dmd/phobos/std/meta.d(546): Error: template instance 
F!(__lambda2) cannot use local '__lambda2' as parameter to 
non-global template AppliedReturnType(alias f)
/usr/include/dmd/phobos/std/meta.d(552): Error: template instance 
maps_square.main.staticMap!(AppliedReturnType, __lambda2) error 
instantiating
/usr/include/dmd/phobos/std/algorithm/iteration.d(447):
instantiated from here: staticMap!(AppliedReturnType, __lambda2, 
__lambda3)

maps_square.d(71):instantiated from here: map!(Result)
/usr/include/dmd/phobos/std/meta.d(546): Error: template instance 
F!(__lambda3) cannot use local '__lambda3' as parameter to 
non-global template AppliedReturnType(alias f)
/usr/include/dmd/phobos/std/meta.d(553): Error: template instance 
maps_square.main.staticMap!(AppliedReturnType, __lambda3) error 
instantiating
/usr/include/dmd/phobos/std/algorithm/iteration.d(447):
instantiated from here: staticMap!(AppliedReturnType, __lambda2, 
__lambda3)

maps_square.d(71):instantiated from here: map!(Result)

Am I missing something here over how the lambda notation can be 
used? I personally prefer the lambda notation to be clear than 
passing strings as arguments. Kindly help me on this.


How to instantiate a map with multiple functions

2015-12-26 Thread karthikeyan via Digitalmars-d-learn
How to instantiate a map with multiple functions. I looked into 
the docs at 
http://dlang.org/phobos/std_algorithm_iteration.html#map. They 
contain a string which I suppose is a mixin and when I change "a" 
to some other name it results in an error for me. Are there any 
ways to use lambda functions directly instead of strings and any 
explanation of the strings used in the map example and why a is 
used will be helpful.


I tried reading the source 
https://github.com/D-Programming-Language/phobos/blob/master/std/algorithm/iteration.d#L520 .Some hint that "a" should be used at https://github.com/D-Programming-Language/phobos/blob/master/std/functional.d#L101 but how do I change that since map doesn't allow me any params to specify the parameter name. Also how can I map an array of tuples with two or more elements with a function of two or more params like unpack the tuple into a function like that. I am D beginner so any will insights will be very helpful for me.


I am using dmd version 2.069 on Linux Mint 15 - 64bit