Re: More type-flexible arrays?

2015-06-15 Thread Ozan via Digitalmars-d-learn

On Sunday, 14 June 2015 at 06:46:05 UTC, Ali Çehreli wrote:

On 06/13/2015 11:12 PM, Ozan wrote:

 Hallo!

 Is it possible to create arrays which has more then one type,

Not possible.

 f. ex. array[0] = 1; array[1] = z; array[2] = new clazz(),


 I tried Variant, but it slow down heavily my app.

Another option is OOP. Depending on the way they are used in 
the program, those different types have a common interface. It 
is possible to have an array of that interface:


Foo[] foos;

interface Foo
{
void commonFunction();
// ...
}

class clazz : Foo
{
// ...
}

However, fundamental types like int need to be boxed:

class FundamentalFoo(T) : Foo
{
T value;

// ...
}

If needed, you can specialize FundamentalFoo for int, string, 
etc.


Ali


Thanks.
It need some effort but it is a more performant to have a 
multi-type array compared to Variants. And thanks to D's template 
system it isn't so much effort.


Regards Ozan


Re: More type-flexible arrays?

2015-06-15 Thread Ozan via Digitalmars-d-learn

On Sunday, 14 June 2015 at 06:43:48 UTC, ketmar wrote:

On Sun, 14 Jun 2015 06:12:29 +, Ozan wrote:


Hallo!

Is it possible to create arrays which has more then one type,
f. ex. array[0] = 1; array[1] = z; array[2] = new clazz(), 



I tried Variant, but it slow down heavily my app.


it is possible. with Variant. ;-)

chances are that you're doing something by the wrong way in 
your code. not syntactically wrong, but algorithmically wrong. 
why to you need such an array? weak typing is slow. strong 
typing is fast. seems that you are used to scripting languages 
with weak typing, and designed your algorithm with weak typing 
in mind. i think it's better to try to redesign your algorithm, 
not looking for faster weak typing solution.


Thanks for your advise.
You read my mind. It's a strong Groovy background.
I think with D, I'm on right way to solve my personal problems ;-)

Regards Ozan


Re: CPU cores threads fibers

2015-06-15 Thread Robert M. Münch via Digitalmars-d-learn

On 2015-06-14 15:54:30 +, Etienne Cimon said:

Yes, however nothing really guarantees multi-threading = multi-core. 
The kernel reserves the right and will most likely do everything 
possible to keep your process core-local to use caching efficiently.


Hi, sure. It's more about raising the chance to use the cores ;-)

--
Robert M. Münch
http://www.saphirion.com
smarter | better | faster



oauth, Twitter, and std.net.curl

2015-06-15 Thread Taylor Gronka via Digitalmars-d-learn

Hello,

I've picked up a web design project, and I would love to use 
vibe.d/dlang. I need to use oauth to search on a number of web 
api's, such as searching for Twitter tweets.


I tried using Adam's oauth (Thanks!). However, it doesn't appear 
support the validation needed for searching Twitter.

https://github.com/adamdruppe/arsd/blob/master/oauth.d


Now I'm looking at std.net.curl, and this is my thought: is 
HTTP.addRequestHeader(key, val) able to set the oauth 
authentication headers? It would look a bit odd, but if something 
like this works, then it wouldn't be difficult to code for each 
set of headers specifically:


