小编lha*_*ahn的帖子

在File.write上接收:badarg

我开始学习Elixir,这也是我的第一个动态语言,所以我真的失去了没有类型声明的函数.

我想做什么:
def create_training_data(file_path, indices_path, result_path) do
    file_path
    |> File.stream!
    |> Stream.with_index
    |> filter_data_with_indices(indices_path)
    |> create_output_file(result_path)
  end

  def filter_data_with_indices(raw_data, indices_path) do
    Stream.filter raw_data, fn {_elem, index} ->
      index_match?(index, indices_path)
    end
  end

  defp index_match?(index, indices_path) do
    indices_path
    |> File.stream!
    |> Enum.any? fn elem ->
      (elem
      |> String.replace(~r/\n/, "")
      |> String.to_integer
      |> (&(&1 == index)).())
    end
  end

  defp create_output_file(data, path) do
    File.write(path, data)
  end
Run Code Online (Sandbox Code Playgroud)

当我调用该函数时:

create_training_data("./resources/data/usps.csv","./resources/indices/17.csv","./output.txt")
Run Code Online (Sandbox Code Playgroud)

它返回{:error,:badarg}.我已经检查过,错误发生在create_output_file函数上.

如果我注释掉函数create_output_file,我得到的是一个流(有点意义).问题可能是我不能给Stream to File.write吗?如果是问题,我该怎么办?我在文档上没有找到任何相关内容.

编辑

所以,问题是File.write的路径应该没问题,我修改了这个函数:

