Jump to page: 1 2 3
Thread overview
Preliminary submission - std.rational and std.typelist
Oct 06, 2012
Arlen
Oct 06, 2012
bearophile
Oct 06, 2012
H. S. Teoh
Oct 06, 2012
David Nadlinger
Oct 06, 2012
Arlen
Oct 06, 2012
Dmitry Olshansky
Oct 07, 2012
Philippe Sigaud
Oct 07, 2012
Jakob Ovrum
Oct 07, 2012
Dmitry Olshansky
Oct 07, 2012
Jakob Ovrum
Oct 07, 2012
Jonathan M Davis
Oct 07, 2012
Jakob Ovrum
Oct 08, 2012
H. S. Teoh
Oct 07, 2012
Philippe Sigaud
Oct 07, 2012
David Nadlinger
Oct 07, 2012
Artur Skawina
Oct 07, 2012
Philippe Sigaud
Oct 08, 2012
Arlen
Oct 08, 2012
Simen Kjaeraas
Oct 08, 2012
H. S. Teoh
Oct 08, 2012
Aziz K.
Oct 08, 2012
Ellery Newcomer
Oct 09, 2012
Aziz K.
Oct 09, 2012
Paul D. Anderson
Oct 09, 2012
Aziz K.
Oct 08, 2012
Arlen
Oct 08, 2012
bearophile
Oct 09, 2012
Arlen
October 06, 2012
Greetings,

In one of my D projects a units library was needed, so I decided to port Boost.Units to D.  I've been working on it for the past month or so (mostly on the weekends), and It's about 90% done.  The work has also produced additional functionality not yet available in Phobos, so I've decided to separate them into their own modules and present them here as my preliminary submission.  At the time I wasn't aware of the Boost submission process, otherwise I would have done "Determine Interest" first.  Hopefully nobody has ported Boost.Units to D.

So far the completion of the units library is contingent on the finalization of the design and implementation of the following two modules:

std.rational, which is a small module for rational numbers. Source:  https://github.com/Arlen/phobos/blob/units/std/rational.d Docs:  http://arlen.github.com/phobos/std_rational.html

std.typelist, which provides the TypeList type and several metafunctions.  The metafunctions are pretty much stolen from Haskell.
Source:  https://github.com/Arlen/phobos/blob/units/std/typelist2.d
Docs:  http://arlen.github.com/phobos/std_typelist2.html


I wasn't aware that we already had std.typelist module in Phobos until I was ready to submit my work.  I suppose the work was abandoned or it was never finalized?  There are differences in typelist2.d and typelist.d, though.

I would be interested to know if there is any interest in a units library.  I certainly need it in my project, and I will make my preliminary submission available to the community once it's complete.

Thanks,

Arlen
October 06, 2012
Arlen:

Regarding units in D, here I have shown a (hopefully) nice usage syntax:

http://forum.dlang.org/thread/uchcycnsvykuojzhuokh@forum.dlang.org#post-qnbpufluqlaxfyxzwxnq:40forum.dlang.org


> std.rational, which is a small module for rational numbers.
> Source:  https://github.com/Arlen/phobos/blob/units/std/rational.d
> Docs:  http://arlen.github.com/phobos/std_rational.html

I'd like a Rational in Phobos. Have you tested it (in unittests) widely with Bigints?


> std.typelist, which provides the TypeList type and several metafunctions.  The metafunctions are pretty much stolen from Haskell.  
> Source:  https://github.com/Arlen/phobos/blob/units/std/typelist2.d
> Docs:  http://arlen.github.com/phobos/std_typelist2.html

Aren't TypeTuples enough?

Bye,
bearophile
October 06, 2012
On Saturday, 6 October 2012 at 17:56:39 UTC, Arlen wrote:
> Hopefully nobody has ported Boost.Units to D.

I wrote a units library in D last year: http://klickverbot.at/code/units/. Before setting out, I extensively researched existing solutions in other languages, Boost.Units being on of them, and I believe that I managed to come up with a design which takes advantage of D's advanced metaprogramming capabilities, and as such is more powerful while being much less clunky.

For example, in comparison to Boost.Units, I didn't introduce a notion of "dimension" separate from the concept of units, which seemed to ultimately serve no purpose besides making valid conversions easier to determine given the limitations of C++. I'd love to hear your comments on the design!

There didn't seem much interest in it back then, but I know Andrei and a few others would like to see something like it in Phobos. It is pretty much finished, but needs a bit of polish here and there (many compiler improvements have been made since I wrote it). Unfortunately, I'm currently swamped in other work, though…

> std.typelist, which provides the TypeList type and several metafunctions.  The metafunctions are pretty much stolen from Haskell.  
> Source:  https://github.com/Arlen/phobos/blob/units/std/typelist2.d
> Docs:  http://arlen.github.com/phobos/std_typelist2.html

What are the real benefits of this over std.typetuple? We recently discussed what to do with std.typelist, which, as you noticed, has been bitrotting without being included in the official documentation. As far as I remember, there was consensus that having another implementation of "compile-time tuples" besides std.typetuple is not something we really want to do.

