我有一个279MB的文件,包含大约1000万个键/值对,大约有500,000个唯一键.它按键分组(每个键只需要写一次),因此给定键的所有值都在一起.
我想要做的是转换关联,创建一个文件,其中对按值分组,给定值的所有键都存储在一起.
目前,我正在使用Parsec作为元组列表读取对(K,[V])(使用惰性IO,因此我可以在Parsec处理输入文件时将其作为流处理),其中:
newtype K = K Text deriving (Show, Eq, Ord, Hashable)
newtype V = V Text deriving (Show, Eq, Ord, Hashable)
tupleParser :: Parser (K,[V])
tupleParser = ...
data ErrList e a = Cons a (ErrList e a) | End | Err e
parseAllFromFile :: Parser a -> FilePath-> IO (ErrList ParseError a)
parseAllFromFile parser inputFile = do
contents <- readFile inputFile
let Right initialState = parse getParserState inputFile contents
return $ loop initialState
where loop …Run Code Online (Sandbox Code Playgroud) 我想监视OSX盒子上的屏幕保护程序和锁屏事件.作为第一关,我很好,他们只是打印到控制台.
以下的其他人的问题的建议,我写了一些目标C监听可可通知的
com.apple.screensaver.didstart,com.apple.screensaver.didstop,com.apple.screenIsLocked,和com.apple.screenIsUnlocked事件.
// ScreenSaverMonitor.h
#import <Foundation/NSObject.h>
#import <Foundation/NSNotification.h>
@interface ScreenSaverMonitor: NSObject {}
-(id) init;
-(void) receive: (NSNotification*) notification;
@end
// ScreenSaverMonitor.m
#import "ScreenSaverMonitor.h"
#import <Foundation/NSString.h>
#import <Foundation/NSDistributedNotificationCenter.h>
#import <Foundation/NSRunLoop.h>
#import <stdio.h>
@implementation ScreenSaverMonitor
-(id) init {
NSDistributedNotificationCenter * center
= [NSDistributedNotificationCenter defaultCenter];
[center addObserver: self
selector: @selector(receive:)
name: @"com.apple.screensaver.didstart"
object: nil
];
[center addObserver: self
selector: @selector(receive:)
name: @"com.apple.screensaver.didstop"
object: nil
];
[center addObserver: self
selector: @selector(receive:)
name: @"com.apple.screenIsLocked"
object: …Run Code Online (Sandbox Code Playgroud) 我正在尝试按照我的建议回答这个stackoverflow问题,uniplate但到目前为止我提出的唯一解决方案非常难看.
这似乎是一个相当普遍的问题,所以我想知道是否有更优雅的解决方案.
基本上,我们有一个GADT可以解析为Expression Int或者Expression Bool(忽略codataIf = If (B True) codataIf codataIf):
data Expression a where
I :: Int -> Expression Int
B :: Bool -> Expression Bool
Add :: Expression Int -> Expression Int -> Expression Int
Mul :: Expression Int -> Expression Int -> Expression Int
Eq :: Expression Int -> Expression Int -> Expression Bool
And :: Expression Bool -> Expression Bool -> …Run Code Online (Sandbox Code Playgroud) 为了帮助自学C++,我正在开发一个红黑树实现.来自Haskell,我认为看看我是否可以 在C++的类型系统中静态强制执行红黑树的属性是很有趣的:
- 节点为红色或黑色.
- 根是黑色的[...]
- 所有叶子(NIL)都是黑色的.
- 如果节点为红色,则其子节点均为黑色.
- 从给定节点到其任何后代NIL节点的每条路径都包含相同数量的黑色节点.[...]
我想出了如何使用模板为树的节点制作类型以满足约束1,3,4和5:
template <typename Key, typename Value>
class RedBlackTree {
private:
enum class color { Black, Red };
// [1. A node is either red or black]
template <color Color, size_t Height>
struct Node {};
// [3. All leaves are black]
struct Leaf : public Node<color::Black, 0> {};
template <color Left, color Right, size_t ChildHeight>
struct Branch {
public:
template <color ChildColor>
using Child = unique_ptr<Node<ChildColor, ChildHeight>>;
Key key;
Value …Run Code Online (Sandbox Code Playgroud) 开始使用conduit,我注意到在Data.Conduit.Util中:
实用程序功能来自旧版本的管道.这些应该被视为已弃用,因为现在有更简单的方法来处理它们的用例.提供此模块仅用于向后兼容.
我特别关注的是zip :: Monad m => Source m a -> Source m b -> Source m (a, b).这对我来说似乎非常有用 - 我可以独立地研究生成as的方法和生成bs的方法,然后zip在需要时将它们放在一起,而不是在过程的早期混合关注.
但是,就像我说的那样,我是管道的新手,所以我无知.这些"更简单的方法来处理他们的用例"是什么?
我已经定义了无限列表的无限列表pathCounts和有限列表的无限列表pathCounts':
import Data.Function (fix)
nextRow xs = fix $ \ys -> zipWith (+) xs (0:ys)
pathCounts = repeat 1 : map nextRow pathCounts
pathCounts' = map (take 100) pathCounts
Run Code Online (Sandbox Code Playgroud)
放入ghci,如果我根本没有评估,我可以:p成功使用:
ghci> :p pathCounts
pathCounts = (_t1::[[Integer]])
ghci> :p pathCounts'
pathCounts' = (_t2::[[Integer]])
Run Code Online (Sandbox Code Playgroud)
但如果我pathCounts'部分评估,那么:p在pathCounts继续成功的同时冻结pathCounts':
ghci> head . head $ pathCounts'
1
ghci> :p pathCounts'
pathCounts' = (1 : (_t4::[Integer])) : (_t5::[[Integer]])
ghci> :p pathCounts
^CInterrupted.
Run Code Online (Sandbox Code Playgroud)
我希望:p …
我试图理解使用forall量化两个类型变量和使用forall量化元组类型的单个类型变量之间的区别.
例如,给定以下类型系列:
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE DataKinds #-}
type family Fst (p :: (a,b)) :: a where
Fst '(a,_) = a
type family Snd (p :: (a,b)) :: b where
Snd '(_,b) = b
type family Pair (p :: (Type,Type)) :: Type where
Pair '(a,b) = (a,b)
Run Code Online (Sandbox Code Playgroud)
我可以使用两个类型变量在对上定义一个标识,并让它在GHC 8.0.1上编译:
ex0 :: forall (a :: Type) (b :: Type). Pair '(a,b) -> (Fst '(a,b), Snd '(a,b))
ex0 …Run Code Online (Sandbox Code Playgroud) 有没有办法可以从JRuby显式地将一个Java对象转换为另一个Java类?
有时我希望能够调用SomeJavaClass#aMethod(MySuperClass)而不是SomeJavaClass#aMethod(MyClass)从JRuby 调用.
从Java开始,我会这样做:
someJavaObject.aMethod( (MySuperClass) myObj );
Run Code Online (Sandbox Code Playgroud)
但我没有看到一个#castruby方法或类似的东西来做JRuby中的等效方法.
请注意,从JRuby转换Java对象的问题缺乏对一般情况的答案,这就是我重新提出问题的原因.
我开始阅读关于CRDT的这篇论文,这是一种通过确保修改数据的操作是可交换的,同时共享可修改数据的方法.在我看来,这将是Haskell中抽象的一个很好的候选者 - 为CRDT提供一个类型类,指定数据类型和在该类型上通信的操作,然后使库实际上在并发进程之间共享更新.
我无法想象的是如何表达操作必须在类型类规范中通勤的合同.
举个简单的例子:
class Direction a where
turnLeft :: a -> a
turnRight :: a -> a
Run Code Online (Sandbox Code Playgroud)
不能保证和它turnLeft . turnRight一样turnRight . turnLeft.我认为后备是指定monad定律的等价物 - 使用注释来指定类型系统不强制执行的约束.