I'm trying to convert this to CoffeeScript, but coming up short:
var paras = document.getElementsByClassName('hi');
while(paras[0]) {
paras[0].parentNode.removeChild(paras[0]);
}
How do I do this in CoffeeScript?
Per #caffeinated.tech's comment, it'd be beneficial for people answering the question and for people visiting this question in the future to see what you've tried.
That all being said, you could use use CoffeeScript's list-like comprehensions to do something like this:
his = document.getElementsByClassName('hi')
hi.remove hi for hi in his
Related
I want to do this:
var arr = [0,1,0,1,0,2,1,0,0]
switch arr {
case [1,_,_,_,_,_,_,_]: print("Yay")
default: print("Nay")
}
But I get this error: "'_' can only appear in a pattern or on the left side of an assignment"
I searched around on this error message and "wildcards swift array switch statements" and it seems like it is not possible to use wildcards in this context, but there may be some sort of workaround using 'operator overloading' with something involving ~=. However, this is way over my current knowledge, I haven't found a case example exactly like mine, and there seems to be a fair amount of implied concern that operator overloading is bad for some reason. Also, the examples I've found that is sort of like my case look very convoluted. I'm a self-taught amateur just trying to hack together a basic app to make my work life easier, so apologies for any stupid questions but I'd like to better understand the following:
Why can't I use wildcards in this context? It seems like they work with tuples but not arrays for some reason, and I don't get why one would work but not the other.
Is operating overloading actually 'bad' and is there a relatively straightforward way to implement this functionality?
If not- can someone recommend an alternate approach? I chose this approach because I have two arrays, one for a series of questions, one to record responses to each question. I want to implement pretty complicated logic where the configuration of responses to prior questions will determine which question is presented next (example below). I wasted a bunch of time drowning in convoluted 'if' statements before realizing I needed a cleaner approach and this seemed to be it. However, without wildcards or something similar, it would not be practical to explicitly define every case. Could someone recommend a different approach in this situation? Obviously, this is a streamlined example and there would be many more defined cases. Thanks in advance.
var responseArr = [0,2,1,2,1,0,0] // 0 = unasked, 1 = asked answered no, 2 = asked answered yes
switch responseArr {
case: [0,_,_,2,1,0,_]: nextQuestion = questionArr[0] // If 'yes' to question 4, no to question 5, and questions 1 and 6 were not asked yet, ask question 1
case: [1,_,_,2,1,0,_]: nextQuestion = questionArr[5] // If 'yes' to question 4, no to question 5, no to question 1, and question 6 was not asked yet, ask question 6
...
default: print("error")
}
It seems like they work with tuples but not arrays for some reason
The reason is that a tuple is a multi-value type, each value is evaluated separately, on the other hand an array is one value.
A valid syntax is
switch arr {
case arr where arr[0] == 0 && arr[5] == 2 : print("Yay")
default: print("Nay")
}
The tuple syntax would be something like
switch (arr[0], arr[1], arr[2], arr[3], arr[4], arr[5], arr[6], arr[7], arr[8]) {
case (0,_,_,2,1,0,_,_,_): print("Yay")
default: print("Nay")
}
This is heavily implied by Joakim's and vadian's responses, but for the benefit of other beginners that might come across this- a very simple conversion of the array to a tuple (as below) seems to work perfectly for what I was trying to accomplish in the first place, and only really requires addition of one intermediary variable and replacing some brackets with parentheses:
var arr2 = [0,1,0,1,0,2,1,0,0]
let arr2AsTuple = (arr2[0],arr2[1],arr2[2],arr2[3],arr2[4],arr2[5],arr2[6],arr2[7],arr2[8])
switch arr2AsTuple {
case (1,_,_,_,_,_,_,_,_): print("Yay")
default: print("Nay")
}
I'm new in scala language I have a listbuffer :
var oldQuestions: Seq[Question] = section.questions
var newQuestions: ListBuffer[Question] = new ListBuffer()
So all I need is,to loop over the newQuestions list and access to one question based on her id and delete it.
Any help would be very appreciated.
Don't use vars, and mutable collections. It is a really, really rare occurrence that you actually need either of those in scala. So, for now, until you get sufficient grip on the language to be able to tell when those rare cases happen, just pretend these things don't exist. Learn to write good functional code before you explore mutability.
To answer your question:
val newQuestions: Seq[Question] = section.questions.filterNot(_.id == idToDelete)
I have this Swift code:
for var a = 0; a < 10; a++{
println(a)
}
There is an compile error on
a++{
Can anyone explain why?
You just need to add a space between a++ and {:
for var a = 0; a < 10; a++ {
println(a)
}
If you want to use the "{" against your variable you need to use the variable name between the "+" and the "{" as per the swift documentation
for var a = 0; a < 10; ++a{
println(a)
}
Another option as suggest ABakerSmith is to space the operators "+" and "{"
I particularly prefer the first option as it keeps my code consistent as I never use space before my "{" and also it is how is used through all apple documentation
#vacawama and ABakerSmith already told you how to fix it. The reason is that Swift uses whitespace to figure out the difference between multi-character expressions and separate expressions. It requires whitespace between symbols where languages like C don't. It still trips me up sometimes.
Also, for future reference, Swift code allows for two different For loop syntaxes.
for <initialization>; <condition>; <increment> { <statements> }
or when in an array or a collection
for <identifier> in <collection> { <statements> }
But both of them require the attention to detail on where your spaces are in the code, so be careful.
Also, since it seems like you may be rather new at Swift, I recommend checking out these awesome resources that make the journey of learning Swift a lot easier.
Apple's free 500 page Swift Code Reference Guide
Thinkster.io has a great guide for everything swift, even quick little cheat sheets to keep handy for any questions you might have in the future. When I learned swift I used this site a lot!
If you want to build a cool little game using swift start here!
Hope that helped! Swift is a great programming language that has a lot to offer, and I hope you have fun learning it!
This is a really silly question, but how can you do convenient formatting of log strings in Play Framework 2 (and in Scala?).
I've googled but its very difficult to find an example, essentially most links are talking about configuring Logback in the first place which I've done fine.
I'm basically trying to find the best stylistic way to do something like:
if(Logger.isDebugEnabled)
Logger.debug("Modified: Id = '" + real_session_id + "', Modified = " + modified.toString)
Coming from a C# background (and log4net) I'd assume you could do something like:
if(Logger.isDebugEnabled)
Logger.debug("Modified: Id = '{0}', Modified = {1}", real_session_id, modified.toString)
But I can't see how this would work with the trait the way it is defined. I've also seen vague references to how you might be able to avoid checking Logger.isDebugEnabled by using a lazy evaluative syntax like:
Logger.debug("Modified: Id = ${real_session_id}, Modified = ${modified.toString}")
That uses Scala macros - but again, that doesn't work and I can find very little information about it.
Am I missing something really blatant here?
The framework used for logging is logback. When you type : Logger.debug, the isDebugEnabled is already implicitly checked.
For the syntax of logging, use the Scala string interpolation.
Logger.debug(s"Modified: Id = '$real_session_id', Modified = $modified.toString")
Why not just use the standard String interpolation capabilities of the language/stdlib? http://docs.scala-lang.org/overviews/core/string-interpolation.html
I apologise if I've missed something crucial about your question.
As to avoiding the if (Logger.isDebugEnabled) check, if the logging framework is not providing some sort of lazy evaluation scheme for arguments passed into it, I would just first consider defining my own wrappers:
object MyLazyLogger {
def debug(msg: => Any) =
if (Logger.isDebugEnabled) Logger.debug(msg)
}
Also, I don't think the way in which you interpolate stuff into the string has anything to do with not evaluating the arguments to debug() if logging is disabled—if debug() declares that it eager-evaluates any arguments passed into it, there's no way that I can see you can change to lazy evaluation at the call site by just using a "special form" of string interpolation. (I'd be happy if anyone proved me wrong here and taught me something new :))
Disclosure: I'm not familiar with Play (yet), so I'm just taking a shot at a general approach here.
I have been looking everywhere for an answer to this question - perhaps I'm looking in the wrong places. Also, I'm brand new to Objective C although I have around 10 years of experience as a developer.
for this code:
[receiver makeGroup:group, memberOne, memberTwo, memberThree];
what would the method definition look like?
- (void)makeGroup:(Group *)g, (NSString *)memberOne, ...?
Thanks for any help you can provide. I know this is probably very simple...
Thanks,
R
It looks like you have a method that can take a variable number of arguments. If that's the case, the definition would look something like:
- (void)makeGroup:(Group *)g, ...;
Check out NSString's stringWithFormat: or NSArray's arrayWithObjects: methods for examples.
Edit: Upon further documentation reading, it seems that you are looking at the exact example that's in the Objective-C 2.0 documentation. The declaration you're looking for is right at the bottom of page 36.
You can receive an infinte number of arguments with an ellipsis (...). Check this for further details!
It would make more sense to have the members as a separate array argument, like -(void)makeGroup:(Group *)g members:(NSArray *)members. If you must do varargs (which is a pain), it should be written like -(void)makeGroup:(Group *)g members:(NSString *)firstMember, ....
Since I this is trying to figure out how an example method from the documentation would be declared, it would be like this:
- (void)makeGroup:(id)group, ...
Then you would start up the varags machinery with the group argument and use it to find the other arguments.
either you're looking for MrHen's answer if you're seeking to do your own class method or if you want to do them separately you write the following into your header file:
-(void)makeGroup:(Group *)g;
-(NSString *)memberOne;
EDIT: I answered the wrong question. Ignore this.
The correct way to do this is:
-(void)makeGroup:(Group *)g memberOne:(NSString *)memberOne memberTwo:(NSString *)memberTwo memberThree:(NSString *)memberThree {
...
}
The call will look like this:
[receiver makeGroup:group memberOne:memberOne memberTwo:memberTwo memberThree:memberThree];