David
October 06, 2012
On 06-Oct-12 21:03, Arlen wrote:
> Greetings,
>
> In one of my D projects a units library was needed, so I decided to port Boost.Units to D.  I've been working on it for the past month or so (mostly on the weekends), and It's about 90% done.  The work has also produced additional functionality not yet available in Phobos, so I've decided to separate them into their own modules and present them here as my preliminary submission.  At the time I wasn't aware of the Boost submission process, otherwise I would have done "Determine Interest" first.  Hopefully nobody has ported Boost.Units to D.
>
> So far the completion of the units library is contingent on the finalization of the design and implementation of the following two modules:
>
> std.rational, which is a small module for rational numbers.
> Source:  https://github.com/Arlen/phobos/blob/units/std/rational.d
> Docs:  http://arlen.github.com/phobos/std_rational.html

Cool, does it work with BigInt?

Also I think there is better version of toString that has signature:
void toString(scope void delegate(const(char)[]) sink)

this toString just use functions like formattedWrite to write chars to sink. It thus totally avoids overhead of allocating strings.

Your current code may have some bad impact on performance:
return "(" ~ to!string(res.numerator) ~ "/" ~ to!string(res.denominator) ~ ")";

Allocates 4 times. ~ operator is convenient shortcut to get job done but it's unsuitable for building strings and formatted output.

> std.typelist, which provides the TypeList type and several metafunctions.  The metafunctions are pretty much stolen from Haskell.
> Source:  https://github.com/Arlen/phobos/blob/units/std/typelist2.d
> Docs:  http://arlen.github.com/phobos/std_typelist2.html
>

I concur with the general idea that it needs to be folded into std.typetuple unless there is some *really* compelling reason not to.

> I wasn't aware that we already had std.typelist module in Phobos until I was ready to submit my work.  I suppose the work was abandoned or it was never finalized?  There are differences in typelist2.d and typelist.d, though.

Old and never finalized.
Recently re-'discovered'. It never participated in
the compilation of Phobos and sort of went under the radar.


-- 
Dmitry Olshansky
October 06, 2012
On 10/06/2012 07:54 PM, bearophile wrote:
> I'd like a Rational in Phobos. Have you tested it (in unittests) widely with
> Bigints?

Seconded, this would be very nice to see.

October 06, 2012
Testing...


October 06, 2012
On Sat, Oct 06, 2012 at 10:54:31PM +0200, Joseph Rushton Wakeling wrote:
> On 10/06/2012 07:54 PM, bearophile wrote:
> >I'd like a Rational in Phobos. Have you tested it (in unittests) widely with
> >Bigints?
> 
> Seconded, this would be very nice to see.

+1.


T

-- 
Computers are like a jungle: they have monitor lizards, rams, mice, c-moss, binary trees... and bugs.
October 07, 2012
On Sat, Oct 6, 2012 at 8:32 PM, Dmitry Olshansky <dmitry.olsh@gmail.com> wrote:

> Your current code may have some bad impact on performance:
> return "(" ~ to!string(res.numerator) ~ "/" ~ to!string(res.denominator) ~
> ")";
>
> Allocates 4 times. ~ operator is convenient shortcut to get job done but it's unsuitable for building strings and formatted output.

And the solution is?
October 07, 2012
On Sunday, 7 October 2012 at 07:36:08 UTC, Philippe Sigaud wrote:
> On Sat, Oct 6, 2012 at 8:32 PM, Dmitry Olshansky <dmitry.olsh@gmail.com> wrote:
>
>> Your current code may have some bad impact on performance:
>> return "(" ~ to!string(res.numerator) ~ "/" ~ to!string(res.denominator) ~
>> ")";
>>
>> Allocates 4 times. ~ operator is convenient shortcut to get job done but
>> it's unsuitable for building strings and formatted output.
>
> And the solution is?

Appender and formattedWrite can do this with a minimal amount of memory allocations.

The best solution is of course the writeTo function from DIP9 [1], which hasn't been accepted yet for some reason.

[1] http://www.prowiki.org/wiki4d/wiki.cgi?LanguageDevel/DIPs/DIP9

October 07, 2012
On 07-Oct-12 12:10, Jakob Ovrum wrote:
> On Sunday, 7 October 2012 at 07:36:08 UTC, Philippe Sigaud wrote:
>> On Sat, Oct 6, 2012 at 8:32 PM, Dmitry Olshansky
>> <dmitry.olsh@gmail.com> wrote:
>>
>>> Your current code may have some bad impact on performance:
>>> return "(" ~ to!string(res.numerator) ~ "/" ~
>>> to!string(res.denominator) ~
>>> ")";
>>>
>>> Allocates 4 times. ~ operator is convenient shortcut to get job done but
>>> it's unsuitable for building strings and formatted output.
>>
>> And the solution is?
>
> Appender and formattedWrite can do this with a minimal amount of memory
> allocations.
>
> The best solution is of course the writeTo function from DIP9 [1], which
> hasn't been accepted yet for some reason.
>
> [1] http://www.prowiki.org/wiki4d/wiki.cgi?LanguageDevel/DIPs/DIP9
>
Ehem.. I've been pushing for DIP9 a lot of time. But then I find out that it is already here (and been for some time).

Like I said use a special overload of toString that is exactly writeTo.

Just define method with this signature:
void toString(scope void delegate(const(char)[]) sink)

And bingo! It's used in all of formatting stuff like write(f)(ln), format, formattedWrite etc.

e.g. before posting I played with this:

import std.stdio, std.format;

struct A{
	int k;
	void toString(scope void delegate(const(char)[]) sink)
	{
		formattedWrite(sink, "[%d]", k);
	}
}

void main(){
	A a = A(90);
	writeln(a);
}

-- 
Dmitry Olshansky
« First   ‹ Prev
1 2 3