我试图使用:~:Data.Type.Equality在编译时确定类型相等。我的期望是它的行为符合 Scala 确定类型相等的标准方法:
case class Equals[A >: B <:B , B]()
Equals[Int, Int] // compiles fine
Equals[String, Int] // doesn't compile
Run Code Online (Sandbox Code Playgroud)
所以我尝试了
foo :: Int :~: Bool
foo = undefined
Run Code Online (Sandbox Code Playgroud)
我预计会失败,因为Int :~: Bool它不适合居住。但它编译得很好。
应该如何:~:工作?的文档Data.Type.Equality对我来说非常难以理解,而且我无法在任何地方找到简洁的示例。
也许我完全偏离了轨道。那么,我怎样才能Equals在 Haskell 中实现示例的语义呢?
为什么当我this在变量声明中使用引用时,没有出现非法正向引用?有this和没有声明之间有什么区别?
由于非法前向引用,以下示例无法编译:
class FailsToCompile {
int a = b; //illegal forward reference
int b = 10;
}
Run Code Online (Sandbox Code Playgroud)
通过排位赛使用b的this编译错误消失。
class Compiles {
int a = this.b; //that's ok
int b = 10;
}
Run Code Online (Sandbox Code Playgroud) 例
给定数组[1,2,3]或[1,2,3,4],请打印长度为3的所有唯一组合。
码
public class PrintCombo {
public void printCombo(int [] a, int [] buffer, int startIndex, int bufferIndex){
printArray(buffer);
if(buffer.length == bufferIndex){
System.out.println();
System.out.println("SOLUTION START");
printArray(buffer);
System.out.println("SOLUTION END");
System.out.println();
return;
}
if(startIndex == a.length){
return;
}
for(int i = startIndex; i<a.length; i++){
buffer[bufferIndex] = a[i];
printCombo(a,buffer,i+1,bufferIndex+1);
}
}
public void printArray(int [] buffer){
for(int i = 0; i<buffer.length; i++){
System.out.print(" "+buffer[i]);
}
System.out.println();
}
}
Run Code Online (Sandbox Code Playgroud)
输出值
对于数组[1,2,3] ==> 1,2,3
对于数组[1,2,3,4] ==> 1,2,3 || 1,2,4 || 1,3,4 || 2,3,4
问题 …
假设我试图“抽象执行”:
import scala.language.higherKinds
class Operator[W[_]]( f : Int => W[Int] ) {
def operate( i : Int ) : W[Int] = f(i)
}
Run Code Online (Sandbox Code Playgroud)
现在,我可以定义一个Operator[Future]或Operator[Task]等。例如...
import scala.concurrent.{ExecutionContext,Future}
def futureSquared( i : Int ) = Future( i * i )( ExecutionContext.global )
Run Code Online (Sandbox Code Playgroud)
以REPL风格...
scala> val fop = new Operator( futureSquared )
fop: Operator[scala.concurrent.Future] = Operator@105c54cb
scala> fop.operate(4)
res0: scala.concurrent.Future[Int] = Future(<not completed>)
scala> res0
res1: scala.concurrent.Future[Int] = Future(Success(16))
Run Code Online (Sandbox Code Playgroud)
万岁!
但是我也可能想要一个简单的同步版本,所以我在某个地方定义
type Identity[T] = T
Run Code Online (Sandbox Code Playgroud)
我可以定义一个同步运算符...
scala> def square( …Run Code Online (Sandbox Code Playgroud) 在学习 Yoneda 引理时,我发现了 Haskell 中潜在的自然同构的以下编码:
forward :: Functor f => (forall r . (a -> r) -> f r) -> f a
forward f = f id
backward :: Functor f => f a -> (forall r. (a -> r) -> f r)
backward x f = fmap f x
Run Code Online (Sandbox Code Playgroud)
我尝试简化backwardto的实现flip fmap,但失败了,因为后者具有 type f a -> (a -> r) -> f r。
从这里开始,我就一直致力于查明这两种实现之间的差异。更重要的是,将任一函数应用于具体函子都会产生相同的结果类型:
ghci> :t bw (Just "")
bw (Just "") :: (String -> r) …Run Code Online (Sandbox Code Playgroud) 通过 Representables 进行记忆化一文很好地解释了如何通过 Representables 记忆递归函数。首先将斐波那契数列实现为 的固定点fibOp:
fibOp :: Num a => (Natural -> a) -> (Natural -> a)
fibOp v 0 = 0
fibOp v 1 = 1
fibOp v n = v (n-1) + v (n-2)
fix f = let x = f x in x
fibNaive :: Num a => Natural -> a
fibNaive = fix fibOp
Run Code Online (Sandbox Code Playgroud)
此实现效率不高,因为它多次计算相同的值。
文章接着介绍了互反函数streamTabulate和streamIndex(稍后将在Representable类型类中进行推广)。这些函数允许我们实现以下的记忆版本fibNaive:
fibSmart :: Num a => Natural -> a …Run Code Online (Sandbox Code Playgroud) scala> sealed trait Gender
defined trait Gender
scala> case object Male extends Gender
defined module Male
scala> case object Female extends Gender
defined module Female
scala> Map(Male -> Male, Female -> Female, Male -> Female, Female -> Male)
res2: scala.collection.immutable.Map[Product with Gender,Product with Gender] =
Map((Male,Female), (Female,Male))
Run Code Online (Sandbox Code Playgroud)
为什么在上面的代码中,类型res2是?Map[Product with Gender, Product with Gender]而不是Map[Gender, Gender]?为什么我提供给地图的四个条目,只添加了两个?
我的代码如下
val hash = new HashMap[String, List[Any]]
hash.put("test", List(1, true, 3))
val result = hash.get("test")
result match {
case List(Int, Boolean, Int) => println("found")
case _ => println("not found")
}
Run Code Online (Sandbox Code Playgroud)
我希望打印出"找到"但打印出"未找到".我正在尝试匹配任何具有Int,Boolean,Int三种元素的List
我想要一个像下面这样的属性,它是地图
propertymap = {
key1:'{subkey1:'subvalue1',subkey2:'subvalue2'}',
key2:'{subkey3:'subvalue3',subkey4:'subvalue4'}' }
@Value("#{${propertymap}}")
private Map<String,Map<String,String>> propertymap;
Run Code Online (Sandbox Code Playgroud)
在我的配置类中使用了类似上面的代码,但出现错误。请让我知道是否有办法做到这一点。
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.util.Map nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelParseException: EL1043E:(pos 17): Unexpected token. Expected 'rcurly(})' but was 'identifier'
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
Run Code Online (Sandbox Code Playgroud) 我有两张当前和之前的地图,我想看看两张地图之间是否有任何差异.如果currentMap中存在新键,或者同一个键的值不同,我可以停止.
Map<String, String> previousValue;
Map<String, String> currValue;
boolean isChangePresent = currValue.entrySet().stream().anyMatch(
x -> !previousValue.containsKey(x.getKey()) ||
(previousValue.get(x.getKey()) != null && !previousValue.get(x.getKey()).equals(
x.getValue())));
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来做这个或内置的实用功能,这样做的东西?
Haskell的基本库包含几个函数,它们各自的数据类型的小写版本,例如bool,maybe和either.在Data.Bool.Extra的源代码中,该bool函数清楚地表示为数据类型的catamorphism:
bool = curry cata
Run Code Online (Sandbox Code Playgroud)
现在,使用Recursion Schemes by Example中定义的catamorphism ,看起来上面提到的基本库函数都是它们数据类型的所有catamorphisms,例如对于Maybe:
-- base library definition
maybe n _ Nothing = n
maybe _ f (Just x) = f x
-- definition with cata
newtype Fix f = Fix {unFix :: f (Fix f)}
cata :: Functor f => (f a -> a) -> Fix f -> a
cata alg = alg . fmap (cata alg) . unFix
maybe n f m …Run Code Online (Sandbox Code Playgroud) 我有一个班级看起来像这样.
// just followed the T, U, V...
public class Some<T...., U....> {
}
Run Code Online (Sandbox Code Playgroud)
我需要添加一个像这样的实例方法.
// not a static method
// just followed from BiFunction<T, U, R>.class
protected <U, R> R apply(final BiFunction<T, U, R> function,
final U u) {
}
Run Code Online (Sandbox Code Playgroud)
该T方法与T该类的方法相同.但是该U方法的方法不一定与U该类方法相同.
我应该换一个U吗?
换一种说法,
那两个U一样吗?
我有BitSet1 - {2,4,6,8}另一个BitSet2 - {10,20}.如何组合两个Bitsets并获得最终结果BitSet as {2,4,6,8,10,20}.请帮忙 ?
java ×6
haskell ×4
scala ×3
recursion ×2
bitset ×1
catamorphism ×1
combinations ×1
config ×1
forall ×1
generics ×1
guava ×1
map ×1
memoization ×1
spring ×1
spring-mvc ×1
types ×1