option

Convert Option to Either in Scala

不想你离开。 提交于 2019-12-02 20:26:59
Suppose I need to convert Option[Int] to Either[String, Int] in Scala. I'd like to do it like this: def foo(ox: Option[Int]): Either[String, Int] = ox.fold(Left("No number")) {x => Right(x)} Unfortunately the code above doesn't compile and I need to add type Either[String, Int] explicitly: ox.fold(Left("No number"): Either[String, Int]) { x => Right(x) } Is it possible to convert Option to Either this way without adding the type ? How would you suggest convert Option to Either ? No, if you do it this way, you can't leave out the type. The type of Left("No number") is inferred to be Either

Check if a string is blank or doesn't exist in Scala

走远了吗. 提交于 2019-12-02 20:18:18
I have an Option[String] . I want to check if there is a string exists and if it's exists its not blank. def isBlank( input : Option[String]) : Boolean = { input.isEmpty || input.filter(_.trim.length > 0).isEmpty } Is there is a better way of doing this in Scala ? What you should do is check using exists . Like so: myOption.exists(_.trim.nonEmpty) which will return True if and only if the Option[String] is not None and not empty. An approach based in pattern matching, def isBlank( input : Option[String]) : Boolean = input match { case None => true case Some(s) => s.trim.isEmpty } This should

Un-optioning an optioned Option

99封情书 提交于 2019-12-02 20:04:01
Say I have a val s: Option[Option[String]] . It can thus have the following values: Some(Some("foo")) Some(None) None I want to reduce it so that the first becomes Some("foo") while the two others become None . Obviously there are many ways to accomplish this, but I'm looking for a simple, perhaps built-in, less-than-one-liner. It's a shame that flatten doesn't exist. It should. Flatten does exist now. As before, s getOrElse None (in addition to the other answers) will also do the same thing. You could use scalaz join to do this, as this is one of the monadic operations: doubleOpt.join Here it

proper way to hide/show columns on DBGrid

别等时光非礼了梦想. 提交于 2019-12-02 19:17:11
问题 I am starting new database project, where I have some tables and maybe 20 columns to show in DBGrid at all, but I know it is not possible to show all 20 columns in Grid as it will need too much space. I need such option that user can adjust which columns to show or hide. what would be a easy and proper solution for it? other solutions will also be accepted. thanks everyone for any help. 回答1: there are 2 ways. 1st. You are using automatically created dbgrid columns. this mean, that you do not

IE浏览器不支持display:none导致option不能隐藏的问题

天大地大妈咪最大 提交于 2019-12-02 16:18:44
项目场景:级联的select元素,根据一级select的选择,二级select中的option选项需要对应的隐藏或显示。 原来的做法是display:none和display:block来隐藏和显示option,option中通过自定义属性data-category过滤,控制option的隐藏或显示,结果在IE浏览器中并不能达到想要的效果,通过网络查找,是因为IE浏览器不支持display:none属性。 解决方案: 1、给option外加一个父级标签,并设置父级标间隐藏[此处用的是span标签],如果要显示的话则去除父级标签即可 (网络上查找的方法) 2、在一级select的onchange事件触发的方法中按选择的option的值判断,通过$(selector).html("option的html代码")方法直接覆盖二级select的option内容 具体参考如下: //html代码 运动 阅读 音乐 篮球 足球 排球 诗词 小说 古典 流行 //方案一 <script type="text/javascript> //隐藏全部选项 function hideAllOption(selId){ $("#"+selId).find("option").each(function(index,element){ var elementJQ = $(element);/

How to filter None's out of List[Option]?

☆樱花仙子☆ 提交于 2019-12-02 16:02:27
If I have a List[Option[A]] in Scala, what is the idiomatic way to filter out the None values? One way is to use the following: val someList: List[Option[String]] = List(Some("Hello"), None, Some("Goodbye")) someList.filter(_ != None) Is there a more "idiomatic" way? This does seem pretty simple. If you want to get rid of the options at the same time, you can use flatten : scala> someList.flatten res0: List[String] = List(Hello, Goodbye) Kevin O'Riordan someList.filter(_.isDefined) if you want to keep the result type as List[Option[A]] The cats library also has flattenOption , which turns any

python cfg文件操作

佐手、 提交于 2019-12-02 15:44:34
格式: [section] option = value import ConfigParser config=ConfigParser.ConfigParser() #读 config.read('test.cfg') #with open('test.cfg','r') as f: #  config.readfp(f,'test.cfg') print config.sections() #获取所有的section print config.has_section(section) #判断是否有section print config.has_option(section,option) #判断是否有option print config.options(section) #获取section里面的所有的option print config.items(section) #获取section里面的所有的(option,value)键值对 print config.get(section,option) #获取value #写 config.add_section('section') #添加一个section config.set('section1','name','aaa') #添加section的option,value with open('test.cfg','a

Same option menu in all Activities in Android

♀尐吖头ヾ 提交于 2019-12-02 15:33:29
I have 10-15 activities in my project. I want to have the option menu mostly in all Activities. Then is their any way we can do it at one place and it appears in all activities. Also, I will like to hide the option menu in some. So, is it possible or I have to write option menu code in all activities. Regards Sunil st0le Create a Class (say BaseActivity ) that extends Activity, and override onCreateOptionsMenu and onOptionsItemSelected functions. public class BaseActivity extends Activity { // Activity code here @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater =

Python命令行神器Click

风格不统一 提交于 2019-12-02 15:06:16
Python命令行神器Click 官网: Click 是用Python写的一个第三方模块,用于快速创建命令行。我们知道,Python内置了一个 Argparse 的标准库用于创建命令行,但使用起来有些繁琐,Click相比于Argparse,就好比requests 相比于urllib。 快速使用 Click 的使用大致有两个步骤: 使用 @click.command() 装饰一个函数,使之成为命令行接口; 使用 @click.option() 等装饰函数,为其添加命令行选项等。 它的一种典型使用形式如下: import click @click.command() @click.option('--param', default=default_value, help='description') def func(param): pass 下面,让我们看一下 官方文档 的入门例子: #!/usr/bin/env python # -*- coding: utf-8 -*- import click @click.command() @click.option('--count', default=1, help='Number of greetings.') @click.option('--name', prompt='Your name', help='The person