我最近读到git/github支持已被添加到Monticello.
我还看到一些在github上发布的Smalltalk项目.喜欢:
https://github.com/timfel/ratpack
我的问题:
在Smalltalk中有方法ifNotNilDo:
它使用如下:
database getUser ifNotNilDo: [:user | Mail sendTo: user ]
Run Code Online (Sandbox Code Playgroud)
在不nil执行块的对象上执行,将对象本身作为参数传递.类中的实现UndefinedObject(Smalltalk相当于Ruby的NilClass)实际上什么都不做.这样,如果让用户得到一个nil对象,就什么都不会发生.
我不知道Ruby有类似的东西,所以我推出了自己的解决方案.它是这样的:
class Object
def not_nil
yield(self)
end
end
class NilClass
def not_nil
# do nothing
end
end
Run Code Online (Sandbox Code Playgroud)
它可以像这样使用:
users = {:peter => "peter@peter.com", :roger => "roger@roger.com" }
users[:roger].not_nil {|u| Mail.send(u) }
Run Code Online (Sandbox Code Playgroud)
这使我们无法访问哈希两次
Mail.send(users[:roger]) if users[:roger]
Run Code Online (Sandbox Code Playgroud)
...或使用临时变量:
u = users[:roger]
Mail.send(u) if u
Run Code Online (Sandbox Code Playgroud)
人们开始建议基于哈希操作的解决方案,并且还要两次访问哈希.我的问题不是直接与哈希相关.
想象一下,第一个操作不是哈希访问,也很昂贵.喜欢:
RemoteUserRepo.find_user(:roger).not_nil {|u| Mail.send(u) }
Run Code Online (Sandbox Code Playgroud)
(结束更新)
我的问题是:
在Smalltalk中,如果您没有明确地返回任何内容,则传递的消息将评估为接收者(或消息上下文中的"self").
例如,给定此方法:
MyClass >> myMethod
Transcript show: 'hello'; cr.
Run Code Online (Sandbox Code Playgroud)
评估(doint"print-it")这个:
| myInstance |
myInstance := MyClass new.
myInstance myMethod.
Run Code Online (Sandbox Code Playgroud)
如果对最后一次调用执行<print-it>,则结果将是实例本身.
是否有函数/ Scala方法重复调用函数,直到成功,同时对失败的尝试作出反应?
让我举一个例子来说明.假设我想从标准输入中读取一个整数,如果用户实际上没有输入整数,则重试.
鉴于此功能:
def read_int(): Either[String, Int] = {
val str = scala.io.StdIn.readLine()
try {
Right(str.trim().toInt)
} catch {
case _: java.lang.NumberFormatException => Left(str)
}
}
Run Code Online (Sandbox Code Playgroud)
而这个匿名函数:
val ask_for_int = () => {
println("Please enter an Int:")
read_int()
}
val handle_not_int = (s: String) => {
println("That was not an Int! You typed: " + s)
}
Run Code Online (Sandbox Code Playgroud)
我会像这样使用它们:
val num = retry_until_right(ask_for_int)(handle_not_int)
println(s"Thanks! You entered: $num")
Run Code Online (Sandbox Code Playgroud)
我的问题是:
retry_until_rightScala中已存在类似的东西吗?谢谢!
*)除了snake_case.我真的很喜欢
捕获每个Smalltalk新手的东西是add:不会返回"自我"但是添加的对象.
例如,使用此代码:
myCollection := OrderedCollection new
add: 'Peter';
add: 'John';
add: 'Paul'.
Run Code Online (Sandbox Code Playgroud)
myCollection将包含字符串"保罗",而不是集合本身.
这是因为add:返回正在添加的对象,整个级联表达式的计算结果是最后发送的消息.
相反,它应该写yourself在最后:
myCollection := OrderedCollection new
add: 'Peter';
add: 'John';
add: 'Paul';
yourself.
Run Code Online (Sandbox Code Playgroud)
add:这种行为有什么好处?是否有预先存在/ Scala-idiomatic /更好的方法来实现这一目标?
def sum(x: Int, y: Int) = x + y
var x = 10
x = applyOrBypass(target=x, optValueToApply=Some(22), sum)
x = applyOrBypass(target=x, optValueToApply=None, sum)
println(x) // will be 32
Run Code Online (Sandbox Code Playgroud)
我的applyOrBypass可以像这样定义:
def applyOrBypass[A, B](target: A, optValueToApply: Option[B], func: (A, B) => A) = {
optValueToApply map { valueToApply =>
func(target, valueToApply)
} getOrElse {
target
}
}
Run Code Online (Sandbox Code Playgroud)
基本上我想要应用操作,具体取决于是否定义了某些选项值.如果不是,我应该得到预先存在的价值.理想情况下,我想链接这些操作,而不必使用var.
我的直觉告诉我,折叠或减少会涉及到,但我不确定它是如何工作的.或者也许有另一种方法与monadic-fors ...
任何建议/提示赞赏!
monads coding-style scala optional-parameters named-parameters
我试图加载一个显然是为早期版本的Pharo编写的软件包(Ratpack,从http://ss3.gemstone.com/ss/RatPack.html进入Pharo 1.4).
在那里,我得到了关于environmentAt:put:不被支持的弃用警告Project.
根据文档,要使用的方法是使用ProcessSpecificVariable.
我的问题是:
谢谢!