defp create_output_file(data, path) do
    IO.puts("You are trying to write to: …
Run Code Online (Sandbox Code Playgroud)

elixir

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

无法使用Webpack进行引导工作

我正在使用a webpack-dev-server,我正在尝试包含bootstrap.

我有这个项目结构:

?? css
?   ??? bootstrap.min.css
??? js
|   ??? bootstrap.min.js
??? dist
??? index.html
??? package.json
??? server.js
??? src
?   ??? actions.js
?   ??? App.js
?   ??? components
?   ??? constants
?   ??? index.js
?   ??? reducers.js
??? webpack.config.js
Run Code Online (Sandbox Code Playgroud)

这是index.html:

<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" href="css/bootstrap.min.css">
  </head>
  <body>
    <div id='root'></div>
    <script src="/static/bundle.js"></script>
  </body>    
</html>
Run Code Online (Sandbox Code Playgroud)

每当我运行服务器时,我都会收到类型错误:

Cannot GET /css/bootstrap.min.css
Run Code Online (Sandbox Code Playgroud)

这是webpack.config.js:

var path = require('path');
var webpack = require('webpack');

module.exports = {
  devtool: 'eval', …
Run Code Online (Sandbox Code Playgroud)

javascript twitter-bootstrap webpack webpack-dev-server webpack-style-loader

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

加载共享库时出错(glew)

我编译了库GLEW.它似乎工作正常,这是make install的输出:

install -d -m 0755 "/usr/include/GL"
install -m 0644 include/GL/wglew.h "/usr/include/GL/"
install -m 0644 include/GL/glew.h "/usr/include/GL/"
install -m 0644 include/GL/glxew.h "/usr/include/GL/"
sed \
        -e "s|@prefix@|/usr|g" \
        -e "s|@libdir@|/usr/lib64|g" \
        -e "s|@exec_prefix@|/usr/bin|g" \
        -e "s|@includedir@|/usr/include/GL|g" \
        -e "s|@version@|1.11.0|g" \
        -e "s|@cflags@||g" \
        -e "s|@libname@|GLEW|g" \
        -e "s|@requireslib@|glu|g" \
        < glew.pc.in > glew.pc
install -d -m 0755 "/usr/lib64"
install -m 0644 lib/libGLEW.so.1.11.0 "/usr/lib64/"
ln -sf libGLEW.so.1.11.0 "/usr/lib64/libGLEW.so.1.11"
ln -sf libGLEW.so.1.11.0 "/usr/lib64/libGLEW.so"
install -m 0644 lib/libGLEW.a "/usr/lib64/"
install -d -m 0755 "/usr/lib64" …
Run Code Online (Sandbox Code Playgroud)

linux glew makefile cmake shared-libraries

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

无法摆脱借来的内容和Builder模式

我只是在学习Rust.我正在尝试为我的Game结构创建一个构建器结构.这是代码:

struct Input {
    keys_pressed: HashMap<VirtualKeyCode, bool>,
}

pub struct GameBuilder {
    settings: GameSettings,
    input: Input,
}

impl GameBuilder {
    pub fn new() -> GameBuilder {
        GameBuilder {
            settings: GameSettings {
                window_dimensions: (800, 600),
                title: "".to_string(),
            },
            input: Input {
                keys_pressed: HashMap::new(),
            }
        }
    }

    pub fn with_dimensions(&mut self, width: u32, height: u32) -> &mut GameBuilder {
        self.settings.window_dimensions = (width, height);
        self
    }

    pub fn with_title(&mut self, title: &str) -> &mut GameBuilder {
        self.settings.title = title.to_string();
        self
    }

    pub fn …
Run Code Online (Sandbox Code Playgroud)

rust borrow-checker

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

二维通用数组初始化

我尝试在java中创建一个二维泛型数组.我没有编译错误,但运行代码时出现异常:

Exception in thread "main" java.lang.ClassCastException: Cannot cast [[[Ljava.lang.String; to [[Ljava.lang.String;
at java.lang.Class.cast(Unknown Source)
at Tabela.<init>(Tabela.java:8)
at TabelaTest.main(TabelaTest.java:4)
Run Code Online (Sandbox Code Playgroud)

这是代码:

import java.lang.reflect.Array;

public class Tabela<T> {

    private T[][] data;

    public Tabela(Class<T[][]> c,int sizeX, int sizeY) {
        this.data = c.cast(Array.newInstance(c.getComponentType(), sizeX, sizeY));
    }

    public void setInfoAt(T info, int x, int y) {
        this.data[x][y] = info;
    }
    public T getObjectAt(int x, int y) {
        return this.data[x][y];
    }
}

public class TabelaTest {

    public static void main(String args[]) {
        Tabela<String> tabela = new Tabela<String>(String[][].class, …
Run Code Online (Sandbox Code Playgroud)

java

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

理解 Writer Monad 的例子

我正在通过 Learn You A Haskell 一书了解 Writer Monad。

这是一段代码:

import Control.Monad.Writer

logNumber :: Int -> Writer [String] Int
logNumber num = writer (num, ["Got number: " ++ show num])

multWithLog :: Writer [String] Int
multWithLog = do
  a <- logNumber 3
  b <- logNumber 5
  return (a * b)
Run Code Online (Sandbox Code Playgroud)

运行时multWithLog,结果如下:

*Main> runWriter multWithLog
(15,["Got number: 3","Got number: 5"])
Run Code Online (Sandbox Code Playgroud)

在这一行:

a <- logNumber 3
b <- logNumber 5
Run Code Online (Sandbox Code Playgroud)

很容易看出a = 3and b = 5,因为它们都在return函数上相乘。 …

monads haskell writer-monad

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

使 QtConcurrent::mapped 与 lambda 一起工作

我正在尝试使用QtConcurrent::mappedQVector<QString>. 我已经尝试了很多方法,但似乎总是存在重载的问题。

\n\n
QVector<QString> words = {"one", "two", "three", "four"};\n\nusing StrDouble = std::pair<QString, double>;\n\nQFuture<StrDouble> result = QtConcurrent::mapped<StrDouble>(words, [](const QString& word) -> StrDouble {\n    return std::make_pair(word + word, 10);\n});\n
Run Code Online (Sandbox Code Playgroud)\n\n

该片段返回以下错误:

\n\n
/home/lhahn/dev/cpp/TestLambdaConcurrent/mainwindow.cpp:23: error: no matching function for call to \xe2\x80\x98mapped(QVector<QString>&, MainWindow::MainWindow(QWidget*)::<lambda(const QString&)>)\xe2\x80\x99\n });\n  ^\n
Run Code Online (Sandbox Code Playgroud)\n\n

我看到这篇文章,它说 Qt 找不到 lambda 的返回值,所以你必须使用std::bind它。如果我尝试这个:

\n\n
using StrDouble = std::pair<QString, double>;\nusing std::placeholders::_1;\n\nauto map_fn = [](const QString& word) -> StrDouble {\n    return std::make_pair(word + word, 10.0);\n};\n\nauto wrapper_map_fn = …
Run Code Online (Sandbox Code Playgroud)

c++ qt c++11

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

无法访问Friend类的私有构造函数

我有以下两个类:

struct Entity 
{
    unsigned id;
    Entity(unsigned id);
    Entity();
};

class EntityManager
{
public:
    Entity create();
    ...

private:
    Entity make_entity(unsigned index, unsigned generation);
};
Run Code Online (Sandbox Code Playgroud)

这个目前工作正常.问题是封装.我不想允许直接创建类Entity.

因此,我的目标是使构造者成为Entity私有的.然后,我可以(从我的理解)十个分量的功能在EntityManager通过使Entity一个friendEntityManager.

因此,进行更改将是结果:

struct Entity 
{
    unsigned id;
private:
    Entity(unsigned id);
    Entity();
};

class EntityManager
{
    friend struct Entity;
public:
    Entity create();

private:
    Entity make_entity(unsigned index, unsigned generation);
};
Run Code Online (Sandbox Code Playgroud)

这打破了代码.我得到的错误就是这个:

entity_manager.cpp: In member function ‘Entity EntityManager::make_entity(unsigned int, unsigned int)’:
entity_manager.cpp:12:1: error: ‘Entity::Entity(unsigned int)’ is …
Run Code Online (Sandbox Code Playgroud)

c++ friend

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

Collections.sort()没有按正确的顺序排序

我在Java中有这个代码:

List<String> unSorted = new ArrayList<String>();
List<String> beforeHash = new ArrayList<String>();
String[] unSortedAux, beforeHashAux; 
String line = null;

BufferedReader reader = new BufferedReader(new FileReader("C:\\CPD\\temp0.txt"));
    while ((line = reader.readLine()) != null){
        unSorted.add(line);  
        beforeHash.add(line.split("#")[0]); 

    }
    reader.close();

    Collections.sort(beforeHash);
    beforeHashAux = beforeHash.toArray(new String[beforeHash.size()]);
    unSortedAux = unSorted.toArray(new String[unSorted.size()]);

    System.out.println(Arrays.toString(beforeHashAux));
    System.out.println(Arrays.toString(unSortedAux));
Run Code Online (Sandbox Code Playgroud)

它读取名为temp0.txt的文件,其中包含:

Carlos Magno#261
Mateus Carl#12
Analise Soares#151
Giancarlo Tobias#150
Run Code Online (Sandbox Code Playgroud)

我的目标是在字符串中对名称进行排序,而不是"#"之后的字符串.我正在使用beforeHash.add(line.split("#")[0]); 去做这个.问题是它正确读取文件,但它按错误的顺序排序.相应的产出是:

[Analise Soares, Giancarlo Tobias, Mateus Carl, Carlos Magno]
[Carlos Magno#261, Mateus Carl#12, Analise Soares#151, Giancarlo Tobias#150]
Run Code Online (Sandbox Code Playgroud)

第一个结果是"排序的",注意"Carlos Magno"来自"Mateus Carl".我在代码中找不到问题.

java sorting collections

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

matplotlib中的直方图未按预期工作

我有两个数组:

import numpy as np
import pylab as pl

x = np.array([-36., -34.95522388, -33.91044776, -32.86567164,
   -31.82089552, -30.7761194 , -29.73134328, -28.68656716,
   -27.64179104, -26.59701493, -25.55223881, -24.50746269,
   -23.46268657, -22.41791045, -21.37313433, -20.32835821,
   -19.28358209, -18.23880597, -17.19402985, -16.14925373,
   -15.10447761, -14.05970149, -13.01492537, -11.97014925,
   -10.92537313,  -9.88059701,  -8.8358209 ,  -7.79104478,
    -6.74626866,  -5.70149254,  -4.65671642,  -3.6119403 ,
    -2.56716418,  -1.52238806,  -0.47761194,   0.56716418,
     1.6119403 ,   2.65671642,   3.70149254,   4.74626866,
     5.79104478,   6.8358209 ,   7.88059701,   8.92537313,
     9.97014925,  11.01492537,  12.05970149,  13.10447761,
    14.14925373,  15.19402985,  16.23880597,  17.28358209,
    18.32835821,  19.37313433,  20.41791045,  21.46268657,
    22.50746269,  23.55223881,  24.59701493,  25.64179104,
    26.68656716,  27.73134328,  28.7761194 , …
Run Code Online (Sandbox Code Playgroud)

python matplotlib histogram

0
推荐指数
2
解决办法
2090
查看次数