Video playback

2014-05-18 Thread Dmitry via Digitalmars-d-learn

Hi everyone!
I want to play video in my D-application (maybe WebM or Theora). 
However didn't find any library for operation with video in D. I 
am a beginner in D, experience of transfer of libraries with 
C/C++, certainly isn't present.

Maybe somebody will prompt something?

P.S. My application uses SFML (DSFML).


Re: randomSample

2014-05-18 Thread bearophile via Digitalmars-d-learn

Meta:


You need to use the function array from std.array.

import std.array;

int[] source = [ ... ];
int[] sample = randomSample(source, 3).array();


In some cases it's also useful to use std.algorithm.copy:


void main() {
import std.stdio, std.algorithm, std.random, std.array,
   std.range;

immutable int[9] source = iota(10, 100, 10).array;
source.writeln;
int[3] sample;
source[].randomSample(sample.length).copy(sample[]);
sample.writeln;
}


Bye,
bearophile


Re: Video playback

2014-05-18 Thread Rikki Cattermole via Digitalmars-d-learn

On 18/05/2014 7:10 p.m., Dmitry wrote:

Hi everyone!
I want to play video in my D-application (maybe WebM or Theora). However
didn't find any library for operation with video in D. I am a beginner
in D, experience of transfer of libraries with C/C++, certainly isn't
present.
Maybe somebody will prompt something?

P.S. My application uses SFML (DSFML).


My suggestion would be to go the direction of libvlc[0]. I did find a 
forum post from SFML that may help you.
It is a c library so it shouldn't be too hard to create a binding to. 
Perhaps something along the lines of Derelict-Util[2] will help with it.


If you need shared libraries and don't want to build your own, you 
should be able to extract them after installing Vlc itself.


[0] https://wiki.videolan.org/LibVLC/
[1] http://en.sfml-dev.org/forums/index.php?topic=9386.0
[2] https://github.com/DerelictOrg/DerelictUtil


UCFS does not work for nested functions?

2014-05-18 Thread Steffen Wenz via Digitalmars-d-learn

Hi,

Just noticed that using UFCS does not work for nested functions, 
and was wondering whether that's intended, and what the rationale 
behind it is:



class X {
void foo() {}
}

void main() {
// moving bar to module scope solves the error below
void bar(X x) {}

X x;
x.foo(); // ok
bar(x); // ok
x.bar(); // Error: no property 'bar' for type 'nested.X'
}



Re: UCFS does not work for nested functions?

2014-05-18 Thread bearophile via Digitalmars-d-learn

Steffen Wenz:


Just noticed that using UFCS does not work for nested functions,


Right.


and was wondering whether that's intended, and what the 
rationale behind it is:


Currently it's intended. Because doing otherwise causes other 
problems with struct/class member functions. Perhaps there are 
ways to design around this problem, but so far no one has 
suggested a good way to design it (and the good Kenji has worked 
on this problem, so probably there are no simple solutions).


Once you understand this problem space well, if you find a good 
design solution you can submit it to Bugzilla.


Bye,
bearophile


Re: Problems with dmd Switches in Makefiles

2014-05-18 Thread via Digitalmars-d-learn

For the compiler version, I've submitted a PR:

https://github.com/D-Programming-Language/dmd/pull/3558


Re: Video playback

2014-05-18 Thread Dmitry via Digitalmars-d-learn

On Sunday, 18 May 2014 at 07:37:33 UTC, Rikki Cattermole wrote:
My suggestion would be to go the direction of libvlc[0]. I did 
find a forum post from SFML that may help you.
It is a c library so it shouldn't be too hard to create a 
binding to. Perhaps something along the lines of 
Derelict-Util[2] will help with it.


If you need shared libraries and don't want to build your own, 
you should be able to extract them after installing Vlc itself.


Thank you, Rikki! I try.


Modify char in string

2014-05-18 Thread Tim via Digitalmars-d-learn

Hi everyone,

is there any chance to modify a char in a string like:

void main()
{
   string sMyText = "Replace the last char_";
   sMyText[$ - 1] = '.';
}

But when I execute the code above I'm always getting "cannot 
modify immutable expression at sMyText[__dollar -1LU]". I though 
D supported such modifications anytime. How can I do that now...?


Re: Modify char in string

2014-05-18 Thread bearophile via Digitalmars-d-learn

Tim:


is there any chance to modify a char in a string like:

void main()
{
   string sMyText = "Replace the last char_";
   sMyText[$ - 1] = '.';
}

But when I execute the code above I'm always getting "cannot 
modify immutable expression at sMyText[__dollar -1LU]". I 
though D supported such modifications anytime. How can I do 
that now...?


D strings are immutable. And mutating immutable variables is a 
bug in D. So you can't do that. You have to work around the 
problem. One solution is to not have a string, but something more 
like a char[] in the first place, and mutate it.


If you have a string, you can do (this works with the current GIT 
DMD compiler):




/// Not Unicode-safe.
string dotLast(in string s) pure nothrow {
auto ds = s.dup;
ds[$ - 1] = '.';
return ds;
}

void main() {
import std.stdio;

immutable input = "Replace the last char_";
immutable result = input.dotLast;
result.writeln;
}


That code doesn't work if your text contains more than the Ascii 
chars.


Bye,
bearophile


Re: Modify char in string

2014-05-18 Thread Chris Cain via Digitalmars-d-learn

On Sunday, 18 May 2014 at 18:55:59 UTC, Tim wrote:

Hi everyone,

is there any chance to modify a char in a string like:


As you've seen, you cannot modify immutables (string is an 
immutable(char)[]). If you actually do want the string to be 
modifiable, you should define it as char[] instead.


Then your example will work:

void main()
{
   char[] sMyText = "Replace the last char_";
   sMyText[$ - 1] = '.';
}

If you actually want it to be immutable, you can still do it, but 
you can't modify in-place, you must create a new string that 
looks like what you want:


void main()
{
   string sMyText = "Replace the last char_";
   sMyText = sMyText[0 .. $-1] ~ ".";
   // you would do
   //sMyText[0 .. 5] ~ "." ~ sMyText[6..$];
   // to "replace" something in the 5th position
}

Note that the second method allocates and uses the GC more (which 
is perfectly fine, but not something you want to do in a tight 
loop). For most circumstances, the second method is good.


Is it possible to assumeSafeAppend malloced memory?

2014-05-18 Thread Ali Çehreli via Digitalmars-d-learn
We know that most of the time memory is allocated more than the 
requested amount. Is there a way to take advantage of that extra 
trailing space? (And potentially the pages that come after that.)


import core.memory;

void main()
{
const count = 1;

// I think there is extra capacity beyond the 'count' elements
int* ptr = cast(int*)GC.malloc(count * int.sizeof);
int[] arr = ptr[0 .. count];

assert(arr.capacity == 0);
arr.assumeSafeAppend;
assert(arr.capacity == 0);// still 0. :(
}

This issue puts std.array.array to a disadvantage compared to proper 
slices because array() involves the following call chain, the last of 
which does call GC.malloc:


  trustedAllocateArray
  uninitializedArray
  arrayAllocImpl

As a result, iota(10).array.assumeSafeAppend ends up having 0 capacity. :(

Ali