我想知道是否有一些通用方法来转换"正常"递归foo(...) + foo(...)
作为最后一次调用尾递归.
例如(scala):
def pascal(c: Int, r: Int): Int = {
if (c == 0 || c == r) 1
else pascal(c - 1, r - 1) + pascal(c, r - 1)
}
Run Code Online (Sandbox Code Playgroud)
函数式语言的一般解决方案,用于将递归函数转换为尾部调用等效函数:
一种简单的方法是将非尾递归函数包装在Trampoline
monad中.
def pascalM(c: Int, r: Int): Trampoline[Int] = {
if (c == 0 || c == r) Trampoline.done(1)
else for {
a <- Trampoline.suspend(pascal(c - 1, r - 1))
b <- Trampoline.suspend(pascal(c, r - 1))
} yield a + b
} …
Run Code Online (Sandbox Code Playgroud) 我正在玩Scala(z)学习功能编程.
我有一个类型的值,Future[List[Error \/ Double]]
并希望将其转换为类型的东西Future[[List[Error] \/ List[Double]]
.
目标是对左派和权利进行分组.
我目前有以下内容:
val foo: Future[List[Error] \/ List[Double]] = {
for {
results <- resultsF
} yield
results.foldLeft(\/[List[Error], List[Double]])({
case (acc, v) if v.isRight => v :: \/-(acc)
case (acc, v) if v.isLeft => v :: -\/(acc)
})
}
Run Code Online (Sandbox Code Playgroud)
但是,我得到一个错误,::
这是因为我的累加器不是一个列表(来自外部)\/[List[Error], List[Double]]
.应该怎么做?
如何List[Option[Double]]
使用以下规则汇总选项列表?
List(Some(1), ..., Some(n)) --> Some(1 + ... + n)
List(Some(1), ..., Some(n), None) --> None
List(None, ..., None) --> None
我安装了Intellij的官方SBT插件(仍然是alpha版),我在没有问题的情况下导入了Scala SBT项目(使用build.sbt).但是当我尝试在Scala控制台中导入某些内容时,它会打印出来:
<scala> import recfun.Main._
<console>:7: error: not found: value recfun
import recfun.Main._
但是当我在终端中运行SBT启动完全相同的命令时,它工作正常.
问题是什么?
我有一个使用 OpenGL 绘制圆的函数,我想向它传递一个包含 x 和 y 坐标以及半径的结构。问题是这个相同的函数必须与 3 个不同的结构一起使用,所有结构都包含坐标、半径和绘图函数不使用的其他一些内容。
有没有办法让 3 个不同的结构只有一个参数(一次只发送一个)。
我希望我说得足够精确。
PS:函数必须是“抽象的”。
我试图实现一个只有在用户登录时才能看到的下拉列表.下拉列表在"if"语句之外但不在内部时工作.显示按钮"Foo"和下拉按钮,但它不会"下拉".
了header.html
<!-- Header -->
<template name="header">
<nav>
<div class="nav-wrapper">
<a class="brand-logo" href="{{pathFor 'home'}}">Logo</a>
<ul id="nav-mobile" class="right hide-on-med-and-down">
{{#if currentUser}}
<!-- dropdown1 trigger -->
<li>
<a class="dropdown-button" href="#!" data-activates="dropdown1">
<i class="mdi-navigation-more-vert"></i>
</a>
</li>
<li><a href="#">Foo</a></li>
{{else}}
<li><a href="{{pathFor 'signin'}}">Sign in</a></li>
{{/if}}
<li><a href="{{pathFor 'about'}}">About</a></li>
</ul>
</div>
</nav>
<!-- dropdown1 structure -->
<ul id="dropdown1" class="dropdown-content">
<li class="signout"><a href="#!">Sign out</a></li>
</ul>
</template>
Run Code Online (Sandbox Code Playgroud)
header.js
Template.header.rendered = function () {
$(".dropdown-button").dropdown({
belowOrigin: true // Displays dropdown below the button
});
};
Run Code Online (Sandbox Code Playgroud)
可能是什么问题呢?
我有一个可折叠的(实体化),其元素是从a创建的for each
,但"下拉列表"不起作用.一切都没有在for each
工作中.
我该如何解决这个问题?
jobList.html
<template name="jobList">
<ul class="collapsible" data-collapsible="accordion">
{{#each jobs}}
<li>
<div class="collapsible-header">{{title}}</div>
<div class="collapsible-body"><p>{{description}}</p></div>
</li>
{{/each}}
</ul>
Run Code Online (Sandbox Code Playgroud)
jobList.js
Template.jobList.rendered = function () {
$('.collapsible').collapsible({
accordion: false
});
};
Template.jobList.helpers({
jobs: function() {
return Jobs.find();
}
});
Run Code Online (Sandbox Code Playgroud)
该模板jobList
位于另一个模板中,该模板无需任何操作{{> jobList}}
.
我有两个monad实例val a: M[A]
和val b: M[B]
.在以下代码情况下会有任何性能差异吗?
def f: (A, B) => C
val applicativeCombine = (a |@| b)(f)
val monadCombine =
for {
aa <- a
bb <- b
} yield f(aa, bb)
Run Code Online (Sandbox Code Playgroud)
......还是依赖?
我开始查看一些Haskell
代码并发现:
foo :: ([a] -> [b]) -> [a] -> [(a, b)]
let foo = (<*>) zip
Run Code Online (Sandbox Code Playgroud)
我不明白这是如何工作的,ap
需要一个f (a -> b) -> f a
却zip
是类型[a] -> [b] -> ([a, b])
.我明白这f a -> f b
会匹配[a] -> [b]
,但不会f (a -> b)
.
我有一张类型的地图Map[A, Map[B, C]]
.
如何将其反转为具有类型的地图Map[B, Map[A, C]]
?
我有Json
字符串:
[{"cid":"1039420885783365","name":"","s":"TSLA160916C00005000","e":"OPRA","p":"219.64","cs":"chb"," c":"0.00","cp":"0.00","b":"190.30","a":"194.85","oi":"2","vol":"-","strike" :"5.00","expiry":"Sep 16, 2016"}, ... ]
我的代码:
case class OptionEntry(cid: String,
name: String,
s: String,
e: String,
p: String,
cs: String,
c: String,
cp: String,
b: String,
a: String,
oi: String,
vol: String,
strike: String,
expiry: String)
val optionEntries = Json.parse(jsonString)
implicit val optionEntryReads: Reads[OptionEntry] = (
(JsPath \ "cid").read[String] and
(JsPath \ "name").read[String] and
(JsPath \ "s").read[String] and
(JsPath \ "e").read[String] and
(JsPath \ "p").read[String] and
(JsPath \ "cs").read[String] and
(JsPath \ "c").read[String] and
(JsPath …
Run Code Online (Sandbox Code Playgroud) 假设我们有EitherT[F, String, A]
.withFilter
Scalaz 的函数使用了String
's Monoid 的零,以便Left
在过滤器失败时填写.
因此,没有有意义的错误消息.
我怎么能实现左边的"不积极"形式的东西.
val a: Either[Future, String, Int] = -1.point[EitherT[Future, String, ?]]
val foo = for {
aa <- a
if aa >= 0
} yield aa
Run Code Online (Sandbox Code Playgroud)
只是hacks的方法filter
和withFilter
方法是否EitherT
满足for-comprehension要求?
scala ×8
scalaz ×3
either ×2
html ×2
javascript ×2
list ×2
materialize ×2
meteor ×2
abstract ×1
applicative ×1
arguments ×1
c ×1
collapsable ×1
console ×1
dictionary ×1
filter ×1
function ×1
haskell ×1
iron-router ×1
json ×1
performance ×1
recursion ×1
sbt ×1
structure ×1
sum ×1
zip ×1