auto client = HTTP(https://api.twitter.com;); // not sure if 
https should be set here or somewhere else

string bearerUrl = https://api.twitter.com/oauth2/token;;
client.addRequestHeader(Authorization, Basic 
eHZ6MWV2RlM0d0VFUFRHRUZQSEJvZzpMOHFxOVBaeVJnNmllS0dFS2hab2xHQzB2SldMdzhpRUo4OERSZHlPZw);

client.postData(grant_type=client_credentials);
client.perform();

(The above example is based on Step 2 of this page)
https://dev.twitter.com/oauth/application-only


Of course, the oauth headers look a bit different, but I imagine 
it being something like this:
client.addRequestHeader(Authorization, OAuth2 token: 
\string\, tokensecret: \string\, appkey, \string\ ...);

\\ not sure if that's how to escape quotes in dlang


Can I get some guidance on this? I would really like to use 
dlang, but I can't afford to be stuck on this for too long. I'd 
be happy to try and write a modification of Adam's code, but I 
lack experience, so it's a slow process for me.


I'd also be happy to write up little tutorials/blogposts about 
how I accomplish various tasks.


Thanks,





Re: Casting MapResult

2015-06-15 Thread jmh530 via Digitalmars-d-learn

Thank you all for the very fast answers. It looks like that works.


dmd/druntime/phobos HEAD: can't link binaries on Arch Linux

2015-06-15 Thread Atila Neves via Digitalmars-d-learn

Anyone else getting this problem on Arch Linux?

dmd hello.d

hello.o:hello.d:TypeInfo_S3std3uni38__T13InversionListTS3std3uni8GcPolicyZ13InversionList67__T9IntervalsTS3std3uni32__T8CowArrayTS3std3uni8GcPolicyZ8CowArrayZ9Intervals.init$:
 error: undefined reference to 
'std.uni.InversionList!(std.uni.GcPolicy).InversionList.Intervals!(std.uni.CowArray!(std.uni.GcPolicy).CowArray).Intervals.__fieldPostblit()'
collect2: error: ld returned 1 exit status
--- errorlevel 1

With -v I see that it's compiling phobos in by calling gcc like 
this:


-l:libphobos2.a

When I replace that with just the path to phobos instead, it 
works.



Atila


Re: Casting MapResult

2015-06-15 Thread ketmar via Digitalmars-d-learn
On Mon, 15 Jun 2015 15:10:20 +, jmh530 wrote:

you shouldn't cast it like that. use `std.array.array` to get the actual 
array. like this:

  import std.array;

  auto y = x.map!(a = exp(a)).array;

the thing is that `map` returns so-called lazy range. lazy ranges 
trying to not do any work until they are explicitely asked. i.e.

  y = x.map!(a = exp(a))

doesn't do any real processing yet, it only prepares everything for it. 
and only when you're calling `y.front`, `map` is processing one element. 
only one, as it has no need to process next until you call `popFront`.

tl;dr: you can't simply cast that lazy range back to array, you have to 
use `std.std.array` to get the array from it.

signature.asc
Description: PGP signature


Re: Process a TypeTuple

2015-06-15 Thread Justin Whear via Digitalmars-d-learn
On Mon, 15 Jun 2015 04:06:12 +, Baz wrote:

 On Monday, 15 June 2015 at 03:53:35 UTC, Yuxuan Shui wrote:
 Is it possible to apply some operation on every member of a TypeTuple,
 then get the result back?

 Say I have a TypeTuple of array types, and I want a TypeTuple of their
 element types, how could I do that?

For this particular example you use std.range.ElementType:

import std.typecons, std.range;
alias Elements = staticMap!(ElementType, MyRangeOrArrayTypes);




Re: oauth, Twitter, and std.net.curl

2015-06-15 Thread Taylor Gronka via Digitalmars-d-learn

You two are phenomenal! Thanks!


Re: oauth, Twitter, and std.net.curl

2015-06-15 Thread Adam D. Ruppe via Digitalmars-d-learn

On Monday, 15 June 2015 at 14:41:32 UTC, Taylor Gronka wrote:
I tried using Adam's oauth (Thanks!). However, it doesn't 
appear support the validation needed for searching Twitter.

https://github.com/adamdruppe/arsd/blob/master/oauth.d


I haven't actually used the twitter search api for like a year 
now... but looking at the docs, it doesn't look like it has 
changed much, you should still be able to get to it by modifying 
the tweet function in my lib.


Though, my thing only really supports the user model (I wrote it 
because we needed to send tweets on behalf of various users) 
rather than the application authorization. But, application auth 
is the easier of the two!


client.addRequestHeader(Authorization, OAuth2 token: 
\string\, tokensecret: \string\, appkey, \string\ ...);

\\ not sure if that's how to escape quotes in dlang


That looks basically right to me, without actually running it, it 
should do what you need for the search.




One thing you could do btw is to get the bearer token manually, 
then copy/paste it right into your code and use it like a 
password. Then the code can skip the Authorization: Basic step 
and only worry about the Bearer part.





Give me a sec, I'll write up an example using just the 
std.net.curl using this strategy.


Re: oauth, Twitter, and std.net.curl

2015-06-15 Thread wobbles via Digitalmars-d-learn

On Monday, 15 June 2015 at 14:41:32 UTC, Taylor Gronka wrote:

Hello,

I've picked up a web design project, and I would love to use 
vibe.d/dlang. I need to use oauth to search on a number of web 
api's, such as searching for Twitter tweets.


I tried using Adam's oauth (Thanks!). However, it doesn't 
appear support the validation needed for searching Twitter.

https://github.com/adamdruppe/arsd/blob/master/oauth.d


Now I'm looking at std.net.curl, and this is my thought: is 
HTTP.addRequestHeader(key, val) able to set the oauth 
authentication headers? It would look a bit odd, but if 
something like this works, then it wouldn't be difficult to 
code for each set of headers specifically:


auto client = HTTP(https://api.twitter.com;); // not sure 
if https should be set here or somewhere else

string bearerUrl = https://api.twitter.com/oauth2/token;;
client.addRequestHeader(Authorization, Basic 
eHZ6MWV2RlM0d0VFUFRHRUZQSEJvZzpMOHFxOVBaeVJnNmllS0dFS2hab2xHQzB2SldMdzhpRUo4OERSZHlPZw);

client.postData(grant_type=client_credentials);
client.perform();

(The above example is based on Step 2 of this page)
https://dev.twitter.com/oauth/application-only


Of course, the oauth headers look a bit different, but I 
imagine it being something like this:
client.addRequestHeader(Authorization, OAuth2 token: 
\string\, tokensecret: \string\, appkey, \string\ ...);

\\ not sure if that's how to escape quotes in dlang


Can I get some guidance on this? I would really like to use 
dlang, but I can't afford to be stuck on this for too long. I'd 
be happy to try and write a modification of Adam's code, but I 
lack experience, so it's a slow process for me.


I'd also be happy to write up little tutorials/blogposts about 
how I accomplish various tasks.


Thanks,


You can use vibe.d to send requests rather than going the 
lowlevel curl library.


Check out the requestHTTP method [1]
From code I've written in the past, your setup looks correct.

This is some vibe.d code I've used to get a refresh token from 
Youtube (I know it's different from twitter, but the idea is the 
same)


YoutubeToken is a local struct that contains the access_token we 
need to perform searches/post videos etc in any subsequent 
requests.


/**
 * Returns a new YoutubeToken
 */
public YoutubeToken getRefreshToken(){
string url = https://accounts.google.com/o/oauth2/token;;

string postBody = 
client_id=$CLIENT_ID$client_secret=$CLIENT_SECRET$refresh_token=$REFRESH_TOKEN$grant_type=$GRANT_TYPE$

.replaceMap(
[
$CLIENT_ID$ : credentials.clientID,
$CLIENT_SECRET$ : credentials.clientSecret,
$REFRESH_TOKEN$ : credentials.refreshToken,
$GRANT_TYPE$ : refresh_token
]
);

logInfo(Getting new refresh token with URL: %s and postBody: 
%s, url, postBody);


auto response = requestHTTP(url,
(scope req){
req.method = HTTPMethod.POST;
req.contentType = application/x-www-form-urlencoded;
req.writeBody(cast(ubyte[])postBody);
}).bodyReader.readAllUTF8().parseJsonString;

return YoutubeToken(response[access_token].get!string, 
response[expires_in].get!long, 
response[token_type].get!string );

}


[1] http://vibed.org/api/vibe.http.client/requestHTTP


Casting MapResult

2015-06-15 Thread jmh530 via Digitalmars-d-learn

I wrote a simple function to apply map to a float dynamic array

auto exp(float[] x) {
auto y = x.map!(a = exp(a));
return y;
}

However, the type of the result is MapResult!(__lambda2, 
float[]). It seems like some of the things that I might do to a 
float[], I can't do to this type, like adding them together. So I 
tried to adjust this by adding in a cast to float[], as in


float[] exp(float[] x) {
auto y = x.map!(a = exp(a));
cast(float[]) y;
return y;
}

But I get an error that I can't convert MapResult!(__lambda2, 
float[]) to float[].


So I suppose I have two questions: 1) am I screwing up the cast, 
or is there no way to convert the MapResult to float[], 2) should 
I just not bother with map (I wrote an alternate, longer, version 
that doesn't use map but returns float[] properly).


Re: oauth, Twitter, and std.net.curl

2015-06-15 Thread Adam D. Ruppe via Digitalmars-d-learn

Here's a working example using std.net.curl to search:

http://arsdnet.net/dcode/twitter.d


I show how you can get a token from the command line and code 
that I think will also work from std.net.curl in the top comment.


Once you get that token, paste it in here, pulling it out of the 
json. Then you can run the code below.



It is pretty simple to use the oauth2 things: you just set that 
authorization header:


client.addRequestHeader(Authorization, Bearer  ~ 
bearer_token);



And be sure to use https in the url, and the rest is just a basic 
http request and read. OAuth 1, needed to tweet for other users, 
is a lot more complicated and that's where you want to look back 
at the library if you need to do that. (My tweet() function in 
there is the one to use.)


Re: Casting MapResult

2015-06-15 Thread Justin Whear via Digitalmars-d-learn
On Mon, 15 Jun 2015 15:10:20 +, jmh530 wrote:

 So I suppose I have two questions: 1) am I screwing up the cast, or is
 there no way to convert the MapResult to float[], 2) should I just not
 bother with map (I wrote an alternate, longer, version that doesn't use
 map but returns float[] properly).

