Why would a bash function be enclosed within a variable using quotes? - bash

I've seen how several Bash functions are often constructed, but I've never quite seen something like this:
TEST="
function mined {
// do something here
}
mined
"
I'm a little confused as to why anyone would create a function like above as opposed to simply normally creating the function and calling it? So if I understand this correctly, does the TEST variable represent a call to the mined function?

Related

Understanding a Cobweb code

I'm trying to run a cobweb code in Mathematica and I need the following script:
ClearAll[CobwebPlot]
Options[CobwebPlot]=Join[{CobStyle->Automatic},Options[Graphics]];
CobwebPlot[f_,start_?NumericQ,n_,xrange:{xmin_,xmax_},opts:OptionsPattern[]]:=Module[{cob,x,g1,coor},
cob=NestList[f,N[start],n];
coor = Partition[Riffle[cob,cob],2,1];
coor[[1,2]]=0;
cobstyle=OptionValue[CobwebPlot,CobStyle];
cobstyle=If[cobstyle===Automatic,Red,cobstyle];
g1=Graphics[{cobstyle,Line[coor]}];
Show[{Plot[{x,f[x]},{x,xmin,xmax},PlotStyle->{{Thick,Black},Black}],g1},FilterRules[{opts},Options[Graphics]]]
]
Manipulate[CobwebPlot[Sqrt[3#-1]&,\[Alpha],40,{0,5},PlotRange->{{0,4.5},{0,3.65}},Frame->True,Axes->False,CobStyle->Directive[Dashed,Red],PlotRangePadding->None],{\[Alpha],0.5,4.375}]
I found the script online but I don't understand some features, such as what is the purpose of the following characters, # and &, in the Manipulate[] segment of the code:
Manipulate[CobwebPlot[Sqrt[3#-1]&,\[Alpha],40,{0,5},PlotRange->{{0,4.5},{0,3.65}},Frame->True,Axes->False,CobStyle->Directive[Dashed,Red],PlotRangePadding->None],{\[Alpha],0.5,4.375}]
Can you help me?
See this Mathematica documentation page on pure functions, or what other languages call anonymous functions, or lambda functions.
To give a cute example, suppose you have the function
doItTwice[x_,f_] := f[f[x]];
Now say you want to use this function to square the number seven twice. One way to do this would be to define a square function like this:
square[x_] := x^2;
doItTwice[7, square]
Well, there is a cleaner way to do this by simply writing the square function as a pure function, which would look like (#^2)&. The # is the parameter to the pure function, and the & is just there to indicate that it's a pure function. Really the parenthesis aren't even necessary, so you could write #^2&. Anyways, the following code is now a cleaner way to square seven twice:
doItTwice[7, (#^2)&]

Check if a variable is used in Smarty template

Is there a way to find out which variables are used by a Smarty template? Consider a function taking in a template as an argument and assigning variables to it. Some of the variables require much computation, and hence I don't want to compute them and assign them to the template if they are not needed. I would like something like this:
function addVariables($tpl) {
if($tpl->usesVariable('foo'))
$tpl->assign('foo', computationallyHeavyFunction());
return $tpl;
}
If $tpl = "some text using some variable {$bar}", foo should not be assigned, but if $tpl = "some text using some variable {$foo}", foo should be computed and assigned. Is this possible?
I don't think so. But probably a better(/working) approach is to create a lazy-loading wrapper plugin and use it instead of direct variable calling.
This way your plugin would be called only if it is used anywhere and if you do the computation here/call the computation heavy parts here you can be sure that the computation will be done only if really required.

Bash: No return value when calling a function

I'm new to stackoverflow and bash scripting, so go easy on me! I've been struggling with a bash script I've been writing: when I try to call a function 'main' from my script like so:
variable=$("main -t $path/$i")
I get the error "main -t ./folder: No such file or directory"; any ideas?
Thanks in advance!
EDIT: Thanks Jkbkot, I'm now calling it like this:
variable=$(main -t "$path/$i")
The original error is sorted but something is still up: 'variable' seemingly isn't being assigned the value echoed in the function, though calling the function manually prints the correct value. Why might this happen?
EDIT: It seems I'm calling and echoing correctly, but when calling 'main' it seems to behave differently when called recursively to the initial call. For example it runs fine up to:
variable=$(main -t "$path/$i") #A line within 'main'
Then begins again, as expected, but this time it stops as soon as it comes across a 'break', apparently breaking out of the entire function call rather that just the 'case' it's currently in. Is there some quirk to 'break' in bash that I'm unaware of?
NOTE: Unfortunately, the script is an assignment from my university, and many of its students and teachers use this website, so publicly posting my solution would likely have negative consequences.
You have to call it without the quotes:
variable=$(main -t $path/$i)
and as #janos says, you might need the quotes around the variables in case they might contain spaces, etc.:
variable=$(main -t "$path/$i")

Mathematica - can I define a block of code using a single variable?

It has been a while since I've used Mathematica, and I looked all throughout the help menu. I think one problem I'm having is that I do not know what exactly to look up. I have a block of code, with things like appending lists and doing basic math, that I want to define as a single variable.
My goal is to loop through a sequence and when needed I wanted to call a block of code that I will be using several times throughout the loop. I am guessing I should just put it all in a loop anyway, but I would like to be able to define it all as one function.
It seems like this should be an easy and straightforward procedure. Am I missing something simple?
This is the basic format for a function definition in Mathematica.
myFunc[par1_,par2_]:=Module[{localVar1,localVar2},
statement1; statement2; returnStatement ]
Your question is not entirely clear, but I interpret that you want something like this:
facRand[] :=
({b, x} = Last#FactorInteger[RandomInteger[1*^12]]; Print[b])
Now every time facRand[] is called a new random integer is factored, global variables b and x are assigned, and the value of b is printed. This could also be done with Function:
Clear[facRand]
facRand =
({b, x} = Last#FactorInteger[RandomInteger[1*^12]]; Print[b]) &
This is also called with facRand[]. This form is standard, and allows addressing or passing the symbol facRand without triggering evaluation.

What is an "argument"?

Assuming I have this code:
function question($argument)
{
var $q = "What does ($argument) mean?";
}
Can any tell me is there any other word (or phrase) that defines what an argument is?
I'm asking this because English is my second language, and I can't for life find a word in my language that defines "argument" in "programming".
I understand how arguments work, and what they are for, I just need a synonym word or phrase to be able to translate it to my language to make it easy to use and understand.
The best thing that I came up with (in my language) is (Passed Variable(s)), does that sound right? Is there any better wording?
Thanks
Parameters
Does that help?
("Passed Variables" is close ... and might work fine in your language)
I wouldn't use "passed variable" because arguments do not have to be variables.
Perhaps the most common usage of the term is seen in this example. Consider
// A function definition
function f(x, y) {
....
}
// A function call
f(57/p*q+4, z);
Most people would call x and y parameters, and call 57/p*q+4 and z arguments. Note that parameters are variables (unless the language has pattern-matching, not too common) and that arguments can be arbitrary expressions.
Now you may hear people call x and y "formal parameters" while the arguments are "actual parameters" but IMHO this distinction is a little old-fashioned. I may be wrong though.
The thing is that the argument is the expression that is passed to the parameter in a function call. So maybe "passed expression" is better than "passed variable" at the very least. Have fun translating. One fun thing about the vocabulary of computing is that almost every word (function, procedure, type, label, constant, variable, expression, declaration, statement, operator, argument, parameter, etc.) is just borrowed from a plain old English word. There aren't too many novel terms.
On the calling side it's an argument, on the function side it's a parameter.
"Parameter" vs "Argument"
An argument is what you pass into a function (also known as a subroutine). Arguments are also known as parameters. A function might take an argument and use it to calculate something or modify the argument itself.
Arguments are the variables of the functions that works during calling them.
And
parameters are also the variables of the functions that works during returning value to the program by that function.

Resources