AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / user-11930602

kesarling He-Him's questions

Martin Hope
kesarling
Asked: 2025-04-28 09:14:20 +0800 CST

如何将 Sender 对象从可变引用移到字符串和 Sender 对象元组的向量?

  • 4

这并不能解决我的问题。建议的答案是用Nones 替换值。我确实需要减小向量的大小。

口粮:

use tokio::sync::mpsc;

#[tokio::main]
async fn main() {

    // Pits the singers against each other...
    // Please do not get confused by the type of the sender used in here.
    // This still will showcase the same problem.
    fn pit(singers: &mut Vec<(String, mpsc::Sender<String>)>) {
        use rand::Rng;
        let mut rng = rand::thread_rng();
        let i1 = rng.gen_range(0..singers.len());
        let i2 = rng.gen_range(0..singers.len());
        // This will add dunsel "singers" to the vector who will get
        // pitted against one another. The actual code makes sure that
        // there are at least 2 singers in queue. What is does not do - 
        // and neither should it - is check if they are dummy objects.
        // Rust did not like me using singers[i1].take().unwrap() and
        // complained about iterators not being implemented for Sender?
        let (dunsel, _) = mpsc::channel::<String>(1);
        let s1 = std::mem::replace(&mut singers[i1], (String::new(), dunsel.clone()));
        let s2 = std::mem::replace(&mut singers[i2], (String::new(), dunsel.clone()));
        // returns s1, s2... not needed for MRE
    }

    let mut v = vec![];
    let (tx, _) = mpsc::channel::<String>(256); // receiver is not needed for MRE
    for name in vec!["name1".to_string(), "name2".to_string(), "name3".to_string()] {
        // In the actual code, the singer clients are sent this tx
        // and they send their transmitter channels via this tx, which
        // is then pushed to the vector. Here, it just accepts `String`s
        // So, tx is of the type mpsc::Sender<mpsc::Sender<String>>!!
        v.push((name.clone(), tx.clone()));
    }
    tokio::spawn(async move {
        let mut v = v;
        pit(&mut v); 
    });
}
rust
  • 1 个回答
  • 52 Views
Martin Hope
kesarling
Asked: 2025-02-11 05:00:42 +0800 CST

我如何向 rust 编译器保证某个变量将在 for 循环中被初始化?

  • 6

这个问题与这里提出的问题有关:如何声明一个变量但不分配它?

我的问题略有不同。MRE:

let var: MyGenericType;
for value in vec_of_values_of_my_generic_type {
    // one of the values is mathematically guaranteed to meet this condition because the vector is special
    if <value meets a certain condition> {
        var = value;
    }
}

<use var>

错误:

error[E0381]: used binding `var` is possibly-uninitialized
   --> example/src/lib.rs:127:23
    |
120 |             let var: MyGenericType;
    |                 -- binding declared here but left uninitialized
...
123 |                     var = value;
    |                     -- binding initialized here in some conditions
...
127 |             foo(var);
    |                       ^^ `var` used here but it is possibly-uninitialized

我该如何修复这个问题?

rust
  • 3 个回答
  • 61 Views
Martin Hope
kesarling
Asked: 2024-12-04 07:24:22 +0800 CST

为什么 printf 不能与 haskell 中的 ccall 一起使用?

  • 6

口粮:

