刚刚开始使用clojure.我正在使用leiningen并且无法弄清楚为什么导入<<宏似乎不起作用
project.clj
(defproject myapp "0.1"
:description "Clojure learning sandbox"
:main myapp.core
:dependencies [[org.clojure/clojure "1.4.0"]
[org.clojure/core.incubator "0.1.2" ]])
Run Code Online (Sandbox Code Playgroud)
core.clj
(ns clojure-shuffle
(:require [clojure.core.incubator :refer [<<]]))
(defn -main [& args]
(println (<< "The sum is: ~(reduce + (map read-string args))")))
Run Code Online (Sandbox Code Playgroud)
当我做一个lein run 3 7 2我期待的时候
The sum is: 12
Run Code Online (Sandbox Code Playgroud)
但是我得到了这个(后面是一个大的堆栈跟踪):
Exception in thread "main" java.lang.IllegalAccessError: << does not exist
Run Code Online (Sandbox Code Playgroud) 我有一个user具有一些属性的对象,我可以使用点表示法访问.
例如,user.fullName输出一个String之类的Firstname Lastname.
如何在println使用字符串插值的语句中访问这些属性?
我尝试过以下方法:
println(s"user's full name is $user.fullName")
Run Code Online (Sandbox Code Playgroud)
但是,它似乎不适用于点表示法,只解析整个$user对象,将剩余fullName部分解释为字符串而不是属性.这输出错误:
>>用户的全名是User(...).fullName
以下是我的追求:
>>用户的全名是名字姓氏
帮助赞赏!
在scala中,您可以轻松地在字符串中包含变量的内容,如下所示:
val nm = "Arrr"
println(s"my name is , $nm")
Run Code Online (Sandbox Code Playgroud)
这是否可能在nim,在那种情况下,如何?
考虑一下这个在Visual Studio 2015中编译得很好的简单程序:
public class Program
{
enum Direction
{
Up,
Down,
Left,
Right
}
static void Main(string[] args)
{
// Old style
Console.WriteLine(string.Format("The direction is {0}", Direction.Right));
Console.WriteLine(string.Format("The direction is {0}", (int)Direction.Right));
// New style
Console.WriteLine($"The direction is {Direction.Right}");
Console.WriteLine($"The direction is {(int)Direction.Right}");
}
}
Run Code Online (Sandbox Code Playgroud)
...按预期输出:
The direction is Right
The direction is 3
The direction is Right
The direction is 3
Run Code Online (Sandbox Code Playgroud)
但是,Visual Studio 2015特别建议在此行中建议"快速操作":
// "Cast is redundant" warning
Console.WriteLine($"The direction is {(int)Direction.Right}");
Run Code Online (Sandbox Code Playgroud)
它坚持认为(int) "演员是多余的",并建议作为"删除不必要的演员" …
使用字符串插值使我的字符串格式看起来更加清晰,但是.ToString()如果我的数据是值类型,我必须添加调用。
class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
var person = new Person { Name = "Tom", Age = 10 };
var displayText = $"Name: {person.Name}, Age: {person.Age.ToString()}";
Run Code Online (Sandbox Code Playgroud)
这.ToString()使得格式更长更难看。我试图摆脱它,但它string.Format是一个内置的静态方法,我无法注入它。你对此有什么想法吗?而且既然字符串插值是 的语法糖string.Format,为什么.ToString()在生成语法糖背后的代码时不添加调用呢?我认为这是可行的。
全部,
尝试绑定到模型上的函数时出现错误
我有以下组件、模型和 Html(对于这个例子都是假的)
export class TestModel {
public getSomeValue(): string {
return "Hello World";
}
}
Run Code Online (Sandbox Code Playgroud)
让我们假设 testModels 属性以某种方式获取它的数据。
@Component(...)
public class MyComponent {
public testModels: TestModel[];
ngOnInit() {
this.loadData();
}
public loadData(): void
{
this.http.get("...")
.map(r=>r.json as TestModel[]) // <--- I think now you've asked the question, I see the problem on this line
.subscribe(d=>this.testModels = d);
}
}
Run Code Online (Sandbox Code Playgroud)
现在在我的 html
<div *ngFor="let testModel of testModels">
<span>{{testModel.getSomeValue()}}</span> <!-- This fails -->
</div>
Run Code Online (Sandbox Code Playgroud)
这是完整的错误:
error_handler.js:48 EXCEPTION: Error in ./MyComponent …
我正在尝试做类似的事情
string heading = $"Weight in {imperial?"lbs":"kg"}"
Run Code Online (Sandbox Code Playgroud)
这有可能吗?
几天前,我按照 ReSharper 的建议将所有 string.Format 修改为字符串插值。我接受了这个建议,并在整个解决方案中对其进行了更改。
因为我们在一个团队中工作,所以我讨论了这个变化(之后,当损坏完成时),我们决定回到 string.Format 以确保没有损坏。
现在,有没有办法告诉 ReSharper 将我所有的字符串插值改回 string.Format 的?我希望有。
笔记:
有一种简单的方法来做到这一点会很棒,而不仅仅是撤消我对 TFS 的检查(每个文件的文件,因为我想保留其他更改)。
有没有办法使用变量来决定文字字符串插值中的小数点数?
例如,如果我有类似的东西
f'{some_float:.3f}'
Run Code Online (Sandbox Code Playgroud)
有没有办法3用变量替换?
最终目标是将数据标签添加到条形图:
def autolabel_bar(rects, ax, decimals=3):
"""
Attach a text label above each bar displaying its height
"""
for rect in rects:
height = rect.get_height()
ax.text(rect.get_x() + rect.get_width()/2.,
height + 0.035,
f'{round(height,decimals):.3f}',
ha='center',
va='center')
Run Code Online (Sandbox Code Playgroud)
但是我想不出一种简单的方法来用3变量替换字符串插值中的decimal。
python string-formatting string-interpolation python-3.x f-string
我正在尝试使用特定上下文/范围中的可用变量动态格式化一些字符串。
该字符串将包含诸如 之类的部分{{parameter1}},{{parameter2}}并且这些变量将存在于我将尝试重新格式化字符串的范围内。变量名应该匹配。
我寻找了诸如动态字符串插值方法或如何使用 FormattableStringFactory 之类的东西,但我没有找到真正能满足我需求的东西。
var parameter1 = DateTime.Now.ToString();
var parameter2 = "Hello world!";
var retrievedString = "{{parameter2}} Today we're {{parameter1}}";
var result = MagicMethod(retrievedString, parameter1, parameter2);
// or, var result = MagicMethod(retrievedString, new { parameter1, parameter2 });
Run Code Online (Sandbox Code Playgroud)
是否有现有的解决方案,或者我应该(在MagicMethod)中将这些部分替换retrievedString为作为参数给出的匿名对象的匹配成员(使用反射或类似的东西)?
编辑:
最后,我创建了一个扩展方法来处理这个问题:
internal static string SpecialFormat(this string input, object parameters) {
var type = parameters.GetType();
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex( "\\{(.*?)\\}" );
var sb = new System.Text.StringBuilder();
var pos = 0;
foreach …Run Code Online (Sandbox Code Playgroud)