小编Den*_*VDB的帖子

将正常递归转换为尾递归

我想知道是否有一些通用方法来转换"正常"递归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)

函数式语言的一般解决方案,用于将递归函数转换为尾部调用等效函数:

一种简单的方法是将非尾递归函数包装在Trampolinemonad中.

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)

recursion scala tail-recursion pascals-triangle

16
推荐指数
3
解决办法
7864
查看次数

未来[List [Error\/ Double]]到Scala中的Future [[List [Error]\/ List [Double]]

我正在玩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]].应该怎么做?

scala list either scalaz

6
推荐指数
1
解决办法
124
查看次数

总结Scala中的选项列表

如何List[Option[Double]]使用以下规则汇总选项列表?

  • List(Some(1), ..., Some(n)) --> Some(1 + ... + n)
  • List(Some(1), ..., Some(n), None) --> None
  • List(None, ..., None) --> None

scala sum list

6
推荐指数
1
解决办法
1991
查看次数

无法使用IntelliJ SBT控制台导入

我安装了Intellij的官方SBT插件(仍然是alpha版),我在没有问题的情况下导入了Scala SBT项目(使用build.sbt).但是当我尝试在Scala控制台中导入某些内容时,它会打印出来: <scala> import recfun.Main._ <console>:7: error: not found: value recfun import recfun.Main._

但是当我在终端中运行SBT启动完全相同的命令时,它工作正常.

问题是什么?

console scala intellij-idea sbt

5
推荐指数
1
解决办法
1018
查看次数

C :为一个函数参数发送不同的结构

我有一个使用 OpenGL 绘制圆的函数,我想向它传递一个包含 x 和 y 坐标以及半径的结构。问题是这个相同的函数必须与 3 个不同的结构一起使用,所有结构都包含坐标、半径和绘图函数不使用的其他一些内容。

有没有办法让 3 个不同的结构只有一个参数(一次只发送一个)。

我希望我说得足够精确。

PS:函数必须是“抽象的”。

c arguments structure function abstract

4
推荐指数
1
解决办法
3274
查看次数

实现:"if"语句中的下拉列表不起作用

我试图实现一个只有在用户登录时才能看到的下拉列表.下拉列表在"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)

可能是什么问题呢?

html javascript materialize meteor iron-router

4
推荐指数
1
解决办法
1064
查看次数

Meteor + Materialize:可折叠为每个都不起作用

我有一个可折叠的(实体化),其元素是从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}}.

html javascript collapsable materialize meteor

3
推荐指数
1
解决办法
1673
查看次数

应用仿函数vs monad在Scala中的表现

我有两个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)

......还是依赖?

performance functional-programming scala scalaz

3
推荐指数
1
解决办法
160
查看次数

部分应用带拉链的<*>

我开始查看一些Haskell代码并发现:

foo :: ([a] -> [b]) -> [a] -> [(a, b)]
let foo = (<*>) zip
Run Code Online (Sandbox Code Playgroud)

我不明白这是如何工作的,ap需要一个f (a -> b) -> f azip是类型[a] -> [b] -> ([a, b]).我明白这f a -> f b会匹配[a] -> [b],但不会f (a -> b).

zip haskell functional-programming applicative

3
推荐指数
1
解决办法
74
查看次数

在Scala中反转嵌套Map

我有一张类型的地图Map[A, Map[B, C]].

如何将其反转为具有类型的地图Map[B, Map[A, C]]

dictionary scala

2
推荐指数
1
解决办法
267
查看次数

Play Json 中的 ValidationError(缺少路径)

我有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)

json scala playframework

1
推荐指数
1
解决办法
2604
查看次数

EitherT过滤器在Scalaz中出错

假设我们有EitherT[F, String, A].withFilterScalaz 的函数使用了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的方法filterwithFilter方法是否EitherT满足for-comprehension要求?

scala filter either scalaz

1
推荐指数
1
解决办法
227
查看次数