小编huy*_*hjl的帖子

使用scalaz的开源项目示例

有人能指出任何使用scalaz的开源项目吗?

我甚至对那些可能以某种小方式使用scalaz的方式感兴趣(与其他编程风格混合或只是从scalaz中提取一些东西).

scala scalaz

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

sbt 0.11运行所需的任务示例

我的项目仍在使用sbt 0.7.7,我发现可以从sbt提示符运行实用程序类非常方便.我还可以将它与单独维护的属性结合使用 - 通常用于从主机更改为主机的环境相关值.这是我在project/build目录下的项目定义的示例:

class MyProject(info: ProjectInfo) extends DefaultProject(info) {
  //...
  lazy val extraProps = new BasicEnvironment {
    // use the project's Logger for any properties-related logging
    def log = MyProject.this.log
    def envBackingPath = path("paths.properties")
    // define some properties that will go in paths.properties
    lazy val inputFile = property[String]
  }

  lazy val myTask = task { args =>
    runTask(Some("foo.bar.MyTask"),
      runClasspath, extraProps.inputFile.value :: args.toList).dependsOn(compile)
      describedAs "my-task [options]"
  }   
}
Run Code Online (Sandbox Code Playgroud)

然后我可以my-task option1 option2在sbt shell下使用我的任务.

我在https://github.com/harrah/xsbt/wiki上阅读了新的sbt 0.11文档,包括有关 …

scala sbt

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

宏在运行时访问源代码文本

是否已经或者是否可以使用Scala宏来访问源文本?例如,我想写这样的代码:

