为什么 GHC 不会派生Applicative为KO?
#!/usr/bin/env stack
-- stack --resolver lts-17.10 script
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
newtype KO a = KO a deriving (Functor, Applicative) -- doesn't derive Applicative
newtype OK f a = OK (f a) deriving (Functor, Applicative) -- that's ok
main :: IO ()
main = print "hello"
Run Code Online (Sandbox Code Playgroud)
• 无法创建“Applicative KO”的派生实例(即使使用狡猾的 GeneralizedNewtypeDeriving):不能充分减少表示类型 • 在“KO”typecheck 的 newtype 声明中
我想联想_与coerce但我不能给它一个类型签名。
有什么技巧可以解决这个问题吗?
import Data.Coerce
ok :: ()
ok =
let a = _ "hi"
in let a :: String = __ "Hi"
in ()
where
_ = undefined
__ :: Coercible a b => a -> b
__ = coerce
ko =
let a = _ "hi"
in let a :: String = __ "Hi"
in ()
where
__ = undefined
_ :: Coercible a b => a -> b. -- Invalid type signature: _ :: …Run Code Online (Sandbox Code Playgroud) 运行以下代码时
open System
open Microsoft.FSharp.Control
type Message(id, contents) =
static let mutable count = 0
member this.ID = id
member this.Contents = contents
static member CreateMessage(contents) =
count <- count + 1
Message(count, contents)
let mailbox = new MailboxProcessor<Message>(fun inbox ->
let rec loop count =
async { printfn "Message count = %d. Waiting for next message." count
let! msg = inbox.Receive()
printfn "Message received. ID: %d Contents: %s" msg.ID msg.Contents
return! loop( count + 1) }
loop 0)
mailbox.Start() …Run Code Online (Sandbox Code Playgroud) 你知道如何在下面初始化变量ret吗?
type ReferenceDataResponse =
{ ResponseError : ResponseError option
SecurityDatas : SecurityData array option }
let ToReferenceDataResponse(elem:Bloomberglp.Blpapi.Element) =
let ret = { ResponseError = null ; SecurityDatas = null }
if elem.HasElement("ResponseError") then
...
end
Run Code Online (Sandbox Code Playgroud)
ps:我想我已经以某种方式声明我的记录值是可变的
更新:
正如评论中所提到的,我之前在我的代码中使用过None:
type ZeroOrMany<'a> =
| Many of 'a array
| None
Run Code Online (Sandbox Code Playgroud)
这为符号'None'创建了一个新的定义,它隐藏了我试图在这里引用的"Option.None".
我有一个副作用
securities |> Seq.map (fun x -> request.Append("securities",x))
Run Code Online (Sandbox Code Playgroud)
使代码执行的最惯用的方法是什么?
我写了一个Seq.Doit,但它发痒
module Seq =
let Doit sa = sa |> Seq.toArray |> ignore
Run Code Online (Sandbox Code Playgroud) 我想根据提供的字段制作一个简单的记录类型.
那是 :
let rectype = MakeRecordType(['fieldname1'; 'fieldname2'])
Run Code Online (Sandbox Code Playgroud)
直接进入类型提供者看起来像是一个如此简单的任务的重枪手.
还有其他方法吗?
更新
我发现以下问题看起来非常相似 通过反射创建F#记录
我很困惑为什么第三个函数不起作用:
let generate1 = id
let generate2 = let a = 1
id
let generate3 = printfn "hi"
id
Run Code Online (Sandbox Code Playgroud)
而前两个很好,最后一个吐出来
error FS0030: Value restriction. The value 'generate3' has been inferred to have generic type
val generate3 : ('_a -> '_a)
Either make the arguments to 'generate3' explicit or, if you do not intend for it to be generic, add a type annotation.
Run Code Online (Sandbox Code Playgroud) 我想将一堆fs文件转换为fsx文件.
每个fs文件引用类都定义在base.fs中,因此不是在项目中编译并依赖于编译器解析,所有都是基于文件的.
这意味着如果我将所有这些文件包含在base.fsx中,并且该文件引用另一个文件,则base.fsx将包含两次.
有谁知道如何使用fsx文件进行条件包含?
该预处理器文档状态
F#中没有#define预处理器指令.必须使用编译器选项或项目设置来定义#if指令使用的符号.
我有一个小的data.table和使用transform它需要永远.这是一个可重复的例子:
library(data.table)
#data.table 1.8.8
set.seed(1)
dataraw <- data.table(sig1 = runif(80000, 0, 9999),
sig2 = runif(80000, 0, 9999),
sig3 = runif(80000, 0, 9999))
system.time(transform(dataraw, d = 1))
# user system elapsed
#16.345 0.016 16.359
dataraw2 <- as.data.frame(dataraw)
system.time(transform(dataraw2, d = 1))
# user system elapsed
#0.002 0.002 0.005
Run Code Online (Sandbox Code Playgroud)
transform与data.frame一起使用时,为什么data.table的速度如此之慢?
这可能是一个愚蠢的问题,当然不是最好的代码,但我不太明白为什么如果我添加一些打字信息haskell对我大吼大叫
这不起作用(注意x:第3行):
groupBy3 :: (a -> a -> Bool)-> [a] -> [[a]]
groupBy3 eq = foldr step []
where step (x:a) res =
let (used, res2) = foldr insertel (False, []) res
where insertel (al) (used, acc) =
if used then (True, al:acc)
else if eq x (head al) then
(True, (x:al):acc)
else
(False, al:acc)
in
if used then
res2
else [x]:res
Run Code Online (Sandbox Code Playgroud)
这是有效的(注意第3行的x缺少类型注释)
groupBy3 :: (a -> a -> Bool)-> [a] -> [[a]]
groupBy3 eq = foldr step []
where …Run Code Online (Sandbox Code Playgroud) f# ×6
haskell ×3
arrays ×1
asynchronous ×1
data.table ×1
deferred ×1
mutable ×1
option ×1
performance ×1
r ×1
sequences ×1
types ×1