Skip to content

Instantly share code, notes, and snippets.

View lbarasti's full-sized avatar

Lorenzo Barasti lbarasti

View GitHub Profile
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?,
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
@lbarasti
lbarasti / launch.json
Created July 14, 2020 09:30
VS code config for Crystal projects - includes tasks to run and debug the current file
{
"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": [],
@lbarasti
lbarasti / select_terminate.cr
Created May 2, 2020 23:54
select use case 1: graceful termination
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
@lbarasti
lbarasti / select_send_receive.cr
Created May 2, 2020 23:50
use case 2: mixing send and receive
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
@lbarasti
lbarasti / select_with_timeout.cr
Last active May 2, 2020 23:49
select use case 3: timeout on async calls
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
@lbarasti
lbarasti / select_pipeline.cr
Created May 2, 2020 23:43
select use case 4: dealing with back-pressure
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
@lbarasti
lbarasti / select_heartbeat.cr
Created May 2, 2020 23:37
select use case 5: Heartbeats
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
@lbarasti
lbarasti / normal_distribution.cr
Last active May 4, 2020 14:13
Sampling random variables and plotting histograms in Crystal
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] }
@lbarasti
lbarasti / des.cr
Last active January 20, 2021 16:44
Playing with queues and Discrete-Event Simulations in Crystal
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} }