val list = List(1, 2, 3)
val (text, sum) = (list.sum).withSource{(source, sum) => (source, sum)}
// would return ("list.sum", 6)
(list.sum).withSource{(source, sum) => println(s"$source: $sum"}
// prints list.sum: 6
Run Code Online (Sandbox Code Playgroud)

macros scala

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

解析器组合器无法终止 - 如何记录正在发生的事情?

我正在尝试使用解析器组合器,我经常遇到无限递归.这是我遇到的第一个:

import util.parsing.combinator.Parsers
import util.parsing.input.CharSequenceReader

class CombinatorParserTest extends Parsers {

  type Elem = Char

  def notComma = elem("not comma", _ != ',')

  def notEndLine = elem("not end line", x => x != '\r' && x != '\n')

  def text = rep(notComma | notEndLine)

}

object CombinatorParserTest {

  def main(args:Array[String]): Unit = {
    val p = new CombinatorParserTest()
    val r = p.text(new CharSequenceReader(","))
    // does not get here
    println(r)
  }

}
Run Code Online (Sandbox Code Playgroud)

如何打印正在发生的事情?为什么没有完成?

scala parser-combinators

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

Scala中的中缀运算符的实际优先级

2011年5月24日Scala语言规范此处发现的第6.12.3节中有一个拼写错误.这已在邮件列表中得到承认.

中缀运算符的实际优先级是什么?

scala operators

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

如何编写一个枚举器来沿着不同的边界对一个枚举器进行分块

因此,Play2.0 Enumeratee页面显示了使用&>through方法将其更改Enumerator[String]Enumerator[Int]:的示例:

val toInt: Enumeratee[String,Int] = Enumeratee.map[String]{ s => s.toInt }
val ints: Enumerator[Int] = strings &> toInt
Run Code Online (Sandbox Code Playgroud)

还有一个Enumeratee.grouped枚举器可以从各个元素创建块的枚举器.这似乎工作正常.

但我所看到的是,通常的输入将是Array[Byte](由Enumerator.fromFileand和返回Enumerator.fromStream)的形式.考虑到这一点,我想采取这些Array[Byte]输入并将它们变成一个Enumerator[String],例如每个字符串是一行(以a结尾'\n').线条和Array[Byte]元素的边界通常不匹配.如何编写可以将分块数组转换为分块字符串的枚举器?

目的是在每个行Array[Byte]变为可用时将这些行重新组合回浏览器,并保留不属于完整行的剩余字节,直到下一个输入块出现.

理想情况下,我希望有一个方法给出一个iter: Iteratee[Array[Byte], T]和一个Enumerator[Array[Byte]]会给我一个Enumerator[T],我的T元素被解析iter.

附加信息:我有一点时间来清理我的代码,这是我正在尝试做的一个具体的例子.我有以下迭代检测下一行:

import play.api.libs.iteratee._
type AB = Array[Byte]

def takeWhile(pred: Byte => Boolean): Iteratee[AB, AB] = {
  def step(e: Input[AB], acc: AB): …
Run Code Online (Sandbox Code Playgroud)

scala enumerator playframework iterate playframework-2.0

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

遍历状态monad时如何处理嵌套结构

我有一个嵌套的结构,我正在使用scalaz状态monad转换为XML.这很有效,直到我必须处理多级嵌套结构.这是一个类似于我正在做的简化示例.鉴于以下ADT:

sealed trait Nested
case object Leaf extends Nested
case class Foo(inner: Nested) extends Nested
case class Bar(inner: Nested) extends Nested
Run Code Online (Sandbox Code Playgroud)

我使用状态monad编写转换器对象(假设Scalaz7和以下导入import scalaz.{Node => _, _}; import Scalaz._; import scala.xml._):

case class Parents(foos: Int, bars: Int)
type ParentsS[X] = State[Parents, X]

def convertFoo(foo: Foo): ParentsS[Seq[Node]] = for {
  parents <- init[Parents]
  _ <- put[Parents](Parents(parents.foos + 1, parents.bars))
  inner <- convert(foo.inner)
  _ <- put[Parents](parents)
} yield <foo count={ parents.foos.toString }/>.copy(child=inner)

def convertBar(bar: Bar): ParentsS[Seq[Node]] = for {
  parents <- …
Run Code Online (Sandbox Code Playgroud)

stack-overflow scala state-monad scalaz

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

Scala continuation插件是否支持嵌套移位?

我将通过以下Shift/Reset教程:http://www.is.ocha.ac.jp/~asai/cw2011tutorial/main-e.pdf.

到目前为止,我将OchaCaml示例转换为Scala(一直到2.11节)都取得了不错的成绩.但现在我似乎已经碰壁了.来自Asai/Kiselyov的论文中的代码定义了以下递归函数(这是OchaCaml - 我认为):

(* a_normal : term_t => term_t *)
let rec a_normal term = match term with
    Var (x) -> Var (x)
  | Lam (x, t) -> Lam (x, reset (fun () -> a_normal t))
  | App (t1, t2) ->
      shift (fun k ->
        let t = gensym () in (* generate fresh variable *)
        App (Lam (t, (* let expression *)
                  k (Var (t))), (* continue with new variable *)
             App …
Run Code Online (Sandbox Code Playgroud)

continuations haskell scala

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

在尝试使用unboundid LDAP SDK更改scala中的密码时,如何解析"WILL_NOT_PERFORM"MS AD回复?

我正在与Active Directory搏斗,试图让它让我更改密码.我发现了大量有用的信息,但我仍然遇到了持续的错误.

一些代码:

import com.unboundid.ldap.sdk._
import com.unboundid.util.ssl._


def main(args: Array[String]) : Unit = {

var sslUtil = new SSLUtil( new TrustAllTrustManager() )
var con = new LDAPConnection(sslUtil.createSSLSocketFactory())
con.connect("ldap.example.net", 636)
con.bind("ldapadmin", "adminpasswd")
val newPass = "Jfi8ZH8#k".getBytes("UTF-16LE");
val modRequest = new ModifyRequest("dn: cn=Tester Dude,ou=Lab,ou=Org,ou=Provider,DC=example,DC=net",
  "changetype: modify",
  "replace: unicodePwd",
  "unicodePwd: " + '"' + newPass + '"')

println("\nGoing to try to set password to " + newPass + " with: " + modRequest.toString())

try {
  con.modify(modRequest)
} catch {
  case lde:LDAPException => println("failed …
Run Code Online (Sandbox Code Playgroud)

java scala ldap active-directory unboundid-ldap-sdk

5
推荐指数
3
解决办法
3万
查看次数

可以将分区应用于`a - > IO Bool`吗?

我有以下代码:

import System.Directory
import System.FilePath
import Control.Monad (filterM)

filesAndDirs dir = do
  entries <- getDirectoryContents dir
  let filtered = [dir </> e | e <- entries, e `notElem` [".", ".."]]
  files <- filterM doesFileExist filtered 
  dirs <- filterM doesDirectoryExist filtered 
  return (files, dirs)
Run Code Online (Sandbox Code Playgroud)

我想写的是类似的东西return $ partitionM doesFileExist filtered.有没有办法重用或提升partition或是filterM最佳方式的双重用途?

monads haskell

5
推荐指数
0
解决办法
271
查看次数