在Scala中,我可以像这样声明一个对象:
class Thing
object Thingy extends Thing
Run Code Online (Sandbox Code Playgroud)
我如何"Thingy"在Scala中获取(对象的名称)?
我听说Lift(Scala的Web框架)能够做到这一点.
当我尝试使用_而不是使用命名标识符时,为什么会出现错误?
scala> res0
res25: List[Int] = List(1, 2, 3, 4, 5)
scala> res0.map(_=>"item "+_.toString)
<console>:6: error: missing parameter type for expanded function ((x$2) => "item
".$plus(x$2.toString))
res0.map(_=>"item "+_.toString)
^
scala> res0.map(i=>"item "+i.toString)
res29: List[java.lang.String] = List(item 1, item 2, item 3, item 4, item 5)
Run Code Online (Sandbox Code Playgroud) 看看这个REPL会话(为了便于阅读,我已整理了它):
scala> val x = 1 to 10
x: Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
scala> val y = x.toSeq
y: Range = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
scala> x eq y
res14: Boolean = true
scala> util.Random.shuffle(y)
<console>:10: error: Cannot construct a collection of type scala.collection.AbstractSeq[Int] with elements of type Int based on a collection of type scala.collection.AbstractSeq[Int].
util.Random.shuffle(y)
^
scala> util.Random.shuffle(x)
res16: scala.collection.immutable.IndexedSeq[Int] = Vector(8, 3, 4, …Run Code Online (Sandbox Code Playgroud) 给定一个API:
class Bar { ... }
class Foo extends Bar { ... }
Run Code Online (Sandbox Code Playgroud)
在Java的Optional类型中,我们可以说:
Optional<Foo> fooOption = ...
fooOption.orElse(aFoo) // returns something of type Foo
Run Code Online (Sandbox Code Playgroud)
但是,既然Foo是a Bar,我想能够说:
Optional<Foo> fooOption = ...
fooOption.orElse(aBar) // returns something of type Bar
Run Code Online (Sandbox Code Playgroud)
作为练习,我想用另一种类型完成此操作:
public abstract class Option<T> {
// this doesn't compile
public abstract <U super T> U orElse(U other);
}
Run Code Online (Sandbox Code Playgroud)
我如何重写这个以进行编译,还支持在需要时同时扩展类型的能力?
让我们想象一下范围内的以下项目:
object Thing {
var data: Box[String] = Empty
}
def perform[T](setter: Box[T] => Unit) {
// doesn't matter
}
Run Code Online (Sandbox Code Playgroud)
以下无法编译:
perform(Thing.data = _)
Run Code Online (Sandbox Code Playgroud)
错误消息是:
<console>:12: error: missing parameter type for expanded function ((x$1) => Thing.data = x$1)
perform(Thing.data = _)
^
<console>:12: warning: a type was inferred to be `Any`; this may indicate a programming error.
perform(Thing.data = _)
^
Run Code Online (Sandbox Code Playgroud)
以下编译:
perform(Thing.data_=)
Run Code Online (Sandbox Code Playgroud)
我已经通过创造更好的抽象来超越这个问题,但我的好奇心仍然存在.
谁能解释为什么会这样?
functional-programming scala type-inference anonymous-function
我们一直在努力提高我们应用技能来解决问题的能力.软件工程原理极大地帮助我编写更高质量的代码.这包括测试,模块化,在适当的地方使用OO等.
这是我在JS中实现模块化的一个例子.也许这是实现这一目标的一种不好的方式,但它可以作为我的意思的一个例子,并包含一些自己的问题.
framework.js
Framework = {
CurrentState : {
IsConfigurationLoaded : false,
IsCurrentConfigurationValid : false,
Configuration : undefined //undefined .. good? bad? undefined?
},
Event : {
//event lib
},
//you get the idea
}
Run Code Online (Sandbox Code Playgroud)
题:
您在哪些方面应用软件工程原理来提高JS的可读性,可维护性和其他质量属性?
其他相关(更具体)的问题有助于回答:
我曾经写过一个简单的JS单元测试框架,它有简单的断言和一个带有lambda的测试助手方法.您对单元测试javascript有何看法?
定义代码和框架之间的边界有多重要?
JS主要用于浏览器或网站.这会减少/消除某些顾虑吗?
您是否建议使用Classes和OO原则?
使用undefined和/或null?它应该被禁止吗?
使用try/catch?建议?
你什么时候从JSON去上课?您是否使用对数据进行操作的Util方法?
使用原型?建议?你不会使用它的好例子是什么?
我在当前的工作中使用Java.当我学习编程时,我学习了C++,但从2002年开始就没有触及它(或者不得不接触它).我甚至不记得如何做最简单的事情.
最近,我的工作表达了对为Windows构建的C++应用程序的需求.
我正在寻找书籍/文章/博客文章(资源):
为什么问这个问题?
这个问题是由于我的第一个程序是5行C++(源自依赖于DLL的文档)这一事实引起的.如何以我想要的方式构建它是非常令人生畏的.
话虽如此,我不明白的片段中有很多元素.
我肯定看到原来的需求很小,但我想在未来开始甚至更简单的开发(如片段)之前获得平台/主题的一些背景知识.
提前致谢.
我正在尝试使用Jenkins构建一个多模块Maven项目.
当我使用与 Jenkins 相同的环境(variables/settings.xml/user)在命令行上构建相同的项目时,测试失败会导致构建立即失败:
Failed tests:
testSomething(com.package_name.TestSomethingOrTheOther)
Tests run: .., Failures: 1, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] There are test failures.
...
# Build fails immediately
Run Code Online (Sandbox Code Playgroud)
在詹金斯建筑的地方:
Failed tests:
testSomething(com.package_name.TestSomethingOrTheOther)
Tests run: .., Failures: 1, Errors: 0, Skipped: 0
[ERROR] There are test failures.
...
# Build continues to other modules
...
[INFO] Reactor Summary:
[INFO] ------------------------------------------------------------------------
[INFO] parent ................................................ SUCCESS [.....s]
[INFO] module-that-failed .................................... SUCCESS [.....s] …Run Code Online (Sandbox Code Playgroud) 我在ghci并发现以下工作:
let foo = ["a", "b", "c"]
Run Code Online (Sandbox Code Playgroud)
...但这不起作用:
let bar = ["a",
"b",
"c"]
Run Code Online (Sandbox Code Playgroud)
......也不是这样:
let baz = ["a"] ++
["b"] ++
["c"]
Run Code Online (Sandbox Code Playgroud)
当我尝试将其编译为文件时会出现相同的错误,因此这不是来自ghci使用ghc.
错误:
[1 of 1] Compiling Main ( test.hs, test.o )
test.hs:3:1: error:
parse error (possibly incorrect indentation or mismatched brackets)
Run Code Online (Sandbox Code Playgroud)