标签: crystal-lang

Crystal - 如何递归更改哈希值并将每个更改保存到新哈希

使用这个Hash对象

{"foo" => {"bar" => 1, "baz" => 2}, "bla" => [1,2,3]}
Run Code Online (Sandbox Code Playgroud)

我想生成这个Hash对象数组

[
  {"foo" => "*", "bla" => [1,2,3]},
  {"foo" => {"bar" => "*", "baz" => 2}, "bla" => [1,2,3]},
  {"foo" => {"bar" => "1", "baz" => "*"}, "bla" => [1,2,3]},
  {"foo" => {"bar" => "*", "baz" => 2}, "bla" => "*"}, 
]
Run Code Online (Sandbox Code Playgroud)

我基本上遍历了每个键并将其值更改为"*",同时保留了散列的整体结构并保存了在某个数组中生成的新散列.

我已经尝试了很多想法,但大多数都不会工作,因为我之前可以猜到Array类型,我只知道这个哈希是由JSON.parse生成的,然后变成Hash(String,JSON :: Any)

我目前正在尝试它

hash = {"bar" => {"and" => "2", "br" => "1"}}
arr = [hash, {"bar" => "1"}]
arr.delete(arr.last)
arr.delete(hash)

def changer(hash, arr, …
Run Code Online (Sandbox Code Playgroud)

crystal-lang

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

在 Crystal lang 中杀死一个线程

我写了一个程序,一个水晶程序,用 Sieve 计算一个范围内的素数。

代码

#!/usr/bin/env crystal

def sieve(max)
    t = Thread.new do
        dot, ary, colours = ".", ["\xE2\xA0\x81", "\xE2\xA0\x88", "\xE2\xA0\xA0", "\xE2\xA0\x84"] * 2, [154, 184, 208, 203, 198, 164, 129, 92]
        print "\e[?25l"

        loop do
            ary.size.times do |x|
                print("\e[2K#{ary[x]} \e[38;5;#{colours[x]}mPlease Wait#{dot * x}\e[0m\r")
                sleep(0.1)
            end
        end
    end

    s = [nil, nil] + (2..max).to_a
    s.each do |x|
        next unless x
        break if (sq = x ** 2) > max
        (sq..max).step(x) { |y| s[y] = nil }
    end

    puts "\e[?25h"
    s.tap { …
Run Code Online (Sandbox Code Playgroud)

multithreading crystal-lang

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

Crystal 编译器未检测到该对象不是 nil

我有以下课程:

class X
    property son, val
    def initialize(@val : Int32)
        @son = nil.as X?
    end

    def add(other : X?)
        unless other.nil?
            if @son.nil?
                @son = other
            else
                @son.add(other)
            end
        end
    end
end

x = X.new 5
x.add(nil)
x.add(X.new 3)
Run Code Online (Sandbox Code Playgroud)

但是当我尝试build我得到

Showing last frame. Use --error-trace for full trace.

In nil-test.cr:12:22

 12 | @son.add(other)
           ^------
Error: undefined method 'include' for Nil (compile-time type is (X | Nil))
Run Code Online (Sandbox Code Playgroud)

根据手册,这正是编译器应该识别出@son不能nilelse分支中的情况,但它显然没有这样做。

我究竟做错了什么 ?

注意:使用 …

crystal-lang

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

为什么 Dart 不能从 Crystal 解码 base64?

为什么 Dart 会使用 Crystal Lang 的 base64 产生错误“位置 61 处的无效字符”?

base64 dart crystal-lang

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

Crystal语言中的.get哈希方法

我有这个Python代码:

# some_dic is a dictionary
value = some_dic.get(var_name, None)
Run Code Online (Sandbox Code Playgroud)

我怎样才能在水晶中做同样的事情?

python hash dictionary crystal-lang

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

标签 统计

crystal-lang ×5

base64 ×1

dart ×1

dictionary ×1

hash ×1

multithreading ×1

python ×1