我尝试了解一些Slick的作品以及它需要什么.
这是一个例子:
package models
case class Bar(id: Option[Int] = None, name: String)
object Bars extends Table[Bar]("bar") {
def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
// This is the primary key column
def name = column[String]("name")
// Every table needs a * projection with the same type as the table's type parameter
def * = id.? ~ name <>(Bar, Bar.unapply _)
}
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下*这里方法的目的是什么<>,为什么unapply?什么是Projection - 方法~'返回实例Projection2?
难道不知道两个函数是否相同吗?例如,编译器编写者想要确定开发人员编写的两个函数是否执行相同的操作,可以使用哪些方法来确定那个函数?或者我们可以做些什么来找出两个TM是相同的?有没有办法规范机器?
编辑:如果一般情况是不可判定的,在正确地说两个函数是等价的之前,您需要多少信息?
有没有人有一个Scala monad的完整工作示例来解决现实世界的问题并且与用Java编写的相同代码进行比较?
我正在玩 Java 代码以创建一个函数式风格的 monad,但是我在使用泛型时感到震惊,如果我不强制转换我的对象,Java 编译器会给我一个编译错误(尽管泛型可以解决这个问题!)
这是用法:
//COMPILATION ERROR! It requires a cast to String
String message = If.of("Hi", s->s!=null).apply(s->s+" guys!").get();
Run Code Online (Sandbox Code Playgroud)
允许:
这是我的单子:
import java.util.function.Function;
import java.util.function.Predicate;
public class If<T, R> {
private T t;
private Predicate predicate;
private If(T t, Predicate predicate) {
this.t = t;
this.predicate = predicate;
}
public static<T> If of(T t, Predicate predicate) {
return new If(t, predicate);
}
public If<R,R> apply(Function<T, R> function) {
if(predicate!=null && predicate.test(t)){
return new If<R, R>(function.apply(t), null);
}
return If.of(this.t, …Run Code Online (Sandbox Code Playgroud)