Forth local variable assigning variables - forth

I have a simple local variable in Forth:
: subtraction { a b } a b - ;
I would like to assign the output of
a b -
to another variable, say c.
Is this possible?

TO works for both VALUEs and local variables, so:
: subtraction { a b | c -- } a b - to c ;

Related

Why is `functionArgs` implemented twice? (i.e, as a primop and in `lib`)

Trying to understand callPackage, so looked up its implementation where it uses lib.functionArgs (source), but there is already a builtins.functionArgs primop, an alias of __functionArgs (implemented in C).
lib.functionArgs is defined as
/* Extract the expected function arguments from a function.
This works both with nix-native { a, b ? foo, ... }: style
functions and functions with args set with 'setFunctionArgs'. It
has the same return type and semantics as builtins.functionArgs.
setFunctionArgs : (a → b) → Map String Bool.
*/
functionArgs = f: f.__functionArgs or (builtins.functionArgs f);
and the __functionArgs attribute above is coming from setFunctionArgs (source):
/* Add metadata about expected function arguments to a function.
The metadata should match the format given by
builtins.functionArgs, i.e. a set from expected argument to a bool
representing whether that argument has a default or not.
setFunctionArgs : (a → b) → Map String Bool → (a → b)
This function is necessary because you can't dynamically create a
function of the { a, b ? foo, ... }: format, but some facilities
like callPackage expect to be able to query expected arguments.
*/
setFunctionArgs = f: args:
{
__functor = self: f;
__functionArgs = args;
};
I understand what setFunctionArgs does, and the comment above its declaration tells why it is necessary, but I can't understand it; both clauses of that sentence are clear but not sure how the first statement prevents the second one to be achieved (without setFunctionArgs, that is).
danbst also tried to elucidate this further,
lib.nix adds __functionArgs attr to mimic __functionArgs builtin. It
used to "pass" actual __functionArgs result down to consumers, because
builtin __functionArgs only works on top-most function args
but not sure what the "consumers" are, and couldn't unpack the last clause (i.e., "builtin __functionArgs only works on top-most function args"). Is this a reference to the fact that Nix functions are curried, and
nix-repl> g = a: { b, c }: "lofa"
nix-repl> builtins.functionArgs g
{ }
?
lib.functionArgs also doesn't solve this problem, but I'm probably off the tracks at this point.
Notes to self
__functor is documented in the Nix manual under Sets.
$ nix repl '<nixpkgs>'
Welcome to Nix version 2.3.6. Type :? for help.
Loading '<nixpkgs>'...
Added 11530 variables.
nix-repl> f = { a ? 7, b }: a + b
nix-repl> set_f = lib.setFunctionArgs f { b = 9; }
nix-repl> set_f
{ __functionArgs = { ... }; __functor = «lambda # /nix/store/16blhmppp9k6apz41gjlgr0arp88awyb-nixos-20.03.3258.86fa45b0ff1/nixos/lib/trivial.nix:318:19»; }
nix-repl> set_f.__functionArgs
{ b = 9; }
nix-repl> set_f set_f.__functionArgs
16
nix-repl> set_f { a = 27; b = 9; }
36
lib.functionArgs wraps builtins.functionArgs in order to provide reflective access to generic functions.
This supports reflection with builtins.functionArgs:
f = { a, b, c }: #...
Now consider the eta abstraction of the same function:
f' = attrs: f attrs
This does not support reflection with builtins.functionArgs. With setFunctionArgs, you can restore that information, as long as you also use lib.functionArgs.
I recommend to avoid reflection because everything that I've seen implemented with it can be implemented without it. It expands the definition of a function to include what should normally be considered implementation details.
Anyway, the primary motivation seems to be callPackage, which can be implemented with normal attrset operations if you change all packages to add ... as in { lib, stdenv, ... }:. I do have a morbid interest in this misfeature that is function reflection, so if anyone finds another use case, please comment.

Lua - Combining two variables

What I am trying to do is create a new variable (let's call it 'C') from adding a string ('Apple') to another variable ('84').
How do I do this?
Example
A = 'Apple'
B = 84
C = 'Apple(84)'
local C = string.format("%s(%i)", A, B)
See the reference for more information on string.format
or, in simple cases like that, you can also just do
local C = A .. "(" .. tostring(B) .. ")"

Lua multiple assignment with tables

This code:
function foo()
return 1, 2, 3
end
bar = {}
bar = {a, b, c = foo()}
produces:
bar.a = nil
bar.b = nil
bar.c = 1
How can this be written so that you get:
bar.a = 1
bar.b = 2
bar.c = 3
without having to write something like this:
function foo()
return 1, 2, 3
end
bar = {}
a, b, c = foo()
bar = {a = a, b = b, c = c}
BLUF
There's no straight forward or elegant way to do this. You'll have to do it manually like this
local r = { f() } --> store all returned values in r
local bar = { }
local c = string.byte 'a' --> start with 'a'
for _, v in ipairs(r) do
local t = string.char(c)
bar[t] = v --> assign each value to respective letter
c = c + 1
end
If you'd had a, b, c = foo() you'd get all the three values assigned to the three variables. However, you've
bar = { a, b, c = foo() }
This table constructor expression will get interpreted as the keys a, b, c getting inserted into the table, with only the last key having an associated value (aside: keys with no associated value are taken as nil; hence a and b never get inserted). Since there's only one variable to take the values returned by foo, except the first everything else it returns are discarded.
Alternatively bar = { foo() } will assign all values returned by foo as array values of bar. However, the key to access these would [1], [2], etc. and not 'a', 'b', etc.
Read below to know when the returned values get discarded and when they don't.
TL;DR
All returned values are retained only when the function call is the last/only expression in a list of expressions; elsewhere all except the first are discarded.
Function call as a statement
In Lua, when we return multiple results from a function, all of them get discarded if the function call is a statement by itself.
foo()
will discard all three return values.
Function call in an expression
If it's used in an expression, only the first will be retained and everything else will be discarded.
x = foo() - 1
print(x) -- prints 0; the values 2, 3 are discarded
Function call in an expression list
The entire list of values returned is retained only when the call appears as the last/only item in a list of expressions. Such list of expressions occur at four places in Lua:
Multiple assignment
E.g. local a, b, c, d = 0, f(). Here b, c, d get the values 1, 2, 3 respectively.
Table constructor
E.g. local t = { 0, f() }. All values returned by f are put into t following the first 0.
Function call arguments
E.g. g(a, f()). g would receive 4, not 2, arguments. a and the three values from f.
return statement
E.g. return 'a', f(). Additional to the string 'a', all values returned by f will be received at the calling end.
In all these situations, had f appeared not as the last expression in the list or wasn't the only expression, then all values it returned except the first would've been discarded.
Multiple assignment statement
In the multiple assignment statement, when the number of values assigned is lesser than number of variables, the extra variables be assigned to nil. When it's the other way around i.e if the number of variables are lesser, the extra values are discarded.
a, b, c = 1, 2 -- a = 1, b = 2, c = nil
a, b, c = 1, 2, 3, 4 -- 4 gets discarded
bar = {}
bar.a, bar.b, bar.c = foo()
bar = {}
local abc = foo()
bar.a, bar.b, bar.c = abc, abc, abc
Simply bar.a, bar.b, bar.c = foo() will only set bar.a to foo(), the other two will be set to nil because they get set to the second and third values respectively, and you've only given one value.
If you can, have foo() return a table formatted the right way.
function foo()
return {a = 1, b = 2, c = 3}
end
bar = foo()

Swap the values of two variables using the tuple unpacking style

In Python, you can swap the values of two variables using the following syntax
a, b = b, a
How to do this in Dart?
Python style tuple unpacking is not supported in Dart. Neither is the assignment of multiple variables as you have in your example. If it is the swap you are after, you could always just do the following:
var a = 10, b = 5, temp;
temp = a;
a = b;
b = temp;
As Shailen Tuli has mentioned, Python-style tuple unpacking is not supported in Dart. You can use Immediately invoked closures.
(tmp) {
a = b;
b = tmp;
}(a)
Which will do the trick.
Thanks to Mr. Randal Schwartz
I tried this:
swapper(int a,int b)
{
return {
"data":
{
'b' : b,
'a' : a
}
}['data'];
}

Ocaml: calling recursive function again

So I have a recursive function that takes in 2 ints, and a out_channel and basically prints line(a,a+1). It should do this until value of a is equal to b. I.e if a = 1, b = 5
line(1,2)
line(2,3)
...line(4,5)
> let rec print_line (out:out_channel)(a:int)(b:int) : unit =
if (a < b) then output_string out ("line("^string_of_int(a)^","^string_of_int(a+1)^")\n")
> ;;
I want to make it recursive where it keeps printing the line(a,a+1) until a is no longer less than b. How exactly do I call it again?
Any help would be appreciated.
So: first check whether a >= b in which case you are done and can return (). Otherwise print one line (the way you did) followed by recursive call to your function, with incremented a. So altogether:
let rec print_line (out:out_channel)(a:int)(b:int) : unit =
if a >= b then
()
else (
output_string out ("line("^string_of_int(a)^","^string_of_int(a+1)^")\n");
print_line out (a + 1) b
)

Resources