为了帮助理解monad是什么,有人可以使用java提供一个例子吗?他们有可能吗?
如果你从这里下载预发布的lambda兼容JDK8,可以使用java表达lambda表达式http://jdk8.java.net/lambda/
使用此JDK的lambda示例如下所示,有人可以提供相对简单的monad吗?
public interface TransformService {
int[] transform(List<Integer> inputs);
}
public static void main(String ars[]) {
TransformService transformService = (inputs) -> {
int[] ints = new int[inputs.size()];
int i = 0;
for (Integer element : inputs) {
ints[i] = element;
}
return ints;
};
List<Integer> inputs = new ArrayList<Integer>(5) {{
add(10);
add(10);
}};
int[] results = transformService.transform(inputs);
}
Run Code Online (Sandbox Code Playgroud) Slick如何翻译代码,例如:
val q2 = for {
c <- Coffees if c.price < 9.0
s <- Suppliers if s.id === c.supID
} yield (c.name, s.name)
for(t <- q2) println(" " + t._1 + " supplied by " + t._2)
Run Code Online (Sandbox Code Playgroud)
进入JDBC?
它是否使用Scala Virtualized?它是否使用其他方法?
我有嵌套的类/对象,并希望使用SLICK在数据库中存储(和检索)它们.据我所知,使用SLICK映射投影将是关键.此外,我使用伴侣对象在嵌套对象和平面结构之间进行映射(存储在DB表中).我想做这样的事情(简化示例):
case class Foo(id: Int, myBar: Bar)
case class Bar(myInt: Int, myString: String)
object Foo {
def apply(id: Int, myInt: Int, myString: String): Foo = Foo(id, Bar(myInt, myString))
override def unapply(f: Foo) = (f.id, f.myBar.myInt, f.myBar.myString)
}
object TTable extends Table[Foo]("FOO") {
def id = column[Int]("id", O.PrimaryKey)
def myInt = column[Int]("myInt", O NotNull)
def myString = column[String]("myString", O NotNull)
def * = id ~ myInt ~ myString <> (Foo.apply _, Foo.unapply _)
def query(db: Database, id: Int): Option[Foo] = db …Run Code Online (Sandbox Code Playgroud) 我正在关注自动增量字段的Slick文档示例,我在创建映射投影时遇到了麻烦......好吧,只有一列.
case class UserRole(id: Option[Int], role: String)
object UserRoles extends Table[UserRole]("userRole") {
def id = column[Int]("ID", O.PrimaryKey, O.AutoInc)
def role = column[String]("ROLE")
// ...
def * = id.? ~ role <> (UserRole, UserRole.unapply _)
// NEXT LINE ERRORS OUT
def forInsert = role <> ({t => UserRole(None, t._1)}, {(r: UserRole) => Some((r.role))}) returning id
}
Run Code Online (Sandbox Code Playgroud)
错误是"value <>不是scala.slick.lifted.Column [String]"的成员
我还认为设计我的架构更有效:
case class UserRole(role: String)
object UserRoles extends Table[UserRole]("userRole") {
def role = column[Int]("ROLE", O.PrimaryKey)
// …Run Code Online (Sandbox Code Playgroud) 假设我有一张桌子:
object Suppliers extends Table[(Int, String, String, String)]("SUPPLIERS") {
def id = column[Int]("SUP_ID", O.PrimaryKey)
def name = column[String]("SUP_NAME")
def state = column[String]("STATE")
def zip = column[String]("ZIP")
def * = id ~ name ~ state ~ zip
}
Run Code Online (Sandbox Code Playgroud)
可以通过以下方式访问表的数据库名称:Suppliers.tableName
这是由AbstractTable上的Scaladoc支持的.
例如,上表的数据库名称是"SUPPLIERS".
翻翻AbstractTable,getLinearizedNodes并且indexes看起来前途无量.但是,字符串表示中没有列名称.
我认为*表示"我通常感兴趣的所有列." *是一个MappedProjection,它有这个签名:
final case class MappedProjection[T, P <: Product](
child: Node,
f: (P) ? T,
g: (T) ? Option[P])(proj: Projection[P]) …Run Code Online (Sandbox Code Playgroud) 我在这里工作了23个字段,最后算了.在使用外键减少31张现场表后,我一般都试图计算它们.
关于如何阅读和理解Slick的一个非常好的Faiz提供的模式代码的基本解释.
超过22个参数......
Stefan Zeigar对他在本次讨论中编写的示例代码非常有帮助,并且在Github上更直接地链接到这里
我认为以上内容足以让我开始对我的应用进行重构,以便CRUD可行.如果出现问题我会更新这个问题或者提出新的问题并让我停滞不前.事情是...
我想念用于查询的理解.我在谈论Slick的查询模板
当我使用for comprehension时遇到的问题是表...可能会有
object Monsters extends Table[Int]("monster_table"){
// lots of column definitions
def * = id /* for a Table[Int] despite
having 21 other columns I'm not describing
in this projection/ColumnBase/??? */
}
Run Code Online (Sandbox Code Playgroud)
并且*投影不会描述我想在查询中返回的所有内容.
通常简单的理解Slick查询模板看起来像这样:
def someQueryTemplate = for {
m <- Monsters
} yield m
Run Code Online (Sandbox Code Playgroud)
并且m将是一个Int而不是我想要的整个对象,因为我声明该表是一个Table[Int] 因为我无法构造22个参数的映射投影,因为需要为编译器支持每个类生成的所有代码生成元组和任意性 …
我有一个跟随我最近问的另一个Slick问题(Slick table Query:Trouble with recognition value).请多多包涵!!我是数据库的新手,Slick在文档方面似乎特别差.无论如何,我有这张桌子:
object Users extends Table[(Int, String)]("Users") {
def userId = column[Int]("UserId", O.PrimaryKey, O.AutoInc)
def userName = column[String]("UserName")
def * = userId ~ userName
}
Run Code Online (Sandbox Code Playgroud)
第一部分
我正在尝试使用此函数进行查询:
def findByQuery(where: List[(String, String)]) = SlickInit.dbSlave withSession {
val q = for {
x <- Users if foo((x.userId, x.userName), where)
} yield x
q.firstOption.map { case(userId, userName) =>
User(userId, userName)}
}
Run Code Online (Sandbox Code Playgroud)
其中"where"是搜索查询列表// ex.("userId","1"),("userName","Alex")"foo"是测试相等性的辅助函数.我遇到了类型错误.
x.userId的类型为Column [Int].如何将其作为Int来操纵?我试过铸造,例如:
foo(x.userId.asInstanceOf[Int]...)
Run Code Online (Sandbox Code Playgroud)
但我也遇到了麻烦.如何处理Slick返回类型?
第二部分 是否有人熟悉铸造功能:
def*= userId~userName <>(User,User.unapply _)
?我知道这个问题有一些很好的答案,最值得一提的是:到目前为止我无法理解的scala光滑方法和一个非常相似的问题:在SLICK中用伴随对象映射投影 …