小编Dul*_*gon的帖子

草案js.将EditorContent持久化到数据库

我想坚持draft-jsEditorContent数据库,然后再次读取并重新创建EditorContent对象.但EditorContent.getPlainText()剥去了丰富的文字内容.我不知道怎么做.

我该如何正确坚持EditorContent

javascript reactjs draftjs

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

如何正确实现ARC兼容和`alloc init`安全的Singleton类?

我看到了线程安全的版本

+(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方法相同的实例?

singleton objective-c ios automatic-ref-counting

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

Option<Box<T>> 与 Box<Option<T>> 的内存布局。哪一个更好?

假设我有这两个结构

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)

内存布局有什么区别?哪一个更好?

rust

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

简单的TCP客户端

我正在尝试在Haskell中实现简单的TCP客户端.但它一旦连接就会关闭.我不知道是什么导致它关闭.我怎么能这样做,以便从服务器打印线路stdoutstdin永久地从服务器发送线路直到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)

haskell haskell-platform

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

循环编程 - 用最小值替换列表元素

我刚读这篇文章有关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].什么是正确的版本?

haskell

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

Cabal配置,用于编译器标志和运行时选项

我正在尝试获得一个等价的配置,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

haskell cabal

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

如何解决"缺少关联类型`Err`值"错误?

我正在尝试创建一个简单的实用程序函数,从中读取多个元素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)

rust

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

如果模式与`Maybe a`结果匹配

以下代码说明了我的意图.我想模式匹配,如果没有结果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)

haskell haskell-lens

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

无法推断 filter_map().sum() 的“B”类型

下面的代码读取数字,对它们求和,然后打印总和。我尝试了一些注释,但没有成功。我肯定错过了什么。我怎样才能让它发挥作用?

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)

parsing types type-inference rust

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

与点免费样式代码混淆

因此,复制列表元素给定次数的函数看起来像这样

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 pointfree

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