Skip to content

Instantly share code, notes, and snippets.

@run-dlang
Created October 17, 2023 06:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save run-dlang/c3da600eeed2c98031c50d08c9df4d86 to your computer and use it in GitHub Desktop.
Save run-dlang/c3da600eeed2c98031c50d08c9df4d86 to your computer and use it in GitHub Desktop.
Code shared from run.dlang.io.
import std;
import std.algorithm;
import std.regex;
import std.array;
import std.conv;
import std.csv;
import std.math;
import std.string;
import std.stdio;
import std.string;
import std.numeric : gcd;
import std.typecons;
import std.functional;
enum OptionType : ushort
{
ISCALL = 0,
ISPUT = 1,
}
struct MYS
{
int a;
bool b;
char[2] c;
int opCmp(const MYS obj) const
{
if (obj.a < a)
return -1;
if (obj.a > a)
return 1;
return 0;
}
}
alias MyTup = Tuple!(long, "a", uint, "b", string, "c");
/+
class Main
{
MyTup* myTuple;
this (ref MyTup _myTuple)
{
myTuple = &_myTuple;
writeln("Addof ", myTuple, " val = ", *myTuple);
}
const(MyTup*) getTuple()
{
return myTuple;
}
}
+/
import std.string : chomp;
auto getVals(T)(T* array, size_t rows, size_t cols, const string delim = "_")
{
string s = "";
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
s ~= to!string(array[i * cols + j]) ~ delim;
s ~= delim;
}
return chomp(s, delim);
}
class Base
{
int a;
immutable(int[2]) b;
this(int _a)
{
a = _a;
int[2] _b;
_b[1] = 10;
_b[0] = -1;
b = _b;
}
void myFun(const int c)
{
writeln("Base myFun is called ", c);
writeln("Base b=", b);
}
override string toString() const
{
return to!string(a);
}
}
class Derived : Base
{
this(int _a)
{
super(_a);
}
override void myFun(const int c)
{
writeln("Derived myFun is called ", c);
}
}
class Tester
{
int a;
string s;
this(int zz = -1)
{
if (zz == -1)
return;
a = -1;
s = "HELLOOO";
}
void print()
{
writeln(a, " ", s, " ", s is null);
}
}
void func(string parameter)(MYS mys)
{
mixin("const MYS Newmys = MYS(-1, true, mys." ~ parameter ~ ");");
writeln(Newmys);
}
import std.string;
void main(string[] args)
{
auto name = "GAI&L-AUTO23OCT130CE";
auto ctrForTicker = ctRegex!(`[A-Z-&]+`);
auto matches = matchAll(name, ctrForTicker);
//matches.popFront();
const string matchh = matches.hit();
writeln(matchh);
//writeln(arrrrr.reduce!((a, b) => a.a + b.a));
//auto sumS = reduce!("a.a + b.a")(0, arrrrr);
//writeln(sumS);
writeln("STARTS WITH TEST", "HDT2V2".startsWith("HDT2"));
int a = -1;
if (a)
writeln("hello");
ushort b = 2605;
writeln(a > b);
auto ctr = ctRegex!(`[0-9]+`);
auto c2 = matchAll("WIPRO23MAY380CE", ctr);
writeln(c2);
writeln(c2.hit());
c2.popFront();
writeln(c2.hit(), typeof(c2.hit()).stringof);
int[] arr = [1, 2, 3, 3, 1];
writeln(isStrictlyMonotonic!("a <= b")(arr));
writeln(1 & 0 || 0);
int[] dynamicArr;
writeln(dynamicArr.length);
dynamicArr = new int[10];
writeln(dynamicArr.length, " ", dynamicArr[0]);
Appender!(MyTup[]) app;
app.put(MyTup(10, 1, "AA"));
app.put(MyTup(11, -1, "CC"));
app.put(MyTup(14, 4, "DD"));
writeln(canFind!("a.c == b")(app.data, "CC"));
Base d = new Base(10);
d.myFun(-1);
int[3][2] arrayy;
arrayy[0][0] = -1;
arrayy[0][1] = 0;
arrayy[0][2] = 1;
arrayy[1][0] = 2;
arrayy[1][1] = 3;
arrayy[1][2] = 4;
writeln(getVals!int(arrayy[0].ptr, 2, 3));
const string stringS = "My Name is DVS ZSDSDSD!";
const(char[]) charArr = cast(const char[]) stringS[0 .. $];
assert(stringS.length == charArr.length);
writeln(stringS, " ", charArr.length, " ", charArr[0 .. $], " ",
stringS.sizeof, " ", charArr.sizeof);
/+
MYS mys = MYS(10, false, 'b');
func!("c")(mys);
auto myTup = MyTup(10, 2);
writeln("Add of ", &myTup, " value= ", myTup);
Main base = new Main(myTup);
myTup[0] = 5;
writeln("Add of ", &myTup, " value= ", myTup);
writeln("Add of ", base.myTuple, " value= ", *(base.myTuple));
writeln(MyTup.sizeof, " ", base.myTuple.sizeof);
auto rawVal = MyTup(20, 100);
MyTup* myPtr = &rawVal;
//myPtr = base.getTuple();
//myPtr = base.getTuple();
writeln("Add of zz ", myPtr, " value= ", *myPtr);
//zz.a = 15;
writeln("Add of ", &myTup, " value= ", myTup);
writeln("Add of ", base.myTuple, " value= ", *(base.myTuple));
writeln("Add of zz ", myPtr, " value= ", *myPtr);
const(uint)* bb = &(myPtr.b);
writeln("Add of bb ", bb, " value= ", *bb);
writeln("Add of orig bb ", &(myPtr.b), " value= ", myPtr.b);
+/
//foreach (record; csvReader!(MYS)(s))
//{
// writeln(record);
//}
/*
File f = File("/tmp/deleteme", "w");
f.writeln("My,Name,is,Khan");
f.writeln("No My Name is Divyesh");
f.close();
f = File("/tmp/deleteme", "r");
auto data = f.byLine().front();
writeln(data, " ", typeof(data).stringof);
writeln(data.idup.replace(",", " "));
auto strData = cast(const string)data;
writeln(strData, " ", typeof(strData).stringof);
f.close();
Appender!(int[]) app;
app.put(10);
app.put(20);
struct Seen
{
static bool opCall(int x, int y) { return x<y; }
}
static assert(is(typeof(binaryFun!Seen)));
assert(binaryFun!Seen(1,2));
assert(!binaryFun!Seen(2,1));
ubyte a = 129;
auto b = cast(const byte)a;
writeln(a, b);
auto x = gcd(1000, 5050);
writeln(x);
auto xy = multiSort!("a.a < b.a")(arr);
writeln(arr);
writeln(arr[0 .. $-1]);
MYS[int] testHash = [1:MYS(-1, true), 2:MYS(2, false), 3:MYS(-3, false)];
foreach(key; testHash.keys)
{
auto val = &testHash[key];
val.a = val.a * -1;
}
auto zz = testHash.keys;
auto mm = testHash.keys;
writeln(&zz, " ", &mm);
writeln(cast(const int[])testHash.keys);
auto arrX = testHash.values;
arrX.sort();
writeln(arrX);
arrX[$-1].a = -5;
writeln(arrX);
*/
/*
auto name = "37500CE";
writeln(name.startsWith("BANKNIFTY"));
writeln("Hello D");
auto sp = name.findSplit("22SEP");
writeln(sp);
writeln(sp[0]);
writeln(sp[1]);
writeln(sp[2]);
writeln(sp[2].strip("CE"));
auto mall = matchAll(name, ctr);
writeln(mall);
//mall.popFront();
writeln(mall.front[0]);
auto arr = ["ABC", "SDFC", "ABC", "XZSDD", "ABC"];
auto result = arr.multiSort!("a < b").uniq.array;
writeln(result);
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment