小编lai*_*onh的帖子

R读取zip数据文件而不解压缩

我有一个非常大的zip文件,我试图将其读入R而不解压缩它如下:

temp <- tempfile("Sales", fileext=c("zip"))
data <- read.table(unz(temp, "Sales.dat"), nrows=10, header=T, quote="\"", sep=",")

Error in open.connection(file, "rt") : cannot open the connection
In addition: Warning message:
In open.connection(file, "rt") :
  cannot open zip file 'C:\Users\xxx\AppData\Local\Temp\RtmpyAM9jH\Sales13041760345azip'
Run Code Online (Sandbox Code Playgroud)

r

60
推荐指数
4
解决办法
6万
查看次数

lambda函数也不是具有Function1特征的对象吗?

object MyApp {
  def printValues(f: {def apply(x: Int): Int}, from: Int, to: Int): Unit = {
    println(
      (from to to).map(f(_)).mkString(" ")
    )
  }

  def main(args: Array[String]): Unit = {
    val anonfun1 = new Function1[Int, Int] {
      final def apply(x: Int): Int = x * x
    }

    val fun1 = (x:Int)=>x*x
    printValues(fun1, 3, 6)
  }
}
Run Code Online (Sandbox Code Playgroud)

我认为scala中的lambda函数也是扩展Function1特性的对象.但是这个代码失败了printValues(fun1, 3, 6),而不是printlnValues(anonfun1, 3, 6).为什么会这样?

scala

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

Data.ByteString和Data.ByteString.Char8之间的区别

我读到Char8仅支持ASCII字符,如果您正在使用其他Unicode字符,则将很危险。

{-# LANGUAGE OverloadedStrings #-}

--import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as BC
import qualified Data.Text.IO as TIO
import qualified Data.Text.Encoding as E
import qualified Data.Text as T

name :: T.Text
name = "{ \"name\": \"???\" }"

nameB :: BC.ByteString
nameB = E.encodeUtf8 name

main :: IO ()
main = do
  BC.writeFile "test.json" nameB
  putStrLn "done"
Run Code Online (Sandbox Code Playgroud)

产生与以下结果相同的结果

{-# LANGUAGE OverloadedStrings #-}

import qualified Data.ByteString as B
--import qualified Data.ByteString.Char8 as BC
import qualified Data.Text.IO as TIO
import qualified …
Run Code Online (Sandbox Code Playgroud)

haskell bytestring

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

Chai-As-Promised即使错了也会通过

const chaiAsPromised = require('chai-as-promised');
const chai = require('chai');
const expect = chai.expect;
chai.use(chaiAsPromised);

describe('[sample unit]', function() {
  it('should pass functionToTest with true input', function() {   
    expect(Promise.resolve({ foo: "bar" })).to.eventually.have.property("meh");
  });
});
Run Code Online (Sandbox Code Playgroud)

这个测试通过??? 我正在使用"chai":"3.5.0","chai-as-promised":"5.2.0",

javascript mocha.js chai chai-as-promised

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

我们如何利用Haskell在执行"2/0"时返回的"无穷大"

我知道Haskell试图做一些比简单地抛出错误更有利的事情

test :: Int -> Int -> String
test a b = case a/b of
    Infinity -> "fool"
    x  -> Show x
Run Code Online (Sandbox Code Playgroud)

但是我被告知我的ghc Infinity不是数据构造函数.实际上是什么,我该如何利用它?我不想简单地检查b0

haskell

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

如何在haskell中实现相同记录类型的不同实现?

我理解为我们需要使用相同类型的不同实现 newtype

data Person = Person {
  name :: String
  , age :: Int
} deriving Show

class Describable a where describe :: a -> String

instance Describable Person where
  describe person = name person ++ " (" ++ show (age person) ++ ")" 

newtype AnotherPerson = AnotherPerson Person
Run Code Online (Sandbox Code Playgroud)

但是,在相同字段名称的记录之间存在名称冲突的haskell问题

instance Describable AnotherPerson where
  describe person = name person ++ " - " ++ show (age person)

<interactive>:79:65: error:
    • Couldn't match expected type ‘Person’
                  with actual type ‘AnotherPerson’
    • …
Run Code Online (Sandbox Code Playgroud)

haskell typeclass

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

将数据拆分为子集并将函数应用于每个子集.(不常见,非常具有挑战性)

我看过普利尔,但我想要达到的目标与平时大不相同

Time               Criteria

17/05/2013 17:22   A
17/05/2013 17:23   A
17/05/2013 17:29   A
17/05/2013 17:22   B
17/05/2013 17:28   B
17/05/2013 17:29   B
25/05/2013 16:56   C
25/05/2013 16:56   C
Run Code Online (Sandbox Code Playgroud)

我想按标准拆分这些数据.然后,对于每个子集,迭代记录并决定是否保留该记录,如果每个记录距离最后一个记录少于5分钟.

期望的结果:

Time               Criteria  Keep

17/05/2013 17:22   A         T
17/05/2013 17:23   A         T
17/05/2013 17:29   A         F --> 29 is more than 5 mins from 23
17/05/2013 17:22   B         F --> Not keeping this because it is >5min from next record
17/05/2013 17:28   B         T 
17/05/2013 17:29   B         T
25/05/2013 16:56   C         T …
Run Code Online (Sandbox Code Playgroud)

r

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

为什么在这种情况下不能简单地加入工作?

我正在学习并发性并编写了一些代码来证明Scala中的交错.但是,即使使用join语句,count仍保持为0.谁能告诉我我在这里缺少什么?

object Main extends App {
  new Worker().doWork()
}

class Worker {
  private var count = 0
  def doWork() = {
    val t1 = new Thread{new Runnable {
      override def run(): Unit = {
        (0 to 10000).foreach {_ => count = count + 1}
      }
    }}
    val t2 = new Thread{new Runnable {
      override def run(): Unit = {
        (0 to 10000).foreach {_ => count = count + 1}
      }
    }}
    t1.start()
    t2.start()

    t1.join()
    t2.join()
    println(s"Thread: ${Thread.currentThread()} - $count")
  } …
Run Code Online (Sandbox Code Playgroud)

concurrency scala

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