function d(x,y)
x=3
y=8
end
d(dog,cat)
This doesn't recognize dog as a variable
how do I get dog=3, cat=8
?
dog = 3; cat = 8
?
or if you want to be fancy
dog, cat = 3, 8
But whether or not that's something you want to to do maybe depends on what you're ultimately trying to accomplish
It sounds like you're trying to run a function inside out. That's now how the data flows. When you write d(dog, cat)
, julia looks up what values are associate with the variables dog
and cat
and then passes those variables into the function d
.
Inside the function, you then bind those values to the arguments, x
and y
, but then inside the function body you then rebind x
and y
to 3
and 8
, completely ignoring whatever values were passed in
If you want d
to assign the values 3
and 8
to new variables dog
and cat
, then you can't do that with a function, but you can do it using a macro.
julia> macro d(x, y)
ex = quote
$x = 3
$y = 8
end
esc(ex)
end
@d (macro with 1 method)
julia> @d(dog, cat)
8
julia> dog + cat
11
That works, but apparently I can't do the same thing in a dataframes, or maybe I'm trying to send data both ways
macro transaction(newdate,newcredit,credit_amount,newdebit, debit_amount)
if credit_amount != debit_amount
"Error--Debit and Credit must match"
else
ex= quote
newtransaction = DataFrame(
$newdate=[newdate]
$newcredit = [credit_amount],
$newdebit = [debit_amount])
end
esc(ex)
end
end
Macros are not like functions. They just operate on syntax.
Ok, so do I then need to somehow have a macro and a function?
I would recommend just writing a function and no macro.
And then just writing
new_transaction = transaction(newdate,newcredit,credit_amount,newdebit, debit_amount)
instead of trying to write a macro
If you really want a macro for this though, I guess you could write
julia> using DataFrames
julia> macro transaction(newdate,newcredit,credit_amount,newdebit, debit_amount)
ex= quote
if $credit_amount != $debit_amount
"Error--Debit and Credit must match"
else
newtransaction = $DataFrame(
newdate=[$newdate],
newcredit = [$credit_amount],
newdebit = [$debit_amount]
)
end
end
esc(ex)
end
@transaction (macro with 1 method)
julia> @transaction(1, 2, 3, 4, 3)
1×3 DataFrame
Row │ newdate newcredit newdebit
│ Int64 Int64 Int64
─────┼──────────────────────────────
1 │ 1 3 3
macro transaction(newdate,newcredit,credit_amount,newdebit, debit_amount)
ex= quote
if $credit_amount != $debit_amount
"Error--Debit and Credit must match"
else
newtransaction = $DataFrame(
Date=[$newdate],
$newcredit = [4],
$newdebit = [4]
)
end
end
esc(ex)
end
@transaction("Jan 1, 1900", chequing, 3, personal, 3)
won't let me add a variable, but I'm not sure which one.
wo
Last updated: Nov 06 2024 at 04:40 UTC