- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 shapeless 的新手,一直在尝试练习一些类型级别的编程。我拿了 Problem #1 from Project Euler作为我的第一个挑战。
我从编写常规 Scala 代码开始:
object ProjectEuler1 {
def e1(limit: Int) = (1 until limit).foldLeft(0) {
case (acc, x) if x % 3 * x % 5 == 0 => acc + x
case (acc, _) => acc
}
val out = e1(10)
assert(out == 23)
}
poly
想出了这个工作的无形实现。 :
object ProjectEuler1Shapeless extends App {
import shapeless._
import nat._
import ops.nat._
import poly._
import test.typed
trait eLP extends Poly1 {
implicit def default[A <: Nat] = at[A] { _ => _0 }
}
object e extends eLP {
implicit def match3[A <: Nat](implicit ev: Mod.Aux[A, _3, _0]) = at[A](identity)
implicit def match5[A <: Nat](implicit ev: Mod.Aux[A, _5, _0]) = at[A](identity)
}
object sum extends Poly2 {
implicit def sum[A <: Nat, B <: Nat, Z <: Nat](implicit s: Sum.Aux[A, B, Z],
z: Witness.Aux[Z]) =
at[A, B] { (_, _) => z.value }
}
type _23 = Succ[_22]
val l = _1 :: _2 :: _3 :: _4 :: _5 :: _6 :: _7 :: _8 :: _9 :: HNil
val out = l.map(e).foldLeft(_0)(sum)
typed[_23](out)
}
object ProjectEuler1Shapeless2 extends App {
import shapeless._
import nat._
import ops.nat._
import test.typed
class E1[I <: Nat, N <: Nat]
trait ELP0 {
implicit def default[I <: Nat, M <: Nat] = new E1[I, _0]
}
trait ELP1 extends E1LP0 {
implicit def match3[A <: Nat](implicit ev: Mod.Aux[A, _3, _0]) = new E1[A, A]
implicit def match5[A <: Nat](implicit ev: Mod.Aux[A, _5, _0]) = new E1[A, A]
}
object E1 extends E1LP1 {
implicit def combine[I <: Nat, L <: Nat, M <: Nat](implicit e1: E1[I, L],
m: E1[Succ[I], M],
sum: Sum[L, M]) =
new E1[Succ[Succ[I]], sum.Out]
}
def e1[N <: Nat](limit: Nat)(implicit e: E1[limit.N, N], w: Witness.Aux[N]): N = w.value
val f1 = e1(1)
typed[_0](f1)
val f2 = e1(2)
typed[_0](f2)
val f3 = e1(3)
typed[_3](f3) // Does not compile!
}
_0
.我猜它是从
def default
获取实例的.
最佳答案
我发现归纳地思考这个问题更容易一些(至少在类型级别)。首先我们可以定义一个返回 N
的辅助类型类。如果 N
是 M
中的数字之一的倍数, 和 _0
除此以外:
import shapeless._, nat._0, ops.nat.Mod
trait IfMultiple[N <: Nat, M <: HList] { type Out <: Nat }
trait LowPriorityIfMultiple {
type Aux[N <: Nat, M <: HList, Out0 <: Nat] = IfMultiple[N, M] {
type Out = Out0
}
implicit def isMultiple1[N <: Nat, H <: Nat, T <: HList](implicit
ifMultiple: IfMultiple[N, T]
): Aux[N, H :: T, ifMultiple.Out] = new IfMultiple[N, H :: T] {
type Out = ifMultiple.Out
}
}
object IfMultiple extends LowPriorityIfMultiple {
implicit def ifMultiple0[N <: Nat]: Aux[N, HNil, _0] =
new IfMultiple[N, HNil] {
type Out = _0
}
implicit def ifMultiple2[N <: Nat, H <: Nat, T <: HList](implicit
mod: Mod.Aux[N, H, _0]
): Aux[N, H :: T, N] = new IfMultiple[N, H :: T] {
type Out = N
}
}
_0
中的所有这些值加起来。至
N - _1
:
import nat._1, ops.nat.Sum
trait SumOfMultiples[N <: Nat, M <: HList] extends DepFn0 { type Out <: Nat }
object SumOfMultiples {
type Aux[N <: Nat, M <: HList, Out0 <: Nat] = SumOfMultiples[N, M] {
type Out = Out0
}
def apply[N <: Nat, M <: HList](implicit
som: SumOfMultiples[N, M]
): Aux[N, M, som.Out] = som
implicit def sum0[M <: HList]: Aux[_1, M, _0] =
new SumOfMultiples[_1, M] {
type Out = _0
def apply(): _0 = _0
}
implicit def sumN[P <: Nat, M <: HList, NV <: Nat, PT <: Nat, NT <: Nat](implicit
ifMultiple: IfMultiple.Aux[P, M, NV],
som: Aux[P, M, PT],
sum: Sum.Aux[NV, PT, NT],
wit: Witness.Aux[NT]
): Aux[Succ[P], M, NT] = new SumOfMultiples[Succ[P], M] {
type Out = NT
def apply(): NT = wit.value
}
}
import nat._, test.typed
val result = SumOfMultiples[_10, _3 :: _5 :: HNil]
typed[Succ[_22]](result())
Nat
范围,然后用
Poly2
折叠它使用
IfMultiple
.你也可以定义一个
IsMultiple
类型类只是见证
N
是
M
中的数字之一的倍数——我第一次快速尝试这样做,但我遇到了歧义问题,所以我选择了上面的类似版本。不过,这里的实现相当简单,除非您有其他应用程序,例如
Nat
范围,我认为这是一个非常合理的解决方案。
关于用于 Euler 项目的 Scala 无形代码 #1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31544673/
我正在尝试将欧拉旋转顺序从现有的 xyz 转换为 zxy。谁能帮我做这件事?谢谢。 编辑:我发现这篇非常有用的文章,认为它可以帮助其他人在同一条道路上 - http://knol.google.com
考虑修改后的欧拉问题 #4——“找出最大回文数,它是 100 到 9999 之间的两个数字的乘积。” rev :: Int -> Int rev x = rev' x 0 rev' :: Int ->
例如,我如何在 R 中输入值 e^2? 最佳答案 R 表达式 exp(1) 代表e,并且 exp(2) 代表e^2。 这是有效的,因为 exp 是以 e 为底的求幂函数。 关于R编程: How do
我正在尝试了解 Euler Tour 算法以及为什么它在树遍历中很受欢迎。但是,我看不出 Euler Tour 和树的预序遍历之间的区别。 假设你有一棵树: A / \ B
我一直在尝试对 Euler #22 问题中的这些名称进行排序。我尝试了很多方法来交换字符串。我每次都有问题。有些是随机的符号;有些是随机的。在其他情况下,我在交换时出现了溢出(?)的名字(就像在尝试交
我想为游戏创建一些 physx,我从小例子开始了解它是如何工作的。在此期间我遇到了一些问题,但我在 90% 内解决了它们。 为了创建我的示例,我研究了一些其他示例并创建了我使用的示例:codeflow
/* The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 60
我正在研究 Problem 8 of Project Euler我必须从给定的 1000 位数字中找到 13 个连续数字的最大乘积。我将该数字存储在一个文本文件中,然后将其输入到我的源代码中。我的代码
我目前正在尝试解决二体问题,然后我可以升级到更多行星,但它不起作用。它正在输出我不可能的位置。有谁知道是什么原因造成的? 这是我使用的代码: day = 60*60*24 # Constants G
我最近一直在研究毕达哥拉斯三元组和 Euler Bricks并且想知道生成所有这些的最佳方法是什么。 我从更广泛的阅读中知道有 10 个 c n: break
在我当前的 Project Euler problem 5 ,我有一个“有效”的解决方案。它适用于较小的数字(问题中的示例),但不适用于实际问题,因为我在强行使用它,并且程序没有完成。 问题的解释如下
基于标准定义,Eulerian Path是图中的一条路径,它恰好访问每条边一次。 现在,我试图在有向图中找到欧拉路径。我知道欧拉电路的算法。如果一个图有欧拉回路,它就有欧拉路径,这似乎是微不足道的。
我正在研究项目欧拉程序是为了“启蒙”,而不仅仅是解决它们。我已经在 80x80 矩阵上使用动态程序解决了问题 81,但是当我尝试使用统一成本搜索解决它时,我的程序消失在永无止境。我只想知道使用统一成本
我有一个关于 Project Euler 问题和使用循环展开优化的问题。 问题描述:2520是能被1到10的每一个数整除而没有余数的最小数。能被 1 到 20 的所有数字整除的最小正数是多少? 解决方
我正在使用以下行旋转 SCNNode: let rotate = SCNAction.rotateByAngle(CGFloat(M_PI), aroundAxis:SCNVector3Make(0
我想获得更精确的 1/7,但它被截断了。如何在转换有理数时获得更好的精度? >>> str(1.0/7)[:50] '0.142857142857' 最佳答案 Python 有一个用于任意精度计算的内
我正在处理项目 euler 中的问题 401,我在 python 中编写了我的解决方案,但它需要几天时间才能运行,显然我需要加快速度或使用不同的方法。我在 Haskell 中遇到了一个看起来与我的 p
我正在处理 Project Euler 的第四个问题并且遇到了 stackoverflow 异常。我不是在寻求解决问题的帮助,我只是想解释为什么我会收到 stackoverflow 异常。这通常是因为
我已经做了几天 Euler 项目,我不得不承认它很有趣。但是,如果有一些更专注于计算机科学而不是数学的东西,那就太好了。这样的事情对于练习算法和数据结构会很有趣,例如在准备编码面试时。 最佳答案 您可
13195 的质因数是 5、7、13 和 29。 数字 600851475143 的最大质因数是多少? 我自己在Project Euler上解决了这个问题,很慢,后来在某人的github账号上找到了这
我是一名优秀的程序员,十分优秀!