MapResult is a wrapper around your original range that performs the 
mapping operation lazily.  If you want eagerly evaluate and get back to 
an array use the std.array.array function:

import std.array : array;
auto y = x.map!(a = exp(a)).array;

Or if you have already allocated an array of the appropriate size you can 
use std.algorithm.copy:

import std.algorithm : copy;
float[] y = new float[](appropriate_length);
x.map!(a = exp(a)).copy(y);


Re: Casting MapResult

2015-06-15 Thread Adam D. Ruppe via Digitalmars-d-learn

On Monday, 15 June 2015 at 15:10:24 UTC, jmh530 wrote:
So I suppose I have two questions: 1) am I screwing up the 
cast, or is there no way to convert the MapResult to float[]


Don't cast it, just slap a .array on the end after importing 
std.range. Like so:


import std.algorithm;
import std.range; // add this line somewhere
float[] exp2(float[] x) {
auto y = x.map!(a = exp(a));
return y.array; // this line changed to make the array
}


The reason is that map returns a lazy generator instead of an 
array directly. It only evaluates on demand.


To get it to evaluate and save into an array, the .array function 
is called.


Tip though: don't call .array if you don't have to, chaining 
calls to map and such, even foreach(item; some_map_result) can be 
done without actually building the array and can give more 
efficiency.


Re: Casting MapResult

2015-06-15 Thread anonymous via Digitalmars-d-learn

On Monday, 15 June 2015 at 15:10:24 UTC, jmh530 wrote:

float[] exp(float[] x) {
auto y = x.map!(a = exp(a));
cast(float[]) y;
return y;
}

But I get an error that I can't convert MapResult!(__lambda2, 
float[]) to float[].


So I suppose I have two questions: 1) am I screwing up the 
cast, or is there no way to convert the MapResult to float[], 
2) should I just not bother with map (I wrote an alternate, 
longer, version that doesn't use map but returns float[] 
properly).


First off: Don't cast unless you know exactly what you're doing. 
It's easy to stumble into undefined behaviour land with casts.


To answer the question: You can convert from from MapResult to 
float[], but not with a cast. Instead, use std.array.array:

import std.array: array;
return x.map!(std.math.exp).array;


Re: oauth, Twitter, and std.net.curl

2015-06-15 Thread wobbles via Digitalmars-d-learn

On Monday, 15 June 2015 at 15:23:43 UTC, Taylor Gronka wrote:

You two are phenomenal! Thanks!


Oh, replaceMap is a custom funciton I wrote too...
Eases replacing multiple values in a string with 1 function call.

I wont pretend that it's fast as it allocates a lot of memory :)

