optional

Return Optional value with ?: operator

最后都变了- 提交于 2019-12-04 17:07:36
问题 I often need to use optional type for functions: std::optional<int32_t> get(const std::string& field) { auto it = map.find(field); if (it != map.end()) return it->second; return {}; } Is there a way to return optional value in one line? e.g. this: std::optional<int32_t> get(const std::string& field) { auto it = map.find(field); return it != map.end() ? it->second : {}; } results in the error error: expected primary-expression before '{' token return it != map.end() ? it->second : {}; ^ 回答1:

Java8 lambda for checking two conditions

 ̄綄美尐妖づ 提交于 2019-12-04 16:58:22
I have the following code snippet which I would try to change to the lambda function. if(catList != null && catList.size() > 0) { animalType = AnimalType.CAT; HttpEntity<List<?>> request = new HttpEntity<>(catList, headers); response = restTemplate.postForObject(apiUrl, request, String.class); } else if(dogList != null && dogList.size() > 0) { animalType = AnimalType.DOG; } else { return; } Somehow I have written like as shown below, but don't know to incorporate the dogList checking the condition Optional.of(catList) .map(catList -> { .... }) .orElse(return); //<------ THIS IS NOT POSSIBLE

Optional vs throwing an exception

那年仲夏 提交于 2019-12-04 16:51:12
问题 Is it true that since Java 1.8 returning Optional object is more preferable than throwing an exception? Increasingly i see the code like this: public Optional<?> get(int i) { // do somtething Object result = ... Optional.ofNullable(result); } Instead of this: public Object get(int i) { if(i<0 || i>=size) { throw new IndexOutOfBoundsException("Index: " + i + ". Size: " + size); } // do somtething Object result = ... return result; } Is it means that we need to forget old approach and use new?

Optional Binding on Implicitly Unwrapped Optional

时光毁灭记忆、已成空白 提交于 2019-12-04 15:13:11
Swift Programming Guide says "You can also use an implicitly unwrapped optional with optional binding, to check and unwrap its value in a single statement". Why do you need to use optional binding when the value is already unwrapped? Does option binding unwrap it again? Calling an implicitly unwrapped is the same as calling a regular optional with ! after it. It can still hold a nil value and calling it when it's nil will result in a runtime error, so you use the if let optional binding if you aren't sure if it is nil or not. var myOptional: Int! = nil 10 + myOptional //runtime error if let

How to iterate nested for loops referring to parent elements using Java 8 streams?

别说谁变了你拦得住时间么 提交于 2019-12-04 11:19:28
问题 I want to iterate nested lists using java8 streams , and extract some results of the lists on first match. Unfortunately I have to also get a values from the parent content if a child element matches the filter. How could I do this? java7 Result result = new Result(); //find first match and pupulate the result object. for (FirstNode first : response.getFirstNodes()) { for (SndNode snd : first.getSndNodes()) { if (snd.isValid()) { result.setKey(first.getKey()); result.setContent(snd.getContent

全面解析GMAT词汇中的熟词僻意情况

▼魔方 西西 提交于 2019-12-04 11:09:41
熟词僻意是 GMAT考试 中常会考到的要点之一。许多考生在背GMAT词汇的过程中,对于陌生的单词往往会投入更多精力去背得滚瓜烂熟,而一些比较脸熟的词汇,就不会花太多心思去仔细研究,这种因为熟词而放松了警惕的做法,就会导致考生因为熟词僻意的出题思路而丢失分数。下面小编就为大家讲解GMAT考试中熟词僻意的出题思路,并对一些常见的僻意熟词做汇总整理。 什么是熟词僻意? 所谓熟词僻意,是指一些大家经常使用的词汇,除了具备常用的含义以外,还有一些少有人用的冷僻意思。这些意思往往不为人所知,却被GMAT出题者利用来作为出题的陷阱,引诱考生在解答时做出错误的选择。对于背单词过程中没有特别留意常见词汇冷僻含义的考生来说,是十分危险的一种出题思路和陷阱。 GMAT考试中熟词僻意常用词整理 perspective 透视画法;观点,方法;前景,远景 prospect 前景,景色;前途;勘探,寻找 elaborate v.&adj. 精心制作,详细描述;精心制作的 address v.从事,忙于;n. 演讲 appreciate 理解,认识,意识到;欣赏;感激 appropriate v. 拨给(资金),盗用/ adj.合适的 strain n. 血统,品系,菌株;紧张,张力;v.扭伤,拉紧 article n. 物品,商品 intriguing adj. 激发兴趣的 intrigue v.&n 激发兴趣

Swift Lazy and Optional properties

℡╲_俬逩灬. 提交于 2019-12-04 10:55:31
What is the difference between a Lazy or Optional property in Swift? For example, if someone is building a navigation bar that comes in from the side, I think that should all be within one UIViewController . The user might never open the menu but sometimes they will. var menu: NavigationBar? lazy var menu: NavigationBar = NavigationBar.initialize() Both of the optional I think are good code, because they don't create the view unless its needed. I understand Optional means there might be a value it might be nil . I also understand Lazy means don't worry about it until I need it. Specific

JSON: Serialize Guava Optional

可紊 提交于 2019-12-04 10:42:17
Is there a Json Serializer/Deserializer for com.google.common.base.Optional? Out of the box this doesn't seem to work with Jackson, see below: package com.example; import java.io.IOException; import org.codehaus.jackson.JsonGenerationException; import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper; import com.google.common.base.Optional; public class TestClass { public Optional<String> myString; public TestClass() { myString = Optional.of("testString"); } public static void main(String[] args) throws JsonGenerationException, JsonMappingException,

Swift pattern matching with enum and Optional tuple associated values

萝らか妹 提交于 2019-12-04 10:05:46
I'm currently using Alamofire and I use an enum to describe the API I used as advised in the readme. The endpoints are represented as follows: public enum API { case GetStops(stopCode:String?) case GetPhysicalStops case GetLinesColors case GetNextDepartures(stopCode:String, departureCode:String?, linesCode:String?, destinationsCode:String?) } The optional parameters are mutually exclusive: public var URLRequest: NSMutableURLRequest { let result:(path:String, parameters:[String:AnyObject]?) = { switch self { case .GetStops(let stopCode) where stopCode != nil : return ("GetStops.json", [

Facebook graph object/entity parsing SDK 4 in Swift

邮差的信 提交于 2019-12-04 06:34:09
问题 In Swift how do you parse the result of a fbk graph request? I have a nested dictionary and casting to [String: String] does not work. I have casted to NSDictionary which works for level 1 but anything nested started complaining about optionals and casting. I see from the changeling that FBGraphObject has been deprecated, so what is the correct approach now in SDK 4? My data looks like { data = { "is_silhouette" = 0; url = "..."; }; } I can do var data = photoRes["data"] as? NSDictionary to