I am trying out CoffeeScript in my Rails 3.1 app. However, I am not able
to figure out how to break long lines in CoffeeScript without getting the
above error
For example, how/where would you break the following line of code
alert x for x in [1,2,3,4,5] when x > 2
if you wanted something like
alert x for
x in [1,2,3,4,5]
when x > 2
In my vimrc, I have set
ts=2, sw=2 and I expand tabs.
And yet, I cannot get something as simple as the line above to work properly.
My Gemfile.lock shows coffee-script-2.2.0 with coffee-script-source 1.1.3
If you have a comprehension that is too long you can break it with \ as #brandizzi mentions, but I think you might have better luck just using comprehensions where they make sense and expanding to 'regular' code where they don't:
alert x for x in [1,2,3,4,5] when x > 2
...can be rewritten as...
for x in [1,2,3,4,5]
alert x if x > 2
...or even...
for x in [1,2,3,4,5]
if x > 2
alert x
In other words, comprehensions are syntactic sugar for short, concise snippets - you don't have to use them for everything.
You're trying to spread a comprehension out over multiple lines, which isn't allowed. It either needs to be on one line, or be a proper loop. Your one line version works as expected, so I'll show the loop version:
for x in [1..5] when x > 2
alert x
You may find it helpful to toss small things like this into the CoffeeScript compiler at http://jashkenas.github.com/coffee-script/ to see if they're compiling to what you'd expect.
I do not understand the inner details of CoffeeScript syntax, so I cannot say what is going wrong in detail. The error is a bit clear, however: you cannot put a newline between the for and its iterator variable. Also, you did not get this error yet, but you cannot put a newline between the iterated object and the when clause. However, if you really want to do it, it is easy: put backslashes at the end of the first and second lines.
console.log x for \
x in [1,2,3,4,5] \
when x > 2
Related
Sometimes I look at code that goes like this:
let x =
(* 1 million lines of code *)
let y =
(* 5 billion lines of code *)
in
(* 5 lines of code *)
in
and I find myself scrolling with my cursor on the right indentation to find where a let ... in starts or ends. Is there a better way to do this in VSCode?
I am not really sure if such a feature exists in VSCode but you can try this extension which I recommend Block Travel it doesn't jump to the start or the end of a block code it seems to be so hard to understand when a block begins and ends but it lets you jump up or down to the nearest all white line.
by default the shortcuts are :
alt+up
alt+shift+up
alt+down
alt+shift+down
but you can update them.
I use worksheet (not document mode).
I know one can do Edit>Remove Output>From Worksheet to "Removes Maple output from all execution groups in a worksheet."
But I could not find a command to do this. I need to do this, since when I run a loop as in
for i from 1 to n do
some_function();
od
And the function above say prints to the screen (i.e. worksheet currently open) lots of information, then the worksheet slows down and I really also do not want to see the logging from all the calls accumulated any way. I just need to see prints from one iteration only at a time.
(Maple has other problems with having large output accumulate in worksheet also, but this is for another topic).
I'd like to do something like this
for i from 1 to n do
some_function();
delete_all_output_in_current_worksheet();
od;
So only one call's output shows up as the some_function is running.
In Matlab, this is easily done using clc which clears the command windows in Matlab.
Is there something like the above in Maple?
Maple 2018.1 on windows.
There is no programmatic way to clear all the output of the current worksheet.
But you can push the running result from some_function() (each time through the loop) to Embedded Components, eg. TextBox, MathContainer, etc.
You don't even have to manually insert such Components from the palettes. Instead you can use the DocumentTools:-Tabulate command to programmatically insert a tabulated collection of components right after the current Execution Group.
For example, as 1D code in a Worksheet, you could have these blocks of code in three separate Execution Groups.
restart;
and,
some_function:=proc(i)
int( sin(x)^i, x);
end proc:
Leafcutoff := 150:
and,
# Put all this in the same Execution Group
for i from 1 to 111 by 10 do
res[i] := some_function(i);
K := MmaTranslator:-Mma:-LeafCount(res[i]);
if K <= Leafcutoff then
L := res[i];
else
L := sprintf("LeafCount > %a",Leafcutoff);
end if;
DocumentTools:-Tabulate(
[["i","LeafCount","result"],
[sprintf("%a",i), K, L]],
':-weights'=[10,10,40],
':-fillcolor'=((T,i,jj)->`if`(i=1,"grey",
"white")),
':-widthmode'=':-pixels',
':-width'=600);
Threads:-Sleep(1.5); # delay at least 1.5 sec
end do:
DocumentTools:-Tabulate([[]]):
I put in a time-delay of 1.5 seconds just to illustrate it working. You can adjust that, or remove it.
The last call to Tabulate is only there to blank it out, when the loop is finished.
The GUI Table inserted by Tabulate actually appears in a region right after the Execution Group. Only one such region appears, per Execution Group. Basically, each call to Tabulate overwrites that region.
If you changed the end do: to end do; then all the regular output would also be shown, as usual for a loop.
I'm getting started with PyDev but am having trouble understanding its code validation.
For example, in a file containing just the code below, PyDev warns "Unused variable i" in the third line, but has no issue with j in the first line.
l1 = ['a' for j in range(10)]
def test():
l2 = ['a' for i in range(10)]
What is triggering PyDev's warning in this example?
(I know to avoid it by adding # #UnusedVariable)
The difference here is that in the first case you're creating global variables and in the second a local variable (global variables created are not reported because they may be used by another module).
You may add an underline (_) in front of the variable (i.e.: _i) to signal that you know it is not used and shouldn't be reported.
In R-studio the processing always stops at the error, if there any way to have this behavior in StatET/Eclipse?
Details:
When using R in Eclipse through StatET, if I run a command such as
x = 7
x = xx + x
x
via "Run Selection in R" (Ctrl + R, Ctrl + R) I get the output
> x = 7
> x = xx + x
Error: object 'xx' not found
> x
[1] 7
In this contrived example there isn't much of a problem, but with many lines of code its easy that the error message gets lost among many pages of output and that the wrong value is ultimately used in the rest of the calculations.
I've tried putting the whole thing curly braces works but this somewhat defeats the speed of having a dedicated command to run the selection. I've also tried options(error = NULL) as per this post but it doesn't fix the problem.
Many thanks.
This problem is a result of options(error = NULL). Remove that and you will get the intended behaviour.
BTW, you should use <- for assignments.
I want to create a predicate, which recognizes a word (in this case: "save") and starts saving the next words, until the sign/word "end" comes.
It should work like this:
?- save.
one
two
end
true.
The predicate for saving:
save(X) :- assert(listitem(X)).
and then I started like this:
save :- read(save).
read:- X -> save(X).
end --> 'end'.
The problem is, that I can add as much words as I want, but if I want to stop the command with "end", the program fails and actually the words have not been saved.
What part of the predicate is wrong? I'd be very happy for some help.
Thank you in advance!
This is some extremely confused code. Here's what you wanted:
:- dynamic listitem/1.
readline(Line) :-
% this is an SWI extension, but it's very handy
read_line_to_codes(user, Codes),
atom_codes(Line, Codes).
save :-
readline(X),
(X \= end -> (assertz(listitem(X)), save)
; true).
Odds are good, somewhere in the code you didn't bring, all you were missing was the effect of the ; true there: that when you find end, you're finished, but not that you failed. But you have a lot of problems here.
save/0 calls read/1, which is a system predicate. All this is going to do is read a word from the user (ending with a period!) and notice that it isn't the word "save". Unfortunately, reading a whole line without periods at the end is a somewhat non-trivial task in Prolog, hence the pile of code in my solution.
read/0 is not called by anything.
The syntax X -> save(X) is almost certainly not what you want. This is the first occurrence of X in the predicate, and so it probably isn't doing you much good to test it conditionally before it has a value.
end --> 'end'. is a DCG rule, but you aren't using phrase/2 anywhere to invoke it (nor are you using end/2 directly with a difference list).
assert/1 is a really bad habit to get into. The ISO predicates asserta/1 and assertz/1 are not only portable, they also give the reader a better idea what the effect on the fact database will be.
Similarly, you have no dynamic declaration for listitem/1, which would raise portability and improve readability.
I would use a 'state machine' approach: it's so simple!
:- dynamic listitem/1.
loop(Mode) :- read(W), loop(Mode, W).
loop(_, stop).
loop(skip, save) :- loop(save).
loop(skip, _) :- loop(skip).
loop(save, W) :- assertz(listitem(W)), loop(save).
test:
1 ?- loop(skip).
|: asd.
|: save.
|: ok.
|: ok1.
|: stop.
true
.
2 ?- listing(listitem).
:- dynamic stackoverflow:listitem/1.
stackoverflow:listitem(ok).
stackoverflow:listitem(ok1).
true.