Print string domain values with comma separation in Chapel - chapel

I have a domain I'd like to output with commas. In Python I can use the string .join() method, being fed by a list .sort()-ed product, but in Chapel I am not getting the right results.
var names = { "anze kopitar",
"tyler toffoli",
"drew doughty",
"jeff carter",
"tanner pearson"
};
writeln( names );
writeln( names.sorted() );
writeln( ",".join( names ) );
writeln( ",".join( names.sorted() ) );
I'd like the last line to read
anze kopitar,drew doughty,jeff carter,tanner pearson,tyler toffoli

In Chapel 1.16, string.join only supports varargs, tuples, and arrays as arguments. For now you will need to convert your domain to one of those types before joining:
var dom = {"apple", "orange", "carrot"};
var A = dom.sorted(); // 'A' is an array
const s = ",".join(A);
writeln(s);
The output is:
apple,carrot,orange

Related

power bi DAX hierarchical table concatenation of names

Since two days I'm on a problem and I can't solve it so I come here to ask some help...
I have that bit of dax that basically take the path of a hierarchical table (integers) and take the string names of the 2 first in the path.
the names I use:
'HIERARCHY' the hierarchical table with names, id, path, nbrItems, string
mytable / addedcolumn1/2 the new table used to emulate the for loop
DisplayPath =
var __Path =PATH(ParentChild[id], ParentChild[parent_id])
var __P1 = PATHITEM(__Path,1) var __P2 = PATHITEM(__Path,2)
var l1 = LOOKUPVALUE(ParentChild[Place],ParentChild[id],VALUE(__P1))
var l2a = LOOKUPVALUE(ParentChild[Place],ParentChild[id],VALUE(__P2))
var l2 = if(ISBLANK(l2a), "", " -> " & l2a)
return CONCATENATE(l1,l2)
My problem is... I don't know the number of indexes in my path, can go from 0 to I guess 15...
I've tried some things but can't figure out a solution.
First I added a new column called nbrItems which calculate the number of items in the list of the path.
The two columns:
Then I added that bit of code that emulates a for loop depending on the number of items in the path list, and I'd like in it to
get name of parameters
concatenate them in one string that I can return and get
string =
var n = 'HIERARCHY'[nbrItems]
var mytable = GENERATESERIES(1, n)
var addedcolumn1 = ADDCOLUMNS(mytable, "nom", /* missing part: get name */)
var addedcolumn2 = ADDCOLUMNS(addedcolumn1, "string", /* missing part: concatenate previous concatenated and new name */)
var mymax = MAXX(addedcolumn2, [Value])
RETURN MAXX(FILTER(addedcolumn2, [Value] = mymax), [string])
Full table:
Thanks for your help in advance!
Ok, so after some research and a lot of try and error... I've came up to a nice and simple solution:
The original problem was that I had a hierarchical table ,but with all data in the same table.
like so
What I did was, adding a new "parent" column with this dax:
parent =
var a = 'HIERARCHY'[id_parent]
var b = CALCULATE(MIN('HIERARCHY'[libelle]), FILTER(ALL('HIERARCHY'), 'HIERARCHY'[id_h] = a))
RETURN b
This gets the parent name from the id_parent (ref. screen).
then I could just use the path function, not on the id's but on the names... like so:
path = PATH('HIERARCHY'[libelle], 'HIERARCHY'[parent])
It made the problem easy because I didn't need to replace the id's by there names after this...
and finally to make it look nice, I used some substitution to remove the pipes:
formated_path = SUBSTITUTE('HIERARCHY'[path], "|", " -> ")
final result

How to do an in-place `expand` on a domain in Chapel

The expand command in Chapel returns a new domain. I would like to increase a domain by one kinda like
var d: domain(1) = {1..5};
writeln(d);
--{1..5}
expand(d)(1);
writeln(d);
--{1..6};
As of Chapel 1.15 there is no in-place option for the expand method on domains. You would need to assign the result of expand to the desired domain:
var eDom = {1..5};
eDom = eDom.expand(1);
writeln(eDom); // {0..6}
It doesn't sound like expand is what you want though, because expand will grow the domain in both directions in each dimension. To add one index to a rectangular domain, you can assign a domain literal to your domain:
var rDom = {1..5};
const hi = rDom.last + 1;
rDom = {rDom.first..hi};
writeln(rDom); // {1..6}
For irregular domains you can use the add method:
var aDom = {1, 3, 5, 7}; // an associative domain
aDom.add(9);
writeln(aDom.sorted()); // 1 3 5 7 9
Note that you cannot use the add method on rectangular domains. This is defined in section 19.8.6 in version 0.983 of the Chapel language specification.
A few online experiments on domain expansion:
some worked as documented, some not:
var d: domain(1) = {1..5};
writeln( d ); // SET {1..5}
// {1..5}
var e: domain(1) = d.expand(1);
writeln( e ); // OK, DOMAIN d == {1..5} EXTENDED ON BOTH ENDS INTO {0..6}
// {0..6}
var AonD: [d] int;
AonD.push_back(1);
writeln( AonD.domain ); // OK, DOMAIN EXTENDED INDIRECTLY ON DESIRED END INTO {1..6}
// {1..6}
// var f: domain(1) = {1..5}; // NEW {1..5} - A NON-SHARED, NON-USED ( NON-MAPPED ) DOMAIN
// f.add(6); // FAILS v/s A PROMISE IN: http://chapel.cray.com/docs/master/builtins/internal/ChapelArray.html#ChapelArray.add
// f += 6; // FAILS
// writeln( f );