public string replaceMap(string str, string[string] keys){
import std.array;
foreach(key, val; keys){
str = str.replace(key, val);
}
return str;
}


Re: Casting MapResult

2015-06-15 Thread wobbles via Digitalmars-d-learn

On Monday, 15 June 2015 at 15:10:24 UTC, jmh530 wrote:

snip
float[] exp(float[] x) {
auto y = x.map!(a = exp(a));
cast(float[]) y;
return y;
}




Also, I dont think your functions will work?
Your recursively calling exp in your map, but with a 'float' 
instead of 'float[]'.


Re: compilation issues in a shared library project

2015-06-15 Thread Nicholas Wilson via Digitalmars-d-learn

On Sunday, 7 June 2015 at 00:38:17 UTC, Jonathan Villa wrote:


module dt2.DataBlock;

class DataBlock
{
public DataBlock * NextBlock;
public DataBlock * PrevBlock;
public string value;

this()
{
NextBlock = null;
PrevBlock = null;
value = null;
}

this(string newvalue)
{
this();
value = newvalue;
}

~this()
{
value = ;
}
}


Just an FYI classes are reference types in D so you probably meant

public DataBlock NextBlock; // is a class reference

 	public DataBlock * PrevBlock; 	//classes are reference types 
already no need for *

// is a pointer to a 
class reference




Re: dmd/druntime/phobos HEAD: can't link binaries on Arch Linux

2015-06-15 Thread Vladimir Panteleev via Digitalmars-d-learn

On Monday, 15 June 2015 at 15:50:59 UTC, Atila Neves wrote:

Anyone else getting this problem on Arch Linux?

dmd hello.d

hello.o:hello.d:TypeInfo_S3std3uni38__T13InversionListTS3std3uni8GcPolicyZ13InversionList67__T9IntervalsTS3std3uni32__T8CowArrayTS3std3uni8GcPolicyZ8CowArrayZ9Intervals.init$:
 error: undefined reference to 
'std.uni.InversionList!(std.uni.GcPolicy).InversionList.Intervals!(std.uni.CowArray!(std.uni.GcPolicy).CowArray).Intervals.__fieldPostblit()'
collect2: error: ld returned 1 exit status
--- errorlevel 1

With -v I see that it's compiling phobos in by calling gcc like 
this:


-l:libphobos2.a

When I replace that with just the path to phobos instead, it 
works.


Not using Arch Linux, but just from your post it looks like it's 
not finding the libphobos.a from HEAD and using the system one 
instead.


You may want to check the dmd.conf file for your HEAD D install: 
make sure DMD is using it (dmd | head -4), and that it adds the 
library search path correctly (-L-L switch in DFLAGS).


Re: Casting MapResult

2015-06-15 Thread Ali Çehreli via Digitalmars-d-learn

On 06/15/2015 08:21 AM, Adam D. Ruppe wrote:

 don't call .array if you don't have to, chaining calls to
 map and such, even foreach(item; some_map_result) can be done without
 actually building the array and can give more efficiency.

To add, the OP can use 'sum' or 'reduce' for adding them together:

  http://dlang.org/phobos/std_algorithm_iteration.html

import std.stdio;
import std.algorithm;
import std.math;

void main()
{
float[] arr = [ 1.5, 2.5 ];
auto y = arr.map!exp;
writeln(y.sum);// same as sum(y)
}

An equivalent of the last line:

writeln(reduce!((result, a) = result + a)(y));

Ali



Extract template parameter at runtime?

2015-06-15 Thread Yuxuan Shui via Digitalmars-d-learn
I have a template class which is derived from a base class. Is it 
possible to extract the template parameter from a reference to 
the base class?


Can is() operate on TypeInfo?


Re: Extract template parameter at runtime?

2015-06-15 Thread Baz via Digitalmars-d-learn

On Monday, 15 June 2015 at 18:10:34 UTC, Yuxuan Shui wrote:

Can is() operate on TypeInfo?


yes, you can compare instance of TypeInfo using is or '==' too, 
using typeid which

is returns at run-time the TypeInfo of the argument:

---
void main()
{
assert(typeid(2) == typeid(1));
assert(typeid(2) is typeid(1));
Object a = new Object;
Object b = new Object;
assert(typeid(b) is typeid(a));
}
---




Re: Casting MapResult

2015-06-15 Thread ketmar via Digitalmars-d-learn
On Mon, 15 Jun 2015 17:07:55 +, jmh530 wrote:

 I have a little bit of a follow up.
 
 After making the recommended changes, the function seems to work with
 both static and dynamic arrays. I then noticed that all of the examples
 for functions that pass arrays in http://dlang.org/function.html use the
 dynamic array notation like my function above. Does this matter?

it doesn't, but i'd use `[]` anyway for code readability. static arrays 
can be converted to slices by the compiler when it needs to, but by using 
explicit `[]` it's easy to see if function expects slice or real static 
array right in the call site, without looking at function signature.

signature.asc
Description: PGP signature


Re: Extract template parameter at runtime?

2015-06-15 Thread Steven Schveighoffer via Digitalmars-d-learn

On 6/15/15 2:10 PM, Yuxuan Shui wrote:

I have a template class which is derived from a base class. Is it
possible to extract the template parameter from a reference to the base
class?


No. You can't get compile-time information at runtime, unless you have 
stored it somewhere that runtime can read.


But perhaps you can further describe your requirement, and a solution 
might be available.


-Steve


Re: Casting MapResult

2015-06-15 Thread Baz via Digitalmars-d-learn

On Monday, 15 June 2015 at 15:10:24 UTC, jmh530 wrote:

I wrote a simple function to apply map to a float dynamic array

auto exp(float[] x) {
auto y = x.map!(a = exp(a));
return y;
}

However, the type of the result is MapResult!(__lambda2, 
float[]). It seems like some of the things that I might do to a 
float[], I can't do to this type, like adding them together. So 
I tried to adjust this by adding in a cast to float[], as in


float[] exp(float[] x) {
auto y = x.map!(a = exp(a));
cast(float[]) y;
return y;
}

But I get an error that I can't convert MapResult!(__lambda2, 
float[]) to float[].


So I suppose I have two questions: 1) am I screwing up the 
cast, or is there no way to convert the MapResult to float[], 
2) should I just not bother with map (I wrote an alternate, 
longer, version that doesn't use map but returns float[] 
properly).


In addition to the other answers you can use 
std.algorithm.iteration.each():


---
float[] _exp(float[] x) {
auto result = x.dup;
result.each!(a = exp(a));
return result;
}
---


Re: Casting MapResult

2015-06-15 Thread via Digitalmars-d-learn

On Monday, 15 June 2015 at 16:16:00 UTC, Ali Çehreli wrote:

On 06/15/2015 08:21 AM, Adam D. Ruppe wrote:

 don't call .array if you don't have to, chaining calls to
 map and such, even foreach(item; some_map_result) can be done
without
 actually building the array and can give more efficiency.

To add, the OP can use 'sum' or 'reduce' for adding them 
together:


  http://dlang.org/phobos/std_algorithm_iteration.html

import std.stdio;
import std.algorithm;
import std.math;

void main()
{
float[] arr = [ 1.5, 2.5 ];
auto y = arr.map!exp;
writeln(y.sum);// same as sum(y)
}

An equivalent of the last line:

writeln(reduce!((result, a) = result + a)(y));

Ali


`sum` is better for floating-point ranges, because it uses 
pair-wise or Kahan summation if possible, in order to preserve 
precision.


Re: Casting MapResult

2015-06-15 Thread Ali Çehreli via Digitalmars-d-learn
On 06/15/2015 09:39 AM, Marc =?UTF-8?B?U2Now7x0eiI=?= 
schue...@gmx.net wrote:



writeln(y.sum);// same as sum(y)
}

An equivalent of the last line:

writeln(reduce!((result, a) = result + a)(y));



`sum` is better for floating-point ranges, because it uses pair-wise or
Kahan summation if possible, in order to preserve precision.


Good point. I had mentioned that elsewhere after learning about it 
recently: the sum of the elements of a range should be calculated by 
std.algorithm.sum, which uses special algorithms to achieve more 
accurate calculations for floating point types. :)


  http://ddili.org/ders/d.en/fibers.html#ix_fibers.recursion

