问题很简单:我必须用"xyz"替换所有出现的"fooo"及其所有子串.例如,在Java中,我将这样做:
someString.replaceAll( "fooo|foo|fo", "xyz" )
Run Code Online (Sandbox Code Playgroud)
它会做的伎俩.但在Haskell中,我发现没有有效的方法来使用正则表达式.首先,我读过这个:http://www.haskell.org/haskellwiki/Regular_expressions
实际上具有replace函数的唯一库是regex-posix,但它在性能上被认为"非常慢".这个事实是不可接受的.另外我发现这个replace函数由于任何原因不符合给定模式的顺序,所以我得到这样的输出:
>replace "boo fooo boo" "xyz"
"boo xyzoo boo"
Run Code Online (Sandbox Code Playgroud)
其他后端并不意味着这样的功能.
所以我决定写简单的解决方法:
replaceFoo input =
helper input []
where
helper ('f':'o':'o':'o':xs) ys = helper xs ("zyx" ++ ys)
helper ('f':'o':'o':xs) ys = helper xs ("zyx" ++ ys)
helper ('f':'o':xs) ys = helper xs ("zyx" ++ ys)
helper (x:xs) ys = helper xs (x:ys)
helper [] ys = reverse ys
Run Code Online (Sandbox Code Playgroud)
虽然我发现这个功能不太好,但效果很好而且速度很快.但是现在我遇到了在这个替换中添加更多单词的必要性,我不喜欢扩展helper模式的想法(我需要说实际应用程序中我实际上有4个单词,这很奇怪).
如果有人帮助我快速解决方案,我会很高兴.
cebewee,感谢Data.String.Utils.但我担心如果要替换很多单词("fooo"到"xyz","foo"到"xyz","fo"到"xyz","bar"到"quux"等等,这种方法很慢),因为为了foldr …
我尝试了很长时间安装testng插件,但是eclipse显示如下错误:
"No repository found as the location".
Run Code Online (Sandbox Code Playgroud)
我也尝试下载并安装插件,但弹出相同的错误.
出于测试目的,我经常开始在现有项目中键入一些代码.所以,我想要测试的代码出现在所有其他代码之前,如下所示:
public static void main(String[] args)
{
char a = '%';
System.out.println((int)a);
// To know where '%' is located in the ASCII table.
// But, of course, I don't want to start the whole project, so:
return;
// The real project starts here...
}
Run Code Online (Sandbox Code Playgroud)
但编译器抱怨return-statement,因为下面的"死代码".(在C++中,编译器服从程序员并简单地编译return语句)
为了防止编译器抱怨,我写了一个愚蠢的if陈述:
if (0 != 1) return;
Run Code Online (Sandbox Code Playgroud)
我讨厌它.为什么编译器不能按我要求做?是否有一些编译标志或注释或其他什么来解决我的问题?
谢谢
我写了以下代码:
int i = 0;
switch(i++)
{
case 0:
cout << 0;
case 1:
cout << 1;
}
cout << "\n" << i;
Run Code Online (Sandbox Code Playgroud)
代码的输出是这样的:
01
1
Run Code Online (Sandbox Code Playgroud)
任何人都可以解释第一行输出?为什么0和1都被打印?
我正在使用Ruby on Rails 3,我想知道这些查询语句的性能差异:
# Case 1
accounts = ids.map { |id| Account.find_by_id(id) }
# Case 2
accounts = ids.map { |id| Account.where(:id => id).first }
Run Code Online (Sandbox Code Playgroud)
还有另一种方法可以做得更好吗? 如果ids是100,如何在帐户为5之前限制搜索?
有可能,如果是这样,如何让Visual Studio在代码中突出显示动态表达式?
当我只是在一些代码上面徘徊时,visual studio告诉我这是一个动态表达.这让我意识到我在我的代码中犯了一个错误并使用了一个动态太多.但我也意识到,如果我没有徘徊,那么应该是静态的"大片"动态代码就会逃脱.
所以我想知道是否有可能使Visual Studio改变动态表达式的背景颜色,因此这些部分将清晰可辨.
[edit]
动态表达式是指在常规代码中使用dynamic关键字.
dynamic-language-runtime syntax-highlighting visual-studio-2010 visual-studio dynamic-code
我正在使用WPF创建一个应用程序.我想计算画布可见区域的坐标.
当我调整窗口大小时将调用哪个方法,以便我可以在调整窗口大小时计算坐标?
尝试执行shell脚本时出现以下错误,
$'\r': command not found: line 2:
Run Code Online (Sandbox Code Playgroud)
请为此建议一个解决方案.
以下是脚本中使用的初始行,
#!/bin/sh
if [[ $# -lt 1 ]]; then
echo "ERROR Environment argument missing <dev,test,qa,prod>"
export RC=50
exit $RC
fi
Run Code Online (Sandbox Code Playgroud) 我试图从Oracle中提取有关外键的一些元数据.我使用"all_constraints"查找所有引用约束信息,使用"all_cons_columns"查找实际列.但是,我无法在外键中获取列的顺序.
下面是4个包含3列的示例表.其中3个表引用表"tab_d",但外键中列的顺序不同.此订单未反映在"all_cons_columns"视图中,那么是否有其他可用信息的位置?
create table tab_d (
col_a int,
col_b int,
col_c int,
constraint tab_d_pk primary key (col_a, col_b, col_c)
);
create table tab_e (
ref_col_a int,
ref_col_b int,
ref_col_c int,
constraint tab_e_fk foreign key (ref_col_b, ref_col_c, ref_col_a)
references tab_d(col_b, col_c, col_a)
);
create table tab_f (
ref_col_a int,
ref_col_b int,
ref_col_c int,
constraint tab_f_fk foreign key (ref_col_b, ref_col_c, ref_col_a)
references tab_d(col_c, col_a, col_b)
);
create table tab_g (
ref_col_a int,
ref_col_b int,
ref_col_c int,
constraint tab_g_fk foreign key (ref_col_c, ref_col_b, …Run Code Online (Sandbox Code Playgroud) 我对LINQ和Entity框架以及c#中的var关键字都很陌生,所以如果这听起来像是一个"新手"问题,请原谅我.
在执行以下操作后,我在检查空值时遇到问题:
var entry = myDB.Entries.Where(e => e.Email == entry.Email);
Run Code Online (Sandbox Code Playgroud)
即使数据库中不存在电子邮件,条目也不等于null.
因此,在执行下一批语句之前,if (entry == null)我不得不if (entry.Count() < 1)检查现有的条目.是否有任何理由不将变量视为null?