Skip to content

Instantly share code, notes, and snippets.

@nileshtrivedi
nileshtrivedi / web-components-are-not-ready.html
Last active May 3, 2022 07:59
Web Components are not yet ready
<!-- I will consider web components ready for use when this piece of HTML works -->
<!-- Here we import two different WC frameworks and mix custom input elements in a single form -->
<!-- Like regular HTML forms, we expect this form to work without writing any custom Javascript code for data plumbing -->
<!-- Some context: This will be possible in future with ElementInternals api which works in Chrome & Firefox, but not in Safari -->
<!-- See this conversation: https://twitter.com/nileshtrivedi/status/1521395909535404032 -->
<!-- Chrome team's blog post announcing ElementInternals from 2019: https://web.dev/more-capable-form-controls/ -->
<!-- Importing shoelace.style for the sl-rating element used below -->
@nileshtrivedi
nileshtrivedi / composable_web.md
Last active January 10, 2022 04:31
My thoughts on making the Web more composable like UNIX

The Composable Web Proposal

Serverless infrastructure like AWS Lambda and Google Cloud Functions have made it much cheaper for developers to offer server-side code for public consumption without keeping a server always running.

If these functions could be declared as stateless or deterministic, costs can be brought down even more because only the first invocation needs to be executed. Cached response could be returned for future invocations with the same input arguments.

All modern browsers support URL lengths of thousands of characters, even on mobile. A lot of data can be embedded and passed around directly in the URLs (instead of passing identifiers which requires a look-up which costs server time).

So here's a thought:

@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 / parrondo.rb
Created January 2, 2021 05:43
Simulation of Parrondo's Paradox
def coin(win) = rand(1000) > (1000 - win) ? 1 : -1
def a(capital) = coin(495) # 49.5% chance of winning
def b(capital) = (capital % 3 != 0) ? coin(745) : coin(95)
def simulate(strategy, count)
(strategy * count).reduce(100) do |capital,game|
capital + send(game, capital)
end
end
@nileshtrivedi
nileshtrivedi / programming.md
Last active December 8, 2020 08:25
Programming: Mostly A Hate Story

Programming: Mostly A Hate Story

I wanted to do digital signatures validation, preferably ed25519, inside PostgreSQL triggers. Here is how it went:

Surely pgcrypto must be supporting it, right? Most Postgres cloud hosting providers already support pgcrypto so this would be perfect. Right?

Well, pgcrypto only supports PGP and that too excludes digital signatures. Let's give PGP a try anyway and see how far can we go.

Installed gpg to generate the keys and the experience is less than pleasant. Sometimes it gets stuck at the passphrase prompt. The keys are too big, but still I can make pgcrypto's pgp_pub_encrypt and pgp_pub_decrypt methods work. Just remeber to convert keys in ASCII to binary and vice-versa using armor()/dearmor(). I hate the big key size in RSA, even though GPG defaults to 2048-bit keys and not the more secure 4096-bit ones. Let's look into ed25519 now.

@nileshtrivedi
nileshtrivedi / chatbot.rb
Last active October 27, 2020 07:29
A stateful chatbot to capture data in 50 lines of Ruby
# https://medium.com/cleartax-engineering/a-simple-rule-based-stateful-chatbot-for-data-capture-ebfad9271388
require 'ostruct'
# Track the current state of the conversation
state = {pointer: ""}
# Metadata is used to fill the outgoing templates AND store the captured data from conversation
metadata = {name: "Calvin"}
@nileshtrivedi
nileshtrivedi / html-mocker.js
Last active July 11, 2020 09:32
HTML Mocker
/*
This is an idea for mocking HTML data that lets you test whether your layout breaks on any screen size for any
unexpected dynamic data.
This script scans the document for class names with a specific pattern and periodically randomizes the content
of those elements while meeting the constraints specified in the class name. This can let you quickly test
whether your layout breaks for any dynamic content that you may not have thought about. This should ideally be used
with a responsive design testing tool such as DevTools or Sizzy/Bizzy.
@nileshtrivedi
nileshtrivedi / worldview.md
Created May 12, 2020 01:50
WorldView : A personal beliefs management system

WorldView

This is the idea for an app I have wanted to build for many years. This would be a database of personal beliefs (also known as a "Propositional Knowledge Base"), connected via logical arguments of various kinds: deductive, inferential and Bayesian. Any new facts you learn should update the certainties of the involved beliefs and propagate that through the graph. You could build a social network around it, giving all users a chance to learn from each other's worldview. This would also help one detect inconsistencies and contradictions. The challenge here is to allow local contradictions without blowing up the system as Principle of Explosion implies.

Related links:

To bring some sanity to your twitter feed, add these words to your muted list here: https://twitter.com/settings/muted_keywords

Needless to say, this is highly subjective and may not be applicable to how you want to use Twitter. Many of these conversations are important, but I have those on other platforms, not Twitter.

Other people's likes

suggest_recycled_tweet_inline

suggest_activity_tweet

@nileshtrivedi
nileshtrivedi / MainActivity.java
Last active January 8, 2019 12:05
HyperTrack Onboarding
HyperTrackCore.requestLocationPermissions(this, new LocationPermissionCallback() {
@Override
public void onLocationPermissionGranted() {
// Handle location permission granted
}
@Override
public void onLocationPermissionDenied() {
// Handle location permission denied
}