小编pro*_*eek的帖子

处理Linq XML字符串

我需要用Linq解析XML字符串,然后我想出了以下代码.

using System;
using System.Linq;
using System.Xml.Linq;

class LinqXml 
{
    public void Parse(string input)
    {
    XDocument xdoc = XDocument.Load(input);
    var lang = from d in xdoc.Elements("PipeUnit").Elements("Step").Elements("Pipelist").Elements("NamedPipe").Elements("NameOfPipe") select d;

    Console.WriteLine(lang.First().Value);

    foreach (var item in lang) 
    {
        Console.WriteLine(item.Value);
    }
    }

    static void Main()
    {

        string tempString = @"
<PipeUnit>
  <Step>
    <Pipelist>
      <NamedPipe>
        <NameOfPipe>Name</NameOfPipe>
        <PipeData>Data</PipeData>
      </NamedPipe>
    </Pipelist>
  </Step>
</PipeUnit>        
        ";
        var linqXml = new LinqXml();
        linqXml.Parse(tempString);
    }
}
Run Code Online (Sandbox Code Playgroud)

使用Mono编译此代码时dmcs linqxml.cs /r:System.Xml.Linq.dll,并尝试运行,我收到以下错误.

Unhandled Exception: System.IO.DirectoryNotFoundException: Could not find a part of the …
Run Code Online (Sandbox Code Playgroud)

c# linq mono

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

错误CS1501:方法"反向"没有重载需要"0"参数

我有这个代码来尊重句子中的单词.

using System;

