Skip to content

Instantly share code, notes, and snippets.

View lbarasti's full-sized avatar

Lorenzo Barasti lbarasti

View GitHub Profile
// views/addMessage
import $ from 'jquery';
import cx from 'classnames';
import styles from '../../styles/Inner.module.css'
export default function Home() {
const submit = () => {
var formData = JSON.stringify($(document.forms[0]).serializeArray());
console.log('form data',formData);
const response = $.ajax({type: "POST", url: "../api/addMessage", async: false,
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] }