This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "json" | |
# https://csrc.nist.gov/schema/nvd/feed/1.1/nvd_cve_feed_json_1.1.schema | |
record CVE_Response, resultsPerPage : Int32, startIndex : Int32, | |
totalResults : Int32, result : CVE_Result do | |
include JSON::Serializable | |
end | |
record CVE_Result, cve_data_type : String, cve_data_format : String, | |
cve_data_version : String, cve_data_numberOfCVEs : String?, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def source(generator, target:, name: nil) | |
Ractor.new(generator, target, name: name) do |generator, target| | |
loop do | |
target.send generator.next | |
end | |
end | |
end | |
def buffer | |
Ractor.new do |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "0.2.0", | |
"configurations": [ | |
{ | |
"type": "lldb", | |
"request": "launch", | |
"name": "crystal: debug current file", | |
"preLaunchTask": "crystal: build current file (debug)", | |
"program": "${workspaceFolder}/bin/${fileBasenameNoExtension}", | |
"args": [], |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def producer(name : String, &generator : -> T) forall T | |
Channel(T).new.tap { |ch| | |
spawn(name: name) do | |
loop do | |
ch.send generator.call | |
end | |
end | |
} | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def producer(name : String, &generator : -> T) forall T | |
Channel(T).new.tap { |ch| | |
spawn(name: name) do | |
loop do | |
ch.send generator.call | |
end | |
end | |
} | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def log(msg) | |
puts "#{Fiber.current.name}: #{msg}" | |
end | |
Cache = Hash(Symbol, Float64?).new | |
def get_stock_price_async(sym : Symbol) : Channel(Float64) | |
Channel(Float64).new.tap { |ch| | |
spawn do | |
sleep rand |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def producer(name : String, &generator : -> T) forall T | |
Channel(T).new.tap { |ch| | |
spawn(name: name) do | |
loop do | |
ch.send generator.call | |
end | |
end | |
} | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def log(msg) | |
puts "#{Fiber.current.name}: #{msg}" | |
end | |
record Heartbeat, id : UInt64 = Fiber.current.object_id | |
Diagnostic = Channel(Heartbeat).new | |
diagnostic_enabled = true # tweak this value | |
if diagnostic_enabled |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "statistics" | |
require "ishi" | |
require "tablo" | |
include Statistics::Distributions | |
# Turns a named tuple into tabular representation | |
def table(data : NamedTuple) | |
Tablo::Table.new(data.map { |k, v| [k, v] }, header_frequency: nil) { |t| | |
t.add_column("coefficient") { |n| n[0] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "statistics" | |
require "tablo" | |
extend Statistics::Distributions | |
# Launches a DES that will halt once the simulation's time exceeds `max_t` | |
def simulate(nodes : Array(Node), max_t : Int32) | |
t = 0_f64 | |
loop do | |
next_node = nodes.map { |a| {a, a.next(t) || Float64::MAX} } |
NewerOlder