Ali



Re: Casting MapResult

2015-06-15 Thread jmh530 via Digitalmars-d-learn

I have a little bit of a follow up.

After making the recommended changes, the function seems to work 
with both static and dynamic arrays. I then noticed that all of 
the examples for functions that pass arrays in 
http://dlang.org/function.html use the dynamic array notation 
like my function above. Does this matter?


Re: Casting MapResult

2015-06-15 Thread Baz via Digitalmars-d-learn

On Monday, 15 June 2015 at 19:22:31 UTC, jmh530 wrote:

On Monday, 15 June 2015 at 19:04:32 UTC, Baz wrote:
In addition to the other answers you can use 
std.algorithm.iteration.each():


---
float[] _exp(float[] x) {
auto result = x.dup;
result.each!(a = exp(a));
return result;
}
---


Am I right that the difference is that map is lazy and each is 
greedy? Does that have any significant performance effects?


i think that the OP wants greedy. That's why he had to fight with 
map results.





Re: Casting MapResult

2015-06-15 Thread Baz via Digitalmars-d-learn

On Monday, 15 June 2015 at 19:30:08 UTC, Baz wrote:

On Monday, 15 June 2015 at 19:22:31 UTC, jmh530 wrote:

On Monday, 15 June 2015 at 19:04:32 UTC, Baz wrote:
In addition to the other answers you can use 
std.algorithm.iteration.each():


---
float[] _exp(float[] x) {
auto result = x.dup;
result.each!(a = exp(a));
return result;
}
---


Am I right that the difference is that map is lazy and each is 
greedy? Does that have any significant performance effects?


i think that the OP wants greedy. That's why he had to fight 
with map results.


Ah sorry it's you the OP. just get it. So you wanted greedy, 
didn't you ?





Re: Casting MapResult

2015-06-15 Thread jmh530 via Digitalmars-d-learn
I suppose I would want whichever has the best performance. 
Without testing, I'm not sure which one would be better. 
Thoughts?


I had been fighting with the map results because I didn't 
realize there was an easy way to get just the array.


I'm actually not having much luck with your original function 
(and I tried some variations on it). It just kept outputting the 
original array without applying the function. I tried it in main 
also (without being in a function) without much luck either.


Re: Casting MapResult

2015-06-15 Thread jmh530 via Digitalmars-d-learn

On Monday, 15 June 2015 at 19:32:12 UTC, Baz wrote:

On Monday, 15 June 2015 at 19:30:08 UTC, Baz wrote:

On Monday, 15 June 2015 at 19:22:31 UTC, jmh530 wrote:

On Monday, 15 June 2015 at 19:04:32 UTC, Baz wrote:
In addition to the other answers you can use 
std.algorithm.iteration.each():


---
float[] _exp(float[] x) {
auto result = x.dup;
result.each!(a = exp(a));
return result;
}
---


Am I right that the difference is that map is lazy and each 
is greedy? Does that have any significant performance effects?


i think that the OP wants greedy. That's why he had to fight 
with map results.


Ah sorry it's you the OP. just get it. So you wanted greedy, 
didn't you ?


I suppose I would want whichever has the best performance. 
Without testing, I'm not sure which one would be better. Thoughts?


I had been fighting with the map results because I didn't realize 
there was an easy way to get just the array.


Re: Casting MapResult

2015-06-15 Thread jmh530 via Digitalmars-d-learn

On Monday, 15 June 2015 at 19:04:32 UTC, Baz wrote:
In addition to the other answers you can use 
std.algorithm.iteration.each():


---
float[] _exp(float[] x) {
auto result = x.dup;
result.each!(a = exp(a));
return result;
}
---


Am I right that the difference is that map is lazy and each is 
greedy? Does that have any significant performance effects?


Re: compilation issues in a shared library project

2015-06-15 Thread Jonathan Villa via Digitalmars-d-learn

On Monday, 15 June 2015 at 06:39:49 UTC, Nicholas Wilson wrote:

On Sunday, 7 June 2015 at 00:38:17 UTC, Jonathan Villa wrote:


Just an FYI classes are reference types in D so you probably 
meant