Can one create an object to store multiple domains?

I have some code which I think should look like:
on Locales[0] {
var slice: domain(1) = {0..#widthOfLocaleMatrix};
on Locales[1] {
slice(0) = A.localSubdomain();
}
var localSlice: [slice(0)] int = A[slice(0)];
}
Basically, I am trying to fetch multiple slices of data from the other numLocales - 1 locales. Can I create an object to store the localSubdomain's from all other locales? I think I can work around this, but I was curious.
To store multiple domains, you'll want to create an array of domains (or some other collection of domains). Specifically, the main problem with the code above is that it is seemingly trying to index into a domain ( slice(0) ) -- keep in mind that domains are merely index sets, not arrays/maps from indices to values.
The following sample program creates a distributed array ( A ) whose distribution we want to interrogate and an array of domains ( slicePerLocale ) that we'll use to keep track of who owns what. It populates slicePerLocale via the localSubdomain() query to determine the subdomain that each locale owns and stores that in the respective element of slicePerLocale. Finally, it prints out what it has learned:
use BlockDist;
config const n = 10;
var D = {1..n, 1..n} dmapped Block({1..n, 1..n});
var A: [D] real;
var slicePerLocale: [LocaleSpace] domain(2);
coforall loc in Locales do
on loc do
slicePerLocale[loc.id] = A.localSubdomain();
for (loc, slice) in zip(LocaleSpace, slicePerLocale) do
writeln("locale ", loc, " owns: ", slice);
Running this on four locales with the default problem size of 10 results in:
locale 0 owns: {1..5, 1..5}
locale 1 owns: {1..5, 6..10}
locale 2 owns: {6..10, 1..5}
locale 3 owns: {6..10, 6..10}

What is the difference between a list of tuples and a list of objects?

Please see I want list employee objects. So I have below two options.
List<Tuple<int, string, string, string>>
List<Employee>where Employee is class contains 4 properties.
My doubt is what should I use(tuple or list of employee object?
If it is List<Employee> then in which scenario I should use List<Tuple<int, string, ...>>.
You should not use tuples unless you are doing some sort of arithmetic operation where tuple would be an acceptable and widely understood method of supplying values. Tuples make it a maintenance nightmare for anyone who is not familiar with your process as you built it.
Edit: Think about the difference between seeing:
var employeeList = DAL.getEmployees();
var activeEmployees = employeeList.Where(employee => employee.IsActive);
vs
var employeeTuple = DAL.getEmployees();
var activeEmployees = employeeTuple.Where(employee => employee.Item3);
In the second example, I know THAT you created an active employee list, but I don't know HOW you did it.
That's rather obvious. If you already have the Employee class then using List<Employee> is straightforward:
List<Employee> list = new List<Employee>();
list.Add( e );
...
Employee e = list.Where( i => i.Name == "John" ).FirstOrDefault();
whereas using List<Tuple<...>> is at least cumbersome:
List<Tuple<....>> list = new List<Tuple<....>>();
list.Add( new Tuple<...>( e.Name, e.Surname, e.Whateverelse, e.YetAnother ) );
...
// retrieve the tuple
var tuple = list.Where( i => i.Item1 == "John" );
// make Employee out of it
Employee e = new Employee( e.Item1, e.Item2, e.Item3, e.Item4 );

List.shuffle() in Dart?

I'm looking every where on the web (dart website, stackoverflow, forums, etc), and I can't find my answer.
So there is my problem: I need to write a function, that print a random sort of a list, witch is provided as an argument. : In dart as well.
I try with maps, with Sets, with list ... I try the method with assert, with sort, I look at random method with Math on dart librabry ... nothing can do what I wana do.
Can some one help me with this?
Here some draft:
var element03 = query('#exercice03');
var uneliste03 = {'01':'Jean', '02':'Maximilien', '03':'Brigitte', '04':'Sonia', '05':'Jean-Pierre', '06':'Sandra'};
var alluneliste03 = new Map.from(uneliste03);
assert(uneliste03 != alluneliste03);
print(alluneliste03);
var ingredients = new Set();
ingredients.addAll(['Jean', 'Maximilien', 'Brigitte', 'Sonia', 'Jean-Pierre', 'Sandra']);
var alluneliste03 = new Map.from(ingredients);
assert(ingredients != alluneliste03);
//assert(ingredients.length == 4);
print(ingredients);
var fruits = <String>['bananas', 'apples', 'oranges'];
fruits.sort();
print(fruits);
There is a shuffle method in the List class. The methods shuffles the list in place. You can call it without an argument or provide a random number generator instance:
var list = ['a', 'b', 'c', 'd'];
list.shuffle();
print('$list');
The collection package comes with a shuffle function/extension that also supports specifying a sub range to shuffle:
void shuffle (
List list,
[int start = 0,
int end]
)
Here is a basic shuffle function. Note that the resulting shuffle is not cryptographically strong. It uses Dart's Random class, which produces pseudorandom data not suitable for cryptographic use.
import 'dart:math';
List shuffle(List items) {
var random = new Random();
// Go through all elements.
for (var i = items.length - 1; i > 0; i--) {
// Pick a pseudorandom number according to the list length
var n = random.nextInt(i + 1);
var temp = items[i];
items[i] = items[n];
items[n] = temp;
}
return items;
}
main() {
var items = ['foo', 'bar', 'baz', 'qux'];
print(shuffle(items));
}
You can use shuffle() with 2 dots like Vinoth Vino said.
List cities = ["Ankara","London","Paris"];
List mixed = cities..shuffle();
print(mixed);
// [London, Paris, Ankara]