{-# LANGUAGE ForeignFunctionInterface #-}

import GHC.Ptr
import Foreign
import Foreign.C
import Control.Monad

foreign import ccall unsafe "fibonacci.c fib" c_fib :: Int -> Int

example :: IO ()
example =
    print (c_fib 5)     

main = example

斐波那契

int fib(const int n) {
    if (n == 1) {
        printf("here");
        return 1;
    }
    else {
        return n*fib(n-1);
    }
}

结果:

120 // does not print "here"

到底是怎么回事?

haskell
  • 1 个回答
  • 16 Views
Martin Hope
kesarling
Asked: 2024-12-04 05:29:19 +0800 CST

我如何使 GHC.Ptr 成为 Monad 的实例?

  • 6

这是我目前所拥有的:

instance Monad Ptr where
    return = pure
    (>>=) (Ptr t) f = f t 

抛出的错误是:

    • Couldn't match a lifted type with an unlifted type
      When matching types
        a :: *
        GHC.Prim.Addr# :: TYPE 'GHC.Types.AddrRep
    • In the first argument of ‘f’, namely ‘t’
      In the expression: f t
      In an equation for ‘>>=’: (>>=) (Ptr t) f = f t
    • Relevant bindings include
        f :: a -> Ptr b (bound at RegionalMemory.hs:16:19)
        (>>=) :: Ptr a -> (a -> Ptr b) -> Ptr b
          (bound at RegionalMemory.hs:16:5)
   |
16 |     (>>=) (Ptr t) f = f t
   |                         ^

我不太明白这个错误。我做错了什么?

pointers
  • 1 个回答
  • 27 Views
Martin Hope
kesarling
Asked: 2024-11-03 13:37:40 +0800 CST

我是否正确地追踪了延续?

  • 5

我正在学习延续的概念。我认为一个好主意是追踪用延续形式编写的典型阶乘代码。我想知道我的理解是否正确。

代码:

fact :: Integer -> (Integer -> Integer) -> Integer
fact 0 k = k 1
fact n k = fact $ n-1 (\v -> k (n*v))

-- call: fact 5 id
-- answer: 120

这是我跟踪代码的方式(我认为能够跟踪它是理解延续性如何工作的基础):

fact 5 id --> 
fact 4 (\v -> id (5*v)) --> 
fact 3 (\v -> (\v -> id (5*v)) (4*v)) --> 
fact 2 (\v -> (\v -> (\v -> id (5*v)) (4*v)) (3*v)) --> 
fact 1 (\v -> (\v -> (\v -> (\v -> id (5*v)) (4*v)) (3*v))) (2*v)) --> 
fact 0 (\v -> (\v -> (\v -> (\v -> (\v -> id (5*v)) (4*v)) (3*v))) (2*v)) (1*v)) --> 
(\v -> (\v -> (\v -> (\v -> (\v -> id (5*v)) (4*v)) (3*v))) (2*v)) (1*v)) 1

这是应该如何追踪吗,还是我的基本概念错了?

附言:我理解vs 有点令人困惑,但我假设内部v遮蔽了外部v?

haskell
  • 1 个回答
  • 34 Views
Martin Hope
kesarling
Asked: 2024-09-25 05:31:48 +0800 CST

我如何重载 haskell 中的某个运算符以在两侧采用不同类型?

  • 5

口粮:

class Foo s where
    myCons :: Char -> s -> s
    myCons c xs = <my definition of however I wish to interpret this>

instance (Eq, Show) Foo where
    (:) x y = x `myCons` y

错误:

Pattern bindings (except simple variables) not allowed in instance declaration:
      (:) x y = x `myCons` y

我做错什么了?

我想要做的事情:

fooFromList :: [Int] -> Foo
fooFromList [] = Foo []
fooFromList (x:xs) = let x' = (convertDigitToChar x) in x':(fooFromList xs)
list
  • 2 个回答
  • 54 Views
Martin Hope
kesarling
Asked: 2024-06-25 03:39:58 +0800 CST

haskell 中的箭头运算符在采用多个参数的函数中如何有意义?

  • 6

仅供参考:进入 Haskell 的第二天

MRE(该函数的作用不重要):

foo :: Int -> Int -> Int
foo x y
        | x == y = x + y
        | x < y = x
        | x > y = y

这是我->迄今为止收集到的有关操作员(?)的信息:

  1. 右结合
  2. 它用于函数类型声明中,表示函数参数的类型及其返回类型

现在看一下以下函数类型声明:
foo :: Int -> Int
这声明(什么是合适的词?)一个名为的函数foo,它接受一个Int并返回一个Int。

但是,声明:foo :: Int -> Int -> Intdeclares(?)foo接受 2Int并返回一个,Int这与说foo :: Int -> (Int -> Int)哪个函数接受一个Int并返回一个函数相同,该函数接受一个Int并返回一个Int。

这怎么讲得通呢?难道不应该是这样的吗Int,Int -> Int?

PS:我正在使用的书:Richard Bird 的《使用 Haskell 进行函数式思考》。