public DataBlock NextBlock; // is a class reference

 	public DataBlock * PrevBlock; 	//classes are reference types 
already no need for *

// is a pointer to a 
class reference


I didn't know D:
thanks so much ^^


Re: Extract template parameter at runtime?

2015-06-15 Thread Yuxuan Shui via Digitalmars-d-learn
On Monday, 15 June 2015 at 18:30:55 UTC, Steven Schveighoffer 
wrote:

On 6/15/15 2:10 PM, Yuxuan Shui wrote:
I have a template class which is derived from a base class. Is 
it
possible to extract the template parameter from a reference to 
the base

class?


No. You can't get compile-time information at runtime, unless 
you have stored it somewhere that runtime can read.


But perhaps you can further describe your requirement, and a 
solution might be available.


-Steve


Well I don't have a serious use case of this. I just started 
using D couple of weeks ago, and is now experimenting with it by 
writing a toy compiler.


What I'm doing is I'm abusing the D type system to represent 
types in my toy language. I store type information into template 
parameters, and when I need to type check, I just use typeid and 
compare the result. Now I want to generate different code when I 
encounter different types, and for that I want to get the 
template parameters.


Of course if this is not possible I can always go back to 
implement my own type system properly. It's just a good thing to 
have.


Re: Extract template parameter at runtime?

2015-06-15 Thread Yuxuan Shui via Digitalmars-d-learn

On Monday, 15 June 2015 at 22:56:57 UTC, Yuxuan Shui wrote:
On Monday, 15 June 2015 at 18:30:55 UTC, Steven Schveighoffer 
wrote:

[...]


Well I don't have a serious use case of this. I just started 
using D couple of weeks ago, and is now experimenting with it 
by writing a toy compiler.


What I'm doing is I'm abusing the D type system to represent 
types in my toy language. I store type information into 
template parameters, and when I need to type check, I just use 
typeid and compare the result. Now I want to generate different 
code when I encounter different types, and for that I want to 
get the template parameters.


Of course if this is not possible I can always go back to 
implement my own type system properly. It's just a good thing 
to have.


Maybe I can put a virtual function in the class that return the 
template parameter?


Re: Casting MapResult

2015-06-15 Thread Ali Çehreli via Digitalmars-d-learn

On 06/15/2015 12:44 PM, jmh530 wrote:

 On Monday, 15 June 2015 at 19:32:12 UTC, Baz wrote:

 Ah sorry it's you the OP. just get it. So you wanted greedy, didn't 
you ?


 I suppose I would want whichever has the best performance. Without
 testing, I'm not sure which one would be better. Thoughts?

There are different levels of lazyness regarding performance. :)

1) map and most algorithms are fully lazy. They don't do anything until 
elements are actually used.


2) Some algorithms that keep their state in struct objects do some work 
in their constructors to prepare the first element for use.


3) 'each' is fully eager. It walks through all elements of the range and 
does something with each of those.


4) 'array' is fully eager but it also creates an array to store all the 
elements in.


There are also semi-eager algorithms like 'asyncBuf' and 'map' in 
std.parallelism that consume the input range in waves.


Ali



Re: Extract template parameter at runtime?

2015-06-15 Thread Ali Çehreli via Digitalmars-d-learn

On 06/15/2015 04:04 PM, Yuxuan Shui wrote:


Maybe I can put a virtual function in the class that return the template
parameter?


Yeah, that's how I would do it.

Ali



Re: Casting MapResult

2015-06-15 Thread Baz via Digitalmars-d-learn

On Monday, 15 June 2015 at 20:10:30 UTC, jmh530 wrote:
I suppose I would want whichever has the best performance. 
Without testing, I'm not sure which one would be better. 
Thoughts?


I had been fighting with the map results because I didn't 
realize there was an easy way to get just the array.


I'm actually not having much luck with your original function 
(and I tried some variations on it). It just kept outputting 
the original array without applying the function. I tried it in 
main also (without being in a function) without much luck 
either.


Right, my bad. This one whould work:

---
float[] test(float[] x) {
auto result = x.dup;
result.each!((ref a) = (a = exp(a)));
return result;
}
---