Re: How to instantiate a map with multiple functions

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

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.


Re: How to check if JSONValue of type object has a key?

2015-10-06 Thread Fusxfaranto via Digitalmars-d-learn
On Tuesday, 6 October 2015 at 20:44:30 UTC, via 
Digitalmars-d-learn wrote:
On Tue, Oct 06, 2015 at 08:28:46PM +, Borislav Kosharov via 
Digitalmars-d-learn wrote:

JSONValue root = parseJSON(text);
if(root["key"].isNull == false) {


try

if("key" in root) {
// it is there
} else {
// it is not there
}


you can also do

if("key" !in root) {}


Additionally, just like associative arrays, if you need to access 
the value, you can get a pointer to it with the in operator (and 
if the key doesn't exist, it will return a null pointer).


const(JSONValue)* p = "key" in root;
if (p)
{
// key exists, do something with p or *p
}
else
{
// key does not exist
}


Re: Checking that a template parameter is an enum

2015-09-30 Thread Fusxfaranto via Digitalmars-d-learn

On Thursday, 1 October 2015 at 00:04:18 UTC, Nordlöw wrote:
How do I check that a template parameter is a CT-value or an 
enum symbol?


I want this to restrict the following template:

/** Returns: true iff all values $(D V) are the same. */
template allSame(V...)  // TODO restrict to values only
{
static if (V.length <= 1)
enum bool allSame = true;
else
enum bool allSame = V[0] == V[1] && allSame!(V[1..$]);
}

unittest
{
static assert(!allSame!(41, 42));
static assert(allSame!(42, 42, 42));
}


std.traits to the rescue!

http://dlang.org/phobos/std_traits.html#isExpressions

Using isExpressions!V as a template constraint looks like the 
behavior you're looking for.


Re: Trouble with template parameter matching

2015-08-02 Thread Fusxfaranto via Digitalmars-d-learn

On Sunday, 2 August 2015 at 08:08:05 UTC, tcak wrote:

[code]
void func1(N)( const N name )
if( is(N: string) || is(N: char[]) )
{
func2( name );
}

[...]


This seems like the reasonable behavior to me.  Perhaps you 
should use Unqual?

http://dlang.org/phobos/std_traits.html#Unqual



Re: Are Lua tables possible to do with D?

2015-07-16 Thread Fusxfaranto via Digitalmars-d-learn

On Thursday, 16 July 2015 at 06:48:12 UTC, Robert M. Münch wrote:
Hi, do you think it's possible to implemented something like 
Lua Tables (a hashed heterogeneous associative array) in D?


I know that Lua is dynamic and interpreted, hence it's a lot 
simpler to do than with a compiled language but I'm wondering 
if we could express such a generic data-structure in D.


An associative array of Variant[string] ought to do the job well 
enough.


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