我想坚持draft-js的EditorContent数据库,然后再次读取并重新创建EditorContent对象.但EditorContent.getPlainText()剥去了丰富的文字内容.我不知道怎么做.
我该如何正确坚持EditorContent?
我看到了线程安全的版本
+(MyClass *)singleton {
static dispatch_once_t pred;
static MyClass *shared = nil;
dispatch_once(&pred, ^{
shared = [[MyClass alloc] init];
});
return shared;
}
Run Code Online (Sandbox Code Playgroud)
但如果有人打电话[MyClass alloc] init]会怎么样?如何让它返回与+(MyClass *)singleton方法相同的实例?
假设我有这两个结构
struct Node<T> {
value: T,
next: Box<Option<Node<T>>>
}
struct Node2<T> {
value: T,
next: Option<Box<Node2<T>>>
}
Run Code Online (Sandbox Code Playgroud)
内存布局有什么区别?哪一个更好?
我正在尝试在Haskell中实现简单的TCP客户端.但它一旦连接就会关闭.我不知道是什么导致它关闭.我怎么能这样做,以便从服务器打印线路stdout并stdin永久地从服务器发送线路直到stdin收到线路":退出"?
import Control.Monad (forever)
import Network (withSocketsDo, PortID(..), connectTo)
import System.IO
import Control.Concurrent (forkFinally)
import Control.Concurrent.Async (race)
main :: IO ()
main = withSocketsDo $ do
-- connect to my local tcp server
handle <- connectTo "192.168.137.1" (PortNumber 44444)
-- should close the connection using handle after everything is done
_ <- forkFinally (talk handle) (\_ -> hClose handle)
return ()
talk :: Handle -> IO ()
talk handle = do
hSetNewlineMode handle universalNewlineMode …Run Code Online (Sandbox Code Playgroud) 我刚读这篇文章有关circular programming.这对我来说似乎很陌生.虽然我可以想象反馈是懒惰的评估thunk,将在以后评估到期望的结果,但是无法绕过它.所以我决定编写一个函数,用它的最小值替换列表中的每个元素.
trace :: (a -> c -> (b,c)) -> a -> b
trace f a = b
where (b,c) = f a c
repminList :: (Num a, Ord a) => [a] -> [a]
repminList = trace repIIminList
repIIminList [x] m = ([m], x)
repIIminList (a:as) m = let (replaced, m) = repIIminList as m
in (m : replaced, min a m)
Run Code Online (Sandbox Code Playgroud)
但repminList [1,2,3]等于[2,3,3].什么是正确的版本?
我正在尝试获得一个等价的配置,ghc -threaded -O2然后与一起运行my.exe +RTS -N4 -s。目前我有
executable my.exe
ghc-options:
-O3
-threaded
-rtsopts
-with-rtsopts="-N4"
main-is: Main.hs
Run Code Online (Sandbox Code Playgroud)
当我跑步时,my.exe它给了我unexpected RTS argument: -N4
我正在尝试创建一个简单的实用程序函数,从中读取多个元素stdin并将它们放入集合中并返回它.但是我在这一点上陷入困境.编译说missing associated type Err value.如何使其工作,同时尽可能保持通用?
虽然这个功能似乎没用,但它用于学习语言及其类型系统.
use std::io::{ stdin };
use std::str::FromStr;
use std::io::Read;
use std::iter::FromIterator;
pub fn read_all<C>() -> C
where C: FromIterator<FromStr<Err>>
{
let mut buff = String::new();
stdin().read_to_string(&mut buff).expect("read_to_string error");
buff.split_whitespace()
.filter_map(|w| w.parse().ok())
.collect()
}
Run Code Online (Sandbox Code Playgroud)
用法示例:
let v: Vec<i32> = read_all();
Run Code Online (Sandbox Code Playgroud) 以下代码说明了我的意图.我想模式匹配,如果没有结果Nothing,如果匹配结果是Just something
data MyData =
A Int
| B String
| C
ifA (A i) = Just i
ifA _ = Nothing
ifB (B str) = Just str
ifB _ = Nothing
ifC C = Just ()
ifC _ = Nothing
mbMult3 i = Just (i*3)
concWorld str = Just (str ++ "World")
example1 v = ifA v >>= mbMult3
example2 v = ifB v >>= concWorld
-- example2 (B "Hello ,") == Just "Hello, World"
-- …Run Code Online (Sandbox Code Playgroud) 下面的代码读取数字,对它们求和,然后打印总和。我尝试了一些注释,但没有成功。我肯定错过了什么。我怎样才能让它发挥作用?
use std::io;
use std::io::Read;
fn main() {
let mut buff = String::new();
io::stdin().read_to_string(&mut buff).expect("read_to_string error");
let v: i32 = buff
.split_whitespace()
.filter_map(|w| w.parse().ok())
.sum();
println!("{:?}", v);
}
Run Code Online (Sandbox Code Playgroud)
来自编译器的错误消息:
use std::io;
use std::io::Read;
fn main() {
let mut buff = String::new();
io::stdin().read_to_string(&mut buff).expect("read_to_string error");
let v: i32 = buff
.split_whitespace()
.filter_map(|w| w.parse().ok())
.sum();
println!("{:?}", v);
}
Run Code Online (Sandbox Code Playgroud) 因此,复制列表元素给定次数的函数看起来像这样
rep :: Int -> [a] -> [a]
rep = concatMap . replicate
Run Code Online (Sandbox Code Playgroud)
按照定义(f . g) x = f(g(x))但是
(concatMap (replicate 4 "abc"))是不一样的(concatMap . replicate) 4 "abc".事实上它根本不起作用.而concatMap的第一个参数必须是函数.我对此感到困惑.那点免费版甚至可以工作吗?任何人都可以解释一下.
haskell ×5
rust ×3
cabal ×1
draftjs ×1
haskell-lens ×1
ios ×1
javascript ×1
objective-c ×1
parsing ×1
pointfree ×1
reactjs ×1
singleton ×1
types ×1