Re: Variadic template with template arguments in pairs

2018-09-12 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 12 September 2018 at 15:12:16 UTC, Anonymouse wrote: void doByPair(Args...)(Args args) if (Args.length) { foreach (pair; args.pairwise) { static assert(is(typeof(pair[0]) == string)); static assert(isPointer!(pair[1])); assert(pair[1] !is null);

Re: assumeNoGC works but can't get an assumePure to work

2018-09-03 Thread Paul Backus via Digitalmars-d-learn
On Monday, 3 September 2018 at 22:07:10 UTC, aliak wrote: Why does it work with nogc but not with pure? Cheers, - Ali You can't define an impure function inside a pure unittest. If you move `modify` outside the unittest block, and change the argument from a lambda to a function pointer, it

Re: Pass lambda into template

2018-09-03 Thread Paul Backus via Digitalmars-d-learn
On Monday, 3 September 2018 at 09:09:44 UTC, Andrey wrote: Hello, Here is a code with comments: https://run.dlang.io/is/BNl2Up. I don't understand how to pass lambda into template. I get an error: onlineapp.d(18): Error: template instance `qwerty!((i) => "arg" ~ i.to!string ~ "[0] == '?'",

Re: Solving the impossible?

2018-08-29 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 29 August 2018 at 22:18:09 UTC, Everlast wrote: No it is not! you have simply accepted it to be fact, which doesn't make it consistent. If you take 100 non-programmers(say, mathematicians) and ask them what is the natural extension of allowing an arbitrary number of parameters

Re: Solving the impossible?

2018-08-29 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 29 August 2018 at 19:56:31 UTC, Everlast wrote: One of the things that makes a good language is it's internal syntactic consistency. This makes learning a language easier and also makes remembering it easier. Determinism is a very useful tool as is abstraction consistency. To say

Re: Solving the impossible?

2018-08-28 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 28 August 2018 at 20:37:05 UTC, Everlast wrote: Also, the biggest complaint is that when we use [] attached to a type it has a specific meaning as "an array of". e.g., int[] means an array of int's. But int[] a... then changes as we don't have an array of int's any more but

Re: Solving the impossible?

2018-08-28 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 28 August 2018 at 19:09:38 UTC, Everlast wrote: Yeah, I see the link paul posted. The actual syntax seems a bit strange to me... We don't do A[] a So it is not "logical". foo(A...)(A a) but if A is a specific type we must do foo(int[] a ...) The actual syntax then looks

Re: How to pass alias as template parameter?

2018-08-27 Thread Paul Backus via Digitalmars-d-learn
On Monday, 27 August 2018 at 09:50:01 UTC, Andrey wrote: alias Pair(alias key, alias value) = AliasSeq!(key, value); alias Pairs = AliasSeq!(Pair!(Option.First, handler!(Option.First)), Pair!(Option.Second, handler!(Option.Second)), handler); You can't nest AliasSeqs. If you examine Pairs

Re: Patterns to avoid GC with capturing closures?

2018-08-26 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 26 August 2018 at 06:08:39 UTC, vit wrote: const x = iota(0, 10) .map!((x, i) => x*i)(a) ///map!((x) => x*a) .map!((x, i) => x*i)(b) ///map!((x) => x*b) .filter!((x, i) => x%i)(c)///filter!((x) => x%c) .any!(x => x % c); I think it's easier to

Re: Solving the impossible?

2018-08-25 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 26 August 2018 at 02:26:58 UTC, Everlast wrote: The problem is, suppose one wants to specify A void print(T, int... A)(T t, A a) while tricks can be used, why doesn't D support such an obvious syntax? We can specify an arbitrary type but can't restrict it in an obvious way, in

Re: Patterns to avoid GC with capturing closures?

2018-08-24 Thread Paul Backus via Digitalmars-d-learn
On Friday, 24 August 2018 at 15:18:13 UTC, Peter Alexander wrote: I can write scaleAll like this: auto scaleAll(int[] xs, int m) @nogc { return repeat(m).zip(xs).map!(mx => mx[0] * mx[1]); } So that repeat(m) stores m, but it is quite hacky to work like this. Here's a spoonful of sugar to

Re: Make function alias

2018-08-20 Thread Paul Backus via Digitalmars-d-learn
On Monday, 20 August 2018 at 13:14:14 UTC, Andrey wrote: Hello, I want to make an alias to function "std.stdio.writeln" and "std.stdio.write" and use it like: static void log(bool newline = true)(string text) { alias print(T...) = newline ? : _file.print(); text.print(); }

Re: Deduce type of struct in function arguments

2018-08-20 Thread Paul Backus via Digitalmars-d-learn
On Monday, 20 August 2018 at 12:33:34 UTC, Andrey wrote: On Monday, 20 August 2018 at 11:38:39 UTC, Paul Backus wrote: Create an overload of foo that takes two arguments and combines them into a `Data` struct internally: void foo(int a, string text) { Data data = {a, text}; foo(data);

Re: Deduce type of struct in function arguments

2018-08-20 Thread Paul Backus via Digitalmars-d-learn
On Monday, 20 August 2018 at 09:23:27 UTC, Andrey wrote: Hello, I have a function and a struct: void foo(ref Data data) { ... } struct Data { int a; string text; } How to pass struct into function without naming its type? This doesn't work: foo({1234, "Hello!"}); Create an

Re: Generically call a function on Variant's payload?

2018-08-19 Thread Paul Backus via Digitalmars-d-learn
On Monday, 20 August 2018 at 00:27:04 UTC, Nick Sabalausky (Abscissa) wrote: Suppose I've wrapped a Variant in a struct/class which ensures the Variant *only* ever contains types which satisfy a particular constraint (for example: isInputRange, or hasLength, etc...). Is there a way to call a

Re: Convert output of map() to array of strings

2018-08-15 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 15 August 2018 at 13:53:02 UTC, Andrey wrote: Hello, I have the following code: string[] list; string text; // ... enum pattern = ctRegex!`^[0-9]+$`; list = text.split('\n').map!(line => line.matchFirst(pattern).hit); Compiler says that it can't convert result of map function

Re: is this a betterC bug ?

2018-08-14 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 14 August 2018 at 13:01:57 UTC, learnfirst1 wrote: enum string[] a = ["a"]; extern(C) void main() { int i = 0; auto s = a[i]; } --- Error: TypeInfo cannot be used with -betterC Workaround: https://run.dlang.io/is/NZykl0 Source:

Re: write a function template specialisation that tests if an argument is known at compile time

2018-08-11 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 12 August 2018 at 02:17:21 UTC, Cecil Ward wrote: I was thinking about reflection and powerful things like traits. Would a test to see if a static if compile do the trick ? You ask the question using traits : "does the following compile? : { static if ( mask == 3 ) { }; }" - any

Re: write a function template specialisation that tests if an argument is known at compile time

2018-08-11 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 12 August 2018 at 00:15:37 UTC, Cecil Ward wrote: Paul, what would the calls look like? I am about to misunderstand things completely so here goes :-) It would be a bit kludgy having to switch from one calling syntax to another, putting the mask argument in the template parameters

Re: write a function template specialisation that tests if an argument is known at compile time

2018-08-11 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 11 August 2018 at 05:17:51 UTC, Cecil Ward wrote: T myfunc(T)( T x, uint mask ) if ( mask == 3 ) { return fast_func( x, mask ); } but of course this doesn't work because mask is not known at compile-time. so I wondered if there is a way to do something like static if

Re: templated lambda with {} cause GC

2018-08-10 Thread Paul Backus via Digitalmars-d-learn
On Friday, 10 August 2018 at 11:10:55 UTC, learnfirst1 wrote: Still, if my first example is use GC, why dmd not throw error at compile time, instead at link time report symbols is missing. Is this a bug ? If you make your main function @nogc, you will get a compile-time error.

Re: templated lambda with {} cause GC

2018-08-10 Thread Paul Backus via Digitalmars-d-learn
On Friday, 10 August 2018 at 09:57:53 UTC, learnfirst1 wrote: import core.stdc.stdio; struct Test { string name ; } void T(alias pred, A...)(){ __gshared t = Test(A) ; pred(t); } extern(C) void main(){ T!(t => printf("test 1 name = %s\n".ptr, t.name.ptr), "test") ;

Re: float/double to string (pure nothrow @nogc)

2018-08-08 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 8 August 2018 at 17:08:57 UTC, vit wrote: Hello, is in phobos some function which convert float/double to string and is pure @nogc and nothrow? Short answer: no. Long answer: https://issues.dlang.org/show_bug.cgi?id=17628

Re: GC and void[N] in struct

2018-08-06 Thread Paul Backus via Digitalmars-d-learn
On Monday, 6 August 2018 at 20:22:36 UTC, vit wrote: On Monday, 6 August 2018 at 19:56:03 UTC, Steven Schveighoffer wrote: BTW, is there a reason you aren't just using Algebraic? https://dlang.org/phobos/std_variant.html#.Algebraic -Steve primarily visit for Algebraic isn't pure, @nogc,

Re: @nogc closures

2018-08-05 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 5 August 2018 at 12:23:17 UTC, vit wrote: Yes, it isn't possible. I modify filter a and map from std.algorithm: void main()@nogc{ import std.experimental.all; const int j = 2; int i = 0; const int[3] tmp = [1, 2, 3]; tmp[]

Re: taskPool.reduce vs algorithm.reduce

2018-07-11 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 11 July 2018 at 08:31:30 UTC, Dorian Haglund wrote: But it fails to compile (with gdc 8.1.0, dmd v2.081) complaining that template instance reduce!(f) cannot use local 'f' as parameter to non-global template reduce(functions...) Congratulations! You've just run into issue

Re: pipeProcess failing

2018-06-03 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 3 June 2018 at 15:07:07 UTC, DigitalDesigns wrote: I'm calling pipe process using pipeProcess([AliasSeq!args], Redirect.stdout | Redirect.stdin); where args is a tuple. Everything works when I pass each argument individually. If I combine any args using a space it fails or if I

Re: New programming paradigm

2018-06-03 Thread Paul Backus via Digitalmars-d-learn
On Monday, 4 September 2017 at 03:26:23 UTC, EntangledQuanta wrote: Take a variant type. It contains the "type" and the data. To simplify, we will treat look at it like (pseudo-code, use your brain) enum Type { int, float } foo(void* Data, Type type); The normal way to deal with this is a

Re: How do I make this function thread safe?

2018-05-31 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 31 May 2018 at 19:26:12 UTC, Dr.No wrote: My application create some HTML which is then converted to PDF by wkhtmltopdf library. I'm trying to figure out how make the PDF generation run parallel, currently, it's running linearly. It looks like wkhtmltopdf does not support

Re: Creating a template mixin for explicit casts.

2018-05-17 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 17 May 2018 at 15:25:37 UTC, Sjoerd Nijboer wrote: I want to make a template mixin that is able to cast one of these generic structs to the other explicitly. I have a bunch of these structs and therefore I thought it would make sense to do it by template mixin. I just don't know

Re: Using iteration / method chaining / etc on multi-dimensional arrays

2018-04-12 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 12 April 2018 at 20:34:40 UTC, Chris Katko wrote: But each doesn't return anything, it mutates, right? I think that's the problem I ran into with my attempt. With your code, I get an error about void: string []x = split(file.readln.idup, " "); x.each((ref s) => s.each((ref

Re: Is std.variant.visit not @nogc?

2018-04-10 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 10 April 2018 at 12:34:07 UTC, aliak wrote: Awesome! this is a neat trick: union { AliasSeq!(T0, T1) values; } Is that usage documented somewhere, or is it somewhere in phobos maybe? Also, can Algebraic be fully replaced with this version then or is there some functionality

Re: Is std.variant.visit not @nogc?

2018-04-09 Thread Paul Backus via Digitalmars-d-learn
On Tuesday, 10 April 2018 at 00:22:18 UTC, helxi wrote: On Monday, 9 April 2018 at 15:59:32 UTC, Paul Backus wrote: On Monday, 9 April 2018 at 07:07:58 UTC, Chris Katko wrote: [...] I agree in general, but in this case it's actually completely doable. In fact, I've done it myself: check out

Re: Is std.variant.visit not @nogc?

2018-04-09 Thread Paul Backus via Digitalmars-d-learn
On Monday, 9 April 2018 at 07:07:58 UTC, Chris Katko wrote: On Monday, 9 April 2018 at 07:02:50 UTC, Hasen Judy wrote: IMO, this is one more reason why sum-types should be built into the language compiler, instead of being implemented in user-space. +1. Any time you have to "create" a

Re: toString contains null for struct with function/method

2018-04-08 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 8 April 2018 at 15:04:49 UTC, number wrote: the write() shows a 'null' if the struct has a function/method. why is that? ``` import std.stdio; void main() { struct S { int i; } S s; writeln(s);// S(0)

Re: Function template argument deduction

2018-04-07 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 7 April 2018 at 14:02:55 UTC, Paul Backus wrote: Interesting. Looks like this is an issue with aliases, because I get the error with this code too: --- test.d import std.typecons: Tuple, tuple; alias Pair(T) = Tuple!(T, T); void foo(T)(Pair!T p) { return; } unittest {

Re: Function template argument deduction

2018-04-07 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 7 April 2018 at 06:26:24 UTC, Uknown wrote: What I did notice though is that when `string list2string(T)(List!T list)` was changed to `string list2string(T)(VariantN!(16LU, Nil, Tuple!(T, "head", This*, "tail")) list)` The compiler correctly deduce `T` to be `int` Interesting.

Re: Function template argument deduction

2018-04-07 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 7 April 2018 at 05:46:07 UTC, Uknown wrote: I don't see the error you are talking about: https://run.dlang.io/is/XWPIc1 Are you using the latest compiler? Compile with -unittest. And yes; I'm using DMD 2.079.0.

Function template argument deduction

2018-04-06 Thread Paul Backus via Digitalmars-d-learn
I'm playing around with functional programming in D, and have run into a problem with the following code: --- list.d import std.variant: Algebraic, This, visit; import std.typecons: Tuple, tuple; struct Nil {} alias List(T) = Algebraic!( Nil, Tuple!(T, "head", This*, "tail") ); alias

Re: #import mapi.h

2018-03-22 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 21 March 2018 at 16:22:45 UTC, Martin Tschierschke wrote: Is there an step by step introduction how to convert a C header of an external lib into the right extern(C){} block? In addition to what others have said, I found the following article on the D Wiki useful:

Re: Zip with range of ranges

2018-02-25 Thread Paul Backus via Digitalmars-d-learn
On Sunday, 25 February 2018 at 16:22:19 UTC, ARaspiK wrote: Instead of passing std.range.zip a set of ranges as different arguments, is it possible to hand the m a range of ranges, and get them to zip together each element of every subrange? `std.range.transposed` does this, but it requires

Re: free func "front" is not picked up as a candidate when doing range.front(...)

2018-02-07 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 7 February 2018 at 19:25:01 UTC, aliak wrote: import std.range: iota; 0.iota.front(100).writeln; // Error: inout method std.range.iota!(int, int).iota.Result.front is not callable using a mutable object import std.algorithm: filter; auto arr = [0, 1];

Re: Templates for DRY code

2018-01-06 Thread Paul Backus via Digitalmars-d-learn
On Saturday, 6 January 2018 at 03:38:35 UTC, codephantom wrote: or even.. a.append( s.to!ConvertToElementType(a) ); That's not valid code of course, but the semantics are all contained in that single chunk. This works: import std.range.primitives: ElementType; a ~=

Re: Error: variable foo conflicts with struct foo

2018-01-04 Thread Paul Backus via Digitalmars-d-learn
On Thursday, 4 January 2018 at 17:45:35 UTC, Stijn wrote: Why is it not allowed for a variable name to match a type name? The following example fails with "Error: variable foo conflicts with struct foo" struct foo {} foo foo; Imagine if you then tried to write something like

Re: Getting nice print of struct for debugging

2017-02-20 Thread Paul Backus via Digitalmars-d-learn
On Monday, 20 February 2017 at 16:04:17 UTC, Martin Tschierschke wrote: Hello, I have a little program where I am filling a struct with values from an regex match. Now I want to display the content of the struct for debugging purpose. I believe the easiest way to do this is to define a

Re: Unable To Install Debian Package on Deepin Linux (Debian Jessie)

2016-11-16 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 16 November 2016 at 02:24:26 UTC, azzuwan wrote: It seems that /usr/bin/dman is currently used by deepin-manual. Is there anyway to get around this? Take a look at dpkg-divert(1)

Re: Calling std.variant.visit from a pure function

2016-11-04 Thread Paul Backus via Digitalmars-d-learn
On Friday, 4 November 2016 at 23:38:47 UTC, sarn wrote: I suggest trying it with the latest dmd and filing a bug report. Taking a quick look at the library code (https://github.com/dlang/phobos/blob/master/std/variant.d), it *seems* like everything uses templates and functions returning

Re: not callable error

2016-11-03 Thread Paul Backus via Digitalmars-d-learn
On Friday, 4 November 2016 at 02:28:17 UTC, bluphantom91 wrote: Hello, I am trying to finish up a group project but I am running into a small problem. I keep getting an error about fgetc not being callable. The purpose of my program is to count the number of characters in a file. Any bit of

Calling std.variant.visit from a pure function

2016-11-03 Thread Paul Backus via Digitalmars-d-learn
As a learning exercise, I'm writing a simple AST evaluator for arithmetic expressions in D. (See https://gist.github.com/ckirkendall/2934374) In order to get a feel for functional programming in D, I decided to try and implement my solution in a functional style, using algebraic data types

<    4   5   6   7   8   9