我想知道是否有可能在finally块中获取函数的返回值.
我有一些像这样的代码.
try
{
return 1;
}
finally
{
//Get the value 1
}
Run Code Online (Sandbox Code Playgroud)
我知道可以添加一个可以保存返回值的变量.但我想知道是否有可能以任何方式获得价值.
谢谢
目前有一个关于哪个代码更易读的局部争论
我们有一个程序员来自ac背景,当程序员编码时看起来像
string foo = "bar";
if (foo[foo.Length - 1] == 'r')
{
}
Run Code Online (Sandbox Code Playgroud)
我们有另一个程序员不喜欢这种方法而宁愿使用
if (foo.EndsWith("r"))
{
}
Run Code Online (Sandbox Code Playgroud)
这种类型的操作哪种方式更好?
写这篇文章的最初目标是尽可能减少占地面积.我可以充满信心地说,这个目标已经实现.不幸的是,这让我的执行速度相当慢.为了产生低于200万的所有质数,在3Ghz英特尔芯片上需要大约8秒.
反正有没有改善这段代码的执行时间,而对内存占用空间的贡献最小?或者,从功能的角度来看,我是否会以错误的方式解决这个问题?
码
/// 6.5s for max = 2,000,000
let generatePrimeNumbers max =
let rec generate number numberSequence =
if number * number > max then numberSequence else
let filteredNumbers = numberSequence |> Seq.filter (fun v -> v = number || v % number <> 0L)
let newNumberSequence = seq { for i in filteredNumbers -> i }
let newNumber = newNumberSequence |> Seq.find (fun x -> x > number)
generate newNumber newNumberSequence
generate 2L (seq { for i in …Run Code Online (Sandbox Code Playgroud) 我向同事程序员展示了这个结构,他们认为它应该是一个可变类.他们觉得没有空引用和根据需要改变对象的能力是不方便的.我真的想知道是否有任何其他原因使这个变成一个可变类.
[Serializable]
public struct PhoneNumber : IEquatable<PhoneNumber>
{
private const int AreaCodeShift = 54;
private const int CentralOfficeCodeShift = 44;
private const int SubscriberNumberShift = 30;
private const int CentralOfficeCodeMask = 0x000003FF;
private const int SubscriberNumberMask = 0x00003FFF;
private const int ExtensionMask = 0x3FFFFFFF;
private readonly ulong value;
public int AreaCode
{
get { return UnmaskAreaCode(value); }
}
public int CentralOfficeCode
{
get { return UnmaskCentralOfficeCode(value); }
}
public int SubscriberNumber
{
get { return UnmaskSubscriberNumber(value); }
}
public int …Run Code Online (Sandbox Code Playgroud) 这段代码是否足够复杂,值得更高级别的抽象?
public static JsonStructure Parse(string jsonText)
{
var result = default(JsonStructure);
var structureStack = new Stack<JsonStructure>();
var keyStack = new Stack<string>();
var current = default(JsonStructure);
var currentState = ParserState.Begin;
var key = default(string);
var value = default(object);
foreach (var token in Lexer.Tokenize(jsonText))
{
switch (currentState)
{
case ParserState.Begin:
switch (token.Type)
{
case TokenType.BeginObject:
currentState = ParserState.Name;
current = result = new JsonObject();
break;
case TokenType.BeginArray:
currentState = ParserState.Value;
current = result = new JsonArray();
break;
default:
throw new JsonException(token, currentState); …Run Code Online (Sandbox Code Playgroud) 这是我的代码:
<script type="text/javascript">
var Note=function(){}
Note.prototype = {
get id()
{
if (!("_id" in this))
this._id = 0;
return this._id;
},
set id(x)
{
this._id = x;
}
}
var a=new Note()
alert(a.id)
</script>
Run Code Online (Sandbox Code Playgroud)
这种风格就像是python,
这是我第一次看到这段代码,
你能不能给我更多关于javascript中'get'和'set'的例子.
谢谢
事件处理程序和事件侦听器之间有什么区别?
直到最近,我认为它们是同一个东西的不同名称:在事件发生时调用的函数.但是我最近阅读了一些内容,它将事件处理程序称为事件监听器绑定的DOM元素,这是有意义的.
我正在努力理解以下两组代码的区别.原始代码来自着名的Ninja教程,我为自己简化了一些.
问题:我想我理解CodeA的工作原理.Ninja.prototype.swung = false正在分配一个新属性function Ninja(),并因此而ninjiaA.swung评估为false.但是,在CodeB中,当我们在开头声明function Ninja()with this.swung = true时,后面的赋值Ninja.prototype.swung = false不起作用,并且ninjaA.swung仍然被评估为true.我无法理解为什么后来的这个任务在CodeB中不起作用.有人可以赐教我这个吗?
CODEa所:
function Ninja(){}
Ninja.prototype.swung = false;
var ninjaA = new Ninja();
ninjaA.swung; //evaluates to false
Run Code Online (Sandbox Code Playgroud)
CodeB:
function Ninja(){
this.swung = true;
}
Ninja.prototype.swung = false; //I'm expecting this changes swung to false,
//but it doesn't.
var ninjaA = new Ninja();
ninjaA.swung; //evaluates to true
Run Code Online (Sandbox Code Playgroud)
非常感谢提前.
我希望有人可以帮助我,数组值在帖子中是空的.
$(function start() {
c_all = new Array('#div { font-color:#ff0000; border:1px solid #00ff00; }', '#div_2 { font-color:#ff0000; }', '.line2 { font-color:#00ffff; }');
css(c_all);
});
function css(x) {
values = new Array();
for (i = 0; i < x.length; i++) {
c0_selector = '' + x[i].match(/^.*{/) + '';
c0_selector = c0_selector.replace(/\s*/g, '');
c0_selector = c0_selector.replace(/{/, '');
x[i] = x[i].replace(/^.*{/, '');
x[i] = x[i].replace(/}/, '');
c0_arr = x[i].split(';');
values['' + c0_selector + ''] = new Array();
$('#log').append(''+c0_selector+'<br />');
for (i2 = 0; …Run Code Online (Sandbox Code Playgroud) 给定LL(1)语法什么是以功能纯粹的方式生成不可变具体语法树的适当数据结构或算法?请随意用您喜欢的任何语言编写示例代码.
我的想法
symbol : either a token or a node
result : success or failure
token : a lexical token from source text
value -> string : the value of the token
type -> integer : the named type code of the token
next -> token : reads the next token and keeps position of the previous token
back -> token : moves back to the previous position and re-reads the token
node : a node in the … language-agnostic parsing functional-programming state-machine data-structures
c# ×4
javascript ×4
.net ×2
parsing ×2
class ×1
events ×1
f# ×1
immutability ×1
jquery ×1
json ×1
mutable ×1
optimization ×1
properties ×1
prototype ×1
refactoring ×1
struct ×1