syntax

Is there an “opposite” to the null coalescing operator? (…in any language?)

為{幸葍}努か 提交于 2020-01-19 04:43:05
问题 null coalescing translates roughly to return x, unless it is null, in which case return y I often need return null if x is null, otherwise return x.y I can use return x == null ? null : x.y; Not bad, but that null in the middle always bothers me -- it seems superfluous. I'd prefer something like return x :: x.y; , where what follows the :: is evaluated only if what precedes it is not null . I see this as almost an opposite to null coalescence, kind of mixed in with a terse, inline null-check,

How to exit a loop in Python? [closed]

情到浓时终转凉″ 提交于 2020-01-17 06:34:06
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . I want to make the loop stops when x + y =z , on the else. Code: # -*- coding: utf-8 -*- """ Created on Mon Nov 16 18:39:40 2015 @author: gabri """ from random import randint import os x = randint(1,11) y = randint(1,11) print ("", x,"+", y,"=\n") z = int(input("Resposta=")) if z == x + y: input ("\n\nCorreto

Referencing entries of matrix in R within for loop

你说的曾经没有我的故事 提交于 2020-01-17 03:03:31
问题 Is there a way to reference a part of a matrix within a for loop? for (j in 1:x1) for (k in 1:x2) { matrix[j,8k-6:8k+1] <- AlleleFreq.t1[k,1:8] } } I get an error message saying "unexpected symbol in "alldata.t1[j,8k". What is the correct syntax for preforming this sort of operation? Thank you. 回答1: use parens & * to multiply: 8k-6:8k+1 ~~~> (8*k-6):(8*k+1) the seq operator : takes precedence over arithmetic operators such as - Thus, without parens, you have (8*k) - c(6, 7, 8) + ((8*k) + 1)

How to encode a constraint on the format of String values

笑着哭i 提交于 2020-01-17 02:53:16
问题 As I frequently observe and how I often implement a name attribute, is to simply model it as String . What now, if the name has to follow a certain syntax, i.e. format? In Java I probably would define a constructor with a check on its arguments, something like: public Name(str: String) { if (str == null) throw new IllegalArgumentException("Str must not be null."); if (!str.matches("name format expressed as regex")) throw new IllegalArgumentException("Str must match 'regex' but was " + str);

Different answers if I use Vector instead of Array while counting Inversions, what could be the cause?

非 Y 不嫁゛ 提交于 2020-01-16 10:08:27
问题 I've been trying to do the count inversions question using mergesort for the past 2-3 days and after much trying, I just picked up the answer from Hackerrank's editorial, now their code is using an Array , and if I use a Vector instead of an Array , the answer is Actual answer + 1 (or different to say the least haven't tried it on many cases). I was wondering what might be the reason for it. I also have another question on explanation of this code, in particular the variable declarations and

One of the omegaSem function arguments is an object not found

十年热恋 提交于 2020-01-16 09:09:34
问题 I am reformulating this question because it seems that the information that I provided before is not that clear. I'm new using R so I'm not able yet to identify the most common and simple errors. So I'm following a tutorial to perform a McDonald Omega analysis to estimate reliability on a psychometric test, here is the link just to make you sure about source of the information that I'm using: http://personality-project.org/r/psych/HowTo/omega.pdf To run that analyisis, we work with the "psych

Syntax confusion (do block)

删除回忆录丶 提交于 2020-01-16 04:37:19
问题 Sorry for a poor title, feel free to edit. I can't understand what the problem is, so it might be altogether wrong. Below is the code (this is after I've done like a hundred of permutations and different sequences of let-do-if and tabulation, and I'm exhausted): -- The last statement in a 'do' construct must be an expression numberOfGoods :: IO String numberOfGoods = do putStrLn "Enter year (2000-2012):\n" let intYear = readYear in if (intYear < 2000 || intYear > 2012) then error "Year must

C++: Syntax for map where Data type is function?

杀马特。学长 韩版系。学妹 提交于 2020-01-16 04:20:27
问题 In C#, what I want would look something like this: IDictionary<string, action()> dict = new Dictionary<string, action()>(); How do I do this in C++? This gives compiler errors: map<string, void()> exercises; 回答1: Use boost::function, a polymorphous wrapper for any object that can be called with your signature (including functions, function objects etc). map<string, function<void()>> ...; Note that the new C++ standard has already included function in <functional> . To explain the backgrounds:

awk embedded script issue (unexpected character '\')

时光怂恿深爱的人放手 提交于 2020-01-15 13:06:22
问题 I use an embedded awk code in a shell script: I have some variable assignments at the BEGIN part of it: \ BEGIN { FS=","; OFS=","; service_not="false"; end_of_line="\n"; is_setup_gps="false"; \ \ a=6378137.0 ; \ b=6356752.3142 ; \ f=(a-b)/a ; \ e=sqrt(f*(2-f)) ; \ } \ \ So I need '\' at the end of each line (to have an entire awk script embedded in .sh). BUT: for the lines: a=...; b=...; f=...; the '\' causing errors...: mawk: 57: unexpected character '\' Why? UPD: Embedding of awk in the

awk embedded script issue (unexpected character '\')

岁酱吖の 提交于 2020-01-15 13:04:01
问题 I use an embedded awk code in a shell script: I have some variable assignments at the BEGIN part of it: \ BEGIN { FS=","; OFS=","; service_not="false"; end_of_line="\n"; is_setup_gps="false"; \ \ a=6378137.0 ; \ b=6356752.3142 ; \ f=(a-b)/a ; \ e=sqrt(f*(2-f)) ; \ } \ \ So I need '\' at the end of each line (to have an entire awk script embedded in .sh). BUT: for the lines: a=...; b=...; f=...; the '\' causing errors...: mawk: 57: unexpected character '\' Why? UPD: Embedding of awk in the