class Code 
{
    public static void Main()
    {
        string x = "I am Sam";

        foreach(var a in x.Split().Reverse()) 
        {
            Console.WriteLine(a);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

当我编译这段代码(单声道2.10)时,我得到了error CS1501: No overload for method反向'接收0' arguments错误消息.

代码有什么问题?

c# string mono reverse

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

在 C# 方法中使用时 OfType&lt;????&gt; 的参数

我有这个代码来获得“A”作为过滤结果。

public static void RunSnippet()
{
    Base xbase = new Base(); 
    A a = new A(); 
    B b = new B();
    IEnumerable<Base> list = new List<Base>() { xbase, a, b };
    Base f = list.OfType<A>().FirstOrDefault();
    Console.WriteLine(f);
}
Run Code Online (Sandbox Code Playgroud)

我需要使用IEnumerable<Base> list = new List<Base>() {xbase, a, b};一个函数,如下所示:

public static Base Method(IEnumerable<Base> list, Base b (????)) // I'm not sure I need Base b parameter for this?
{
    Base f = list.OfType<????>().FirstOrDefault();
    return f;
}

public static void RunSnippet()
{
    Base …
Run Code Online (Sandbox Code Playgroud)

c# linq oftype

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

使用Python广播和接收数据

我正在尝试播放一些数据并使用python接收它.这是我提出的代码.

from socket import *
import threading

class PingerThread (threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run (self):
        print 'start thread'
        cs = socket(AF_INET, SOCK_DGRAM)
        cs.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
        cs.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
        cs.sendto('This is a test', ('192.168.65.255', 4499))

a = PingerThread() 
a.start()

cs = socket(AF_INET, SOCK_DGRAM)
data = cs.recvfrom(1024) # <-- waiting forever
Run Code Online (Sandbox Code Playgroud)

但是,代码似乎永远等待着cs.recvfrom(1024).可能有什么问题?

python networking udp

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

C++构造函数中的不同内存地址

我在C++中有两个构造函数,一个构造函数调用另一个构造函数以便不复制初始化逻辑.

#include <iostream>
#include <memory>

using namespace std;

class A
{
    int x;
    int y;
public: 
    A(int x)
    {
        cout << this << endl;
        this->x = x;
    }

    A()
    {
        cout << this << endl;
        A(20);
    }

    ...
};
Run Code Online (Sandbox Code Playgroud)

有趣的是A()调用A(int),但是this指针指向不同的地址.为什么是这样?或者是这个g ++错误?

int main(int argc, char *argv[]) {
    A* a = new A();
}

0x7fa8dbc009d0 <-- from A()
0x7fff67d660d0 <-- from A(int)
Run Code Online (Sandbox Code Playgroud)

constructor g++

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

Clojure的 - >和 - >>宏

Clojure的->>宏线程从最后一个参数的形式,从第一个->形式开始.

user=> (->> a (+ 5) (let [a 5]))
10
Run Code Online (Sandbox Code Playgroud)

但是,当我使用交换的操作时,我得到一个例外.

user=> (-> a (let [a 5]) (+ 5))

CompilerException java.lang.IllegalArgumentException: let requires a vector for its binding in user:1, compiling:(NO_SOURCE_PATH:1:7) 
Run Code Online (Sandbox Code Playgroud)

此外,我希望这两个操作能得到相同的结果,但事实并非如此.

user=> (-> 0 (Math/cos) (Math/sin))
0.8414709848078965
user=> (->> 0 (Math/sin) (Math/cos))
1.0
Run Code Online (Sandbox Code Playgroud)

怎么了?怎么->->>宏一起工作?

macros clojure operators

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

使用Scala检查列表中的值

我有一个check(a, b, c)方法来检查a是否在b和之间c (a >= b && a <= c).例如,对于给定的列表,v = List(1,2)r = List((3,4),(5,6)); 我想使用check方法检查所有值r是否在r的范围内:check(1, 3, 4) && check (2, 5, 6).

我有如下高级解决方案,但我有一些缺少的部分.

val x = v zip r // (Int, (Int, Int)) 
val y = ???     // (Int, (Int, Int)) => (Int, Int, Int)
(y map check).forall {_ == true} // error 
Run Code Online (Sandbox Code Playgroud)

我怎样才能得到解决方案?

scala

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

使用list comprehension在Haskell中创建一个新类型列表

我正在尝试创建一个函数,它返回我从两个列表中定义的assoc类型列表.我考虑使用列表理解,但我不确定如何从每个列表中获取值.

type Assoc k v = [(k, v)]

makeAssoc :: [k] -> [v] -> [Assoc k v]
makeAssoc k v =
   [Assoc k' v' | k' <- k, ???]

main :: IO ()
main = do
    putStrLn $ show makeAssoc [1,2,3] [4,5,6]
Run Code Online (Sandbox Code Playgroud)

如何实施makeAssoc k v

haskell list-comprehension

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

在bash中没有正确调用函数

我有这个bash代码,检查操作系统是Linux还是Mac,我使用函数isWhat从其他函数调用.

function isWhat 
{
  if [ `uname` == $1 ];
  then
    return 1
  else
    return 0  
  fi
}

function isLinux 
{
    return isWhat("Linux")
}

function isMac
{
    return isWhat("Darwin")
}
Run Code Online (Sandbox Code Playgroud)

但是,我收到了这些错误:

/functions.sh: line 13: syntax error near unexpected token `('
/functions.sh: line 13: `    return isWhat("Linux")'
runme.sh: line 7: isMac: command not found
Run Code Online (Sandbox Code Playgroud)

可能有什么问题?

bash

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

Haskell中(M a >> = f)>> = g和M a >> =(f >> = g)的等价性

我有两个相同的Haskell函数.

triple :: Int -> Int
triple = do
  n <- id
  d <- (n+)
  (d+)

triple2 :: Int -> Int  
triple2 = (id >>= (\n -> (n+))) >>= (\d -> (d+))    
Run Code Online (Sandbox Code Playgroud)

签名>>=M a -> (a -> M b) -> M b,因此括号用于强调关系船((Ma >>= f) >>= f2)M b >> f2.

但是,这个三重3也与三重或三重相同.

triple3 :: Int -> Int  
triple3 = id >>= (\n -> (n+) >>= (\d -> (d+)))
Run Code Online (Sandbox Code Playgroud)

这些等价背后的逻辑是什么?

monads haskell

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