haskell
  • 1 个回答
  • 50 Views
Martin Hope
kesarling He-Him
Asked: 2024-05-24 01:54:42 +0800 CST

在 Haskell 中结合使用 zip 和 zipWith

  • 7

我目前正在学习 Haskell,我遇到了以下难题。下面的结构有什么问题?:

zip [1,2,3] [4,5,6]
Result: [(1,4),(2,5),(3,6)]

zipWith (+) [1,2,3] [4,5,6]
Result: [5,7,9]

zipWith (zip) [1,2,3] [4,5,6]
          OR
zipWith zip [1,2,3] [4,5,6]

Result: <interactive>:22:1: error:
    • No instance for (Num [()]) arising from a use of ‘it’
    • In the first argument of ‘print’, namely ‘it’
      In a stmt of an interactive GHCi command: print it

Expected Result: [(1,4),(2,5),(3,6)]

我缺少什么?

haskell
  • 1 个回答
  • 40 Views
Martin Hope
kesarling He-Him
Asked: 2024-03-30 04:58:57 +0800 CST

如何修改列表理解中的外部变量?

  • 4

Python代码:

i: int = 1
table: list = [[i, bit, None, None] for bit in h]

预期行为:i每次迭代需要增加 1。

伪代码:

i: int = 1
table: list = [[i++, bit, None, None] for bit in h]
python
  • 3 个回答
  • 52 Views
Martin Hope
kesarling He-Him
Asked: 2023-11-05 05:27:43 +0800 CST

java程序中会存在僵尸线程吗?

  • 5

考虑下面的代码:

public... main( String[] args ) // yeah yeah, I'm too lazy to type it out
    new ThreadExample().start(); // the run function just prints out "this is run"
}

现在,可能会发生这样的情况:在调用启动的那一刻,调度程序切换回主程序,主程序块结束。start方法创建的线程会发生什么?仍然有一个非守护线程正在运行(我们创建的线程),这意味着程序应该继续执行,否则它可能会成为僵尸线程(我仍然不完全理解这意味着什么......)。可能发生什么(或者在没有同步的情况下“可能”发生什么)?


编辑:

这个问题最好改写为:“start() 方法调用到底做了什么?”
更多信息可以在:为什么我们调用 Thread.start() 方法?

java
  • 2 个回答
  • 101 Views
Martin Hope
kesarling He-Him
Asked: 2023-10-09 05:24:17 +0800 CST

如何比较Java中类的两个不同的泛型实例化?

  • 5

MRE 因为好吧,作业……叹息

这是我遇到的问题:
考虑以下课程:

class MyClass<T extends Comparable<T>> implements Comparable<MyClass<T>> {

... override the compareTo
}

MyClass<Integer>这使我可以与另一个MyClass<Integer>甚至MyClass<MyClass<Integer>>另一个进行比较MyClass<MyClass<Integer>>。但是,作业要求我能够进行比较,MyClass<MyClass<Integer>>因为MyClass<MyClass<String>>比较仅使用计数器并返回具有较大计数器的类(作为两者中较大的一个)。
我将如何实现这一目标?我的猜测是比较器开始发挥作用,正如此处所示。然而,我无法完全确定它。它是否像仅使用比较器中的比较(对象,对象)一样简单,还是其他东西?

java
  • 1 个回答
  • 41 Views
Martin Hope
kesarling He-Him
Asked: 2023-09-24 08:56:18 +0800 CST

模式“^[ab]?|c?$”接受哪些字符串?

  • 3

到目前为止我收集到的内容:

  1. ^ -> 匹配第一个符号
  2. $ -> 匹配最后一个符号
  3. [] -> 可以出现在该位置的字符集
  4. | ->“或”运算符
  5. ?-> 前面的条件是可选的

我认为该模式的含义是:
该行必须以 ana或 a开头b,或者必须以 a 结尾c,或者必须是空字符串。不过,我在绘制图案的 dfa 时遇到了麻烦。让我失望的是“可选”符号。我无法理解该语言不接受哪些字符串。


有人能指出我正确的方向吗?

pattern-matching
  • 1 个回答
  • 17 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve