Is this element really a set? - nix

If I have a set, I can get easily a element of this set.
{ a = 1; b = 2; c = 3; }
{ a = 1; b = 2; c = 3; }
builtins.typeOf {a = 1; b = 2; c = 3; }
"set"
{ a = 1; b = 2; c = 3; }.a
1
Now I have another set
builtins.typeOf (let f= builtins.getFlake "github:informalsystems/cosmos.nix"; in let ff= f.outputs.devShells.x86_64-linux; in ff.cosmos-shell)
"set"
Ok that a set but it doesn't really look like a set ( «» instead of{})
let f= builtins.getFlake "github:informalsystems/cosmos.nix"; in let ff= f.outputs.devShells.x86_64-linux; in ff.cosmos-shell
«derivation /nix/store/drfgr6zlkbv70wmml5n8h9x2wj29kk39-nix-shell.drv»
and I can't take the field derivation
builtins.attrNames (let f= builtins.getFlake "github:informalsystems/cosmos.nix"; in let ff= f.outputs.devShells.x86_64-linux; in ff.cosmos-shell
give others field. That are the elements used to build cosmos-shell
My question is why this command
let f= builtins.getFlake "github:informalsystems/cosmos.nix"; in let ff= f.outputs.devShells.x86_64-linux; in ff.cosmos-shell
doesn't return
a set with the element defining cosmos-shell
or
another type giving the derivation where cosmos-shell is defined and with method to get the element defining cosmos-shell
Why am I asking this question
Normally I can easily export a set as a json string
builtins.toJSON { a = 1; b = 2; c = 3; }
"{"a":1,"b":2,"c":3}"
But in this case I can't.
builtins.toJSON (let f= builtins.getFlake "github:informalsystems/cosmos.nix"; in let ff= f.outputs.devShells.x86_64-linux; in ff.cosmos-shell)
'>""/nix/store/wf3inmq4x93s3z32m90xz8d10gkz55zb-nix-shell""'

A derivation is little more than an attribute set with the name type having the value "derivation". (Whether such a set can be used as a derivation is meant to be used is another matter.) The Nix repl recognizes all such sets and diplays them in a special way.
> { type = "derivation"; }
«derivation ???»

Related

Testing for identical objects in Dart

According to the documentation, the function identical checks whether two references are to the same object.
With that in mind, I don't understand why the following is the case:
int a = 1;
int b = 1;
print(identical(a, b)); // prints 'true'
Map c = { 1: 'y' };
Map d = { 1: 'y' };
print(identical(c, d)); // prints 'false'
I'd expect both calls to return 'false'.
identical compares references. a and b are references to a compile time literal 1. Thus they are identical.

How can I ask the user of any program for a value in Swift?

In JS it is called "prompt", but it is obviously not the same keyword in Swift. So my problem actually is that I want to ask the user of any program for a value. So how do I ask him and how do I safe that answer into a variable?
So this is how I would write it in Javascript for example when I want to use the input as a value for my calculations:
var n1 = prompt("give me a value");
var n2 = 3;
var i = 0;
var sum = 0;
while(i < n1){
sum = sum + n2;
i += 1;
}
document.write(sum)
And now I don't really know how to write the same in swift. I just want to ask the user for a value in "n1".
var n1 = 9;
var n2 = 5;
var i = 0;
var sum = 0;
while(i < n1){
sum = sum + n2;
i += 1;
}
print(sum)
How shall I write this in "var n1" ?
You can do it in Command Line Tool.
Create New project - mac os tab - Command Line Tools.
You can ask user for input using readLine().
let a = readLine()
if let input = a {
print(input)
}

How to do BigInt arithmetic in Dart 2.x, specifically division?

Dart documentation says that BigInt division returns a value of type 'double'. This is a problem. To illustrate, here are two implementations of an algorithm involving division. The first is in Kotlin, the second is in Dart. The Dart version runs accurately for small numbers but loses precision for larger numbers.
Kotlin
import java.math.BigInteger
fun height(n: BigInteger, m: BigInteger): BigInteger {
var m1 = m
var s = BigInteger("1")
var b = BigInteger("1")
var ans = BigInteger("0")
var i = 0
while (i < n.toInt()) {
s *= m1--
s /= b++
ans += s
i++
}
return ans
}
Dart
BigInt height(int n, int m) {
var m1 = m; // new BigInt.from(m);
var s = 1.0; // new BigInt.from(1);
var b = 1.0; // new BigInt.from(1);
var ans = new BigInt.from(0);
var i = 0;
while (i < n) {
s *= m1--;
s /= b++;
ans += BigInt.from(s);
i++;
}
return ans;
}
As you can see from the commented out Dart code, I have tried various ways to use BigInt.
Here is an example input with answer. The erroneous Dart answer is given below.
height(13, 550),
equals(BigInt.parse('60113767426276772744951355')));
The erroneous Dart answer is --> 60113767426276764034189615
Can someone show me the best way to do the job in Dart v2.x?
The following code works.
BigInt height(int n, int m) {
var m1 = new BigInt.from(m);
var s = new BigInt.from(1);
var b = new BigInt.from(1);
var ans = new BigInt.from(0);
var i = 0;
while (i < n) {
s *= m1;
m1 -= new BigInt.from(1);
s = s ~/ b;
b += new BigInt.from(1);
ans += s;
i++;
}
return ans;
}
Changes:
x++ and x-- are equivalent to x = x + 1 and x = x - 1 but BigInt.+ and BigInt.- only accept BigInt values... so there's a compiler error.
BigInt./ returns a double and this is not what you want here. You need to use the BigInt.~/ operator instead.

What does `?` mean after a set in nix language?

What does the ? in the last line means and why does this evaluates to true?
let
attr = {a="a"; b = 1; c = true;};
in
ex7 = ! attr ? a == false;
I've also tried
{a="a"; b = 1; c = true;} ? false
also evaluate to true, but
! {a="a"; b = 1; c = true;} ? a==true
! {a="a"; b = 1; c = true;} ? a==1
both evaluate to false.
I've found it in the manual of nix language https://nixos.org/nix/manual/#sec-language-operators .
Test whether set e contains the attribute denoted by attrpath; return
true or false.

Emulate string to label dict

Since Bazel does not provide a way to map labels to strings, I am wondering how to work around this via Skylark.
Following my partial horrible "workaround".
First the statics:
_INDEX_COUNT = 50
def _build_label_mapping():
lmap = {}
for i in range(_INDEX_COUNT):
lmap ["map_name%s" % i] = attr.string()
lmap ["map_label%s" % i] = attr.label(allow_files = True)
return lmap
_LABEL_MAPPING = _build_label_mapping()
And in the implementation:
item_pairs = {}
for i in range(_INDEX_COUNT):
id = getattr(ctx.attr, "map_name%s" % i)
if not id:
continue
mapl = getattr(ctx.attr, "map_label%s" % i)
if len(mapl.files):
item_pairs[id] = list(mapl.files)[0].path
else:
item_pairs[id] = ""
if item_pairs:
arguments += [
"--map", str(item_pairs), # Pass json data
]
And then the rule:
_foo = rule(
implementation = _impl,
attrs = dict({
"srcs": attr.label_list(allow_files = True, mandatory = True),
}.items() + _LABEL_MAPPING.items()),
Which needs to be wrapped like:
def foo(map={}, **kwargs):
map_args = {}
# TODO: Check whether order of items is defined
for i, item in enumerate(textures.items()):
key, value = item
map_args["map_name%s" % i] = key
map_args["map_label%s" % i] = value
return _foo(
**dict(map_args.items() + kwargs.items())
)
Is there a better way of doing that in Skylark?
To rephrase your question, you want to create a rule attribute mapping from string to label?
This is currently not supported (see list of attributes), but you can file a feature request for this.
Do you think using "label_keyed_string_dict" is a reasonable workaround? (it won't work if you have duplicated keys)

Resources