What's the meaning of this line of CoffeeScript? - coffeescript

I was reading through the Journo's source code and I stumbled upon this line of code:
markdown = _.template(source.toString()) variables
What is variables doing here? Is _.template(source.toString()) variables valid stntax at all?
Here's the function wrapping that line of code:
Journo.render = (post, source) ->
catchErrors ->
do loadLayout
source or= fs.readFileSync postPath post
variables = renderVariables post
markdown = _.template(source.toString()) variables
title = detectTitle markdown
content = marked.parser marked.lexer markdown
shared.layout _.extend variables, {title, content}

Yes, it is valid. Parenthesis are optional (sometimes) in CoffeeScript when invoking a function, so it is taking the result of template and invoking it with arguments. It compiles to this JavaScript:
_.template(source.toString())(variables);
From the CoffeeScript documentation:
You don't need to use parentheses to invoke a function if you're passing arguments. The implicit call wraps forward to the end of the line or block expression.

_.template compiles a template specified by source.toString(). A template is a function, which is then called. variables is a parameter for that function (just like postPath post are parameters for fs.readFileSync).
See also the docs for _.template

The question was nicely answered, but to help the OP with future coffee stunts, a great way to anser these koans is to
Go to the coffeescript.org site
Click on "Try coffeescript"
Cut/Paste the puzzle into the coffeescript section
Bingo! You see the generated javascript.
I admit to puzzling over coffeescript at times, and this is abs fab .. and saves headaches.

Related

Eval() for setting Form controls property

I work on an Access VBA 2013 application, and this part of the project focuses on form controls and buttons.
I want to lock/unlock control modification thanks to buttons set aside.
For more genericity, I wanted the subs called by the events call homemade functions:
Sub LockAssociateControl(fctName As String, val As Boolean)
Dim code As String
code = "Forms!" & getModuleName & ".Controls!" & fctName & ".Locked = " & CStr(val)
Debug.Print (code) 'just to test
Eval code
End Sub
(getModuleName is also a homemade function that returns the right name of the module calling the function)
For example, this one is called like below:
Private Sub FirstName_Exit(Cancel As Integer)
Call LockAssociateControl("FirstName", True)
End Sub
and in "code" variable, it sets "Forms!Module1.Controls!Name.Locked = True" (with Module1 generated by getModuleName, and Name as the parameter (I haven't found better yet)).
Now I want this code evaluated in order to avoid me from hard coding every event sub.
My problem is that an error occurs at the Eval() line and says:
Error Code: 2770. The object to which you reffered in the Visual Basic procedure as an OLE object is not an OLE object
I've looked around StockOverflow and others forums to find out what's wrong with Eval(), and I found that this function's behavior is not made for what I want. So I tried to find another way to do that, as Evaluate function doesn't exist in Access, etc, but I found nothing really helpful.
I tried to use DoCmd or Call or various transformations on the parameter string of Eval(), but nothing has worked so far...
So here's my question: Do any of you have a solution for that problem? And if not, do you know an alternative not to have to write in every event function full statements like: Forms!Module1.Controls!Name.Locked = False?
You can do what you want easier without using Eval() ...
Sub LockAssociateControl(fctName As String, pVal As Boolean)
Forms(getModuleName).Controls(fctName).Locked = pVal
End Sub
Note I changed the argument name from val to pVal to avoid confusing it with the Val() function.

Doxygen: Add new type or structure

I want to use doxygen to document a c-like language.
I got some issues to solve keywords which are unknown in the context.
One example, I have to use a callback function called
on timer
{
//normal c- code
}
My question is now, can I adopt doxygen to accept the new keyword?
I would like to add this keyword like a function or variable acc. to
/** This timer is used for something. */
on timer
{
}
or maybe
/** \ontimer This timer is used for something. */
on timer
{
}
The documentation of doxygen describes something with ALIASES or \xrefitem but as I understand I can only generate new sections for known types or am I wrong?
Actually I am surrounding the unknown code with a condition block out to avoid errors in the generated output.
As I understand is "on" a keyword which doxygen cannot interpret. One solution could be to declare the keyword on as a predfined macro in the doxyile by using the the PREDEFINED tag as follows:
PREDEFINED = on=
Yes, the = at the end is not a typo! This tells the preprocessor of doxygen to substitute the keyword on with a empty string. Note that you have to set ENABLE_PREPROCESSING to YES.
If the on keyword only appears before callback functions you could alternatively set the PREDEFINED macro to void:
PREDEFINED = on=void

Why is this Coffeescript invalid?

I'm playing around with Coffeescript, trying to convert a JavaScript file to Coffeescript. This is valid JavaScript:
element(by.model('query.address')).sendKeys('947');
This is invalid Coffeescript:
element(by.model('query.address')).sendKeys('947')
What is invalid about the Coffeescript? Coffeelint says "unexpected BY".
CoffeeScript uses the by keyword to let you use a specific step when looping through a range.
From the documentation:
To step through a range comprehension in fixed-size chunks, use by, for example:
evens = (x for x in [0..10] by 2)
Since JavaScript doesn't use by it's valid. For CoffeeScript, try renaming the by to something else.
In response to the comment, since Protractor provides its own by global variable, one idea is to alias it via CoffeeScript's embedded JavaScript syntax (code surrounded by back-ticks), then continue using CoffeeScript and the alias throughout your code.
You'll need to test this type of code:
ptorBy = `by`
element(ptorBy.model('query.address')).sendKeys('947')
Where ptor is just my short-hand for "Protractor." This translates to the following JavaScript:
var ptorBy;
ptorBy = by;
element(ptorBy.model('query.address')).sendKeys('947');

Why does this wildcarded function tells me it has the wrong number of parameters?

The offending code was:
<console>:47: error: wrong number of parameters; expected = 2
terms.foldLeft(r.unitA)(r.add(_, _.eval(x)))
I solved my problem by writing:
terms.foldLeft(r.unitA)((a,b) => r.add(a, b.eval(x)))
But I'd still like to know what prevented my initial attempt?
From what I've read on this type of issue, when you use "_" as a place holder for an anonymous parameter of a function, the scope of that function is the innermost parenthesis containing it. So when you wrapped your two placeholders with r.add(), the scope of the params is lost. Check out this link and see if it helps explain the rules better.
http://www.scala-lang.org/node/2916
Here is the section of the SLS 6.23:
http://iainmcgin.github.io/scala-ref-markdown/#placeholder-syntax-for-anonymous-functions
Updated link:
http://www.scala-lang.org/files/archive/spec/2.11/06-expressions.html#placeholder-syntax-for-anonymous-functions
Daniel Sobral's post says:
"When you use "_" as a place holder for an anonymous parameter of a
function, the scope of that function is the innermost parenthesis
containing it. Most of the time.
Updated spin: I think the syntax explanation from the spec is easier to get, that the placeholder doesn't escape an enclosing Expr. There are various duplicate questions.

Doxygen and Fortran with KIND parameters

I'm using Doxygen to document a Fortran code and I have variables declared such as:
REAL(KIND=8), PARAMETER :: myParam = 1.0_8
but Doxygen gets confused and seems to think REAL is a function and throws:
warning: documented function `real' was not declared or defined.
I have OPTIMIZE_FOR_FORTRAN set to YES so that's not the issue.
Is there a way to rectify this without having to wrap some pre-processor guard around my variables to declare them as REAL without the KIND parameter when building documentation?