Skip to content

Instantly share code, notes, and snippets.

@nileshtrivedi
nileshtrivedi / sm.rb
Created February 2, 2018 06:22
Multiplication of two state machines
a = {a: [:b, :c], b: [:c]}
def get_edges(sm)
res = []
sm.each do |s, e|
e.each do |d|
res.push([s,d])
end
end
res
@nileshtrivedi
nileshtrivedi / ed25519.sql
Last active February 3, 2023 08:59
ed25519 digital signature methods in PostgreSQL via PL/Python
CREATE EXTENSION IF NOT EXISTS plpythonu;
CREATE FUNCTION py_create_ed25519_keypair ()
RETURNS varchar[]
AS $$
import axolotl_curve25519 as curve
import os
import base64
randm32 = os.urandom(32)
Verifying my Blockstack ID is secured with the address 15e4krfMSXCgWrRpo2FnVRy7DLQ7HuMBQm https://explorer.blockstack.org/address/15e4krfMSXCgWrRpo2FnVRy7DLQ7HuMBQm
@nileshtrivedi
nileshtrivedi / deploy.rb
Last active January 16, 2016 21:01
Deploy a Play framework app with SSHKit
require 'sshkit'
require 'sshkit/dsl'
# Once "bundle install" has been run, this script can be run as "ruby deploy.rb staging" or "ruby deploy.rb prod"
if ARGV.first == "prod"
servers = ['deploy@prod1.example.com', 'deploy@prod2.example.com']
elsif ARGV.first == "staging"
servers = ['deploy@staging.example.com']
else
#!/usr/bin/python
# You need stereovision package to run this. Try "sudo pip install stereovision"
import cv2
from stereovision.blockmatchers import StereoBM, StereoSGBM
from stereovision.calibration import StereoCalibration
from stereovision.stereo_cameras import CalibratedPair
from stereovision.ui_utils import STEREO_BM_FLAG, BMTuner
@nileshtrivedi
nileshtrivedi / hash_builder.rb
Created November 24, 2011 17:49 — forked from brentd/gist:360506
HashBuilder allows you to build a Hash in Ruby similar to Builder with some enhancements
# Allows you to build a Hash in a fashion very similar to Builder. Example:
# Fork of https://gist.github.com/360506 by BrentD with some enhancements
#
# HashBuilder.build! do |h|
# h.name "Nilesh"
# h.skill "Ruby"
# h.skill "Rails" # multiple calls of the same method will collect the values in an array
# h.location "Udaipur, India" do # If a block is given, first argument will be set as value for :name
# h.location do
# h.longitude 24.57
@nileshtrivedi
nileshtrivedi / symbolic.rb
Created October 11, 2011 15:38
Simple symbolic math implementation in Ruby (Work in progress)
# TODO
# if a var has a value, it also needs a name. This should not be necessary. Variable.initialize should accept hashargs
# Expression.to_s should output in infix-notation (like normal humans)
# add more syntactic sugar by manipulating Ruby Numeric classes. Add symbolic operators to Ruby's Numeric
# If symbolic expression contains variables without value then it should return nil (should it, really?)
# Implement additional operators like **
# All symbolic expression should be automatically simplified when created:
# cons(0) * x # => 0
# 2 + x + 1 # => x + 3
# -(x-y) + 2*x # => x + y
@nileshtrivedi
nileshtrivedi / computer.rb
Created October 5, 2011 16:30
A simple virtual machine to experiment with minimal instruction sets
# My attempt to implement a simple virtual machine with a minimal instruction set
# 8-bit computer, fixed-size memory, single cpu, 0-registers, based on von-neumann architecture
# No support for interrupts or I/O although we cheat and provide a "print" instruction :)
# The goal is to find a minimal, but turing-complete, instruction set
# For simplicity, every instruction has two operands, which are located adjacent to it in the memory
# This code is just to experiment with various instruction sets and not to be taken seriously
# Next fun step would be to develop a language/compiler for this virtual machine
class Memory
def initialize(size, program)
@nileshtrivedi
nileshtrivedi / jdbcmysql_adapter.rb
Created June 29, 2011 20:16
Make enum-column plugin for Rails work with JDBC MySQL adapter as well
module ActiveRecord
module ConnectionAdapters
class MysqlAdapter
alias __native_database_types_enum native_database_types
def native_database_types #:nodoc
types = __native_database_types_enum
types[:enum] = { :name => "enum" }
types
end
@nileshtrivedi
nileshtrivedi / mmacmd.rb
Created February 12, 2011 21:03
Quick Ruby script to play a simple accompanying track on command-line using MMA
#!/usr/bin/env ruby
#Example usage: mmacmd.rb basicrock 120 4 Bm Bm A A G Em Bm Bm
#MMA Website is here: http://www.mellowood.ca/mma/index.html
groove = ARGV.slice!(0)
tempo = ARGV.slice!(0)
count = ARGV.slice!(0)
puts "Generating MMA file"
File.open("/tmp/mmaout.mma","w") { |f|