Skip to content

Instantly share code, notes, and snippets.

@nileshtrivedi
nileshtrivedi / parrondo.rb
Created Jan 2, 2021
Simulation of Parrondo's Paradox
View parrondo.rb
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 / chatbot.rb
Last active Oct 27, 2020
A stateful chatbot to capture data in 50 lines of Ruby
View chatbot.rb
# 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"}
View html-mocker.js
/*
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 / web-identity.md
Last active Jun 10, 2020
Light-weight Identity for the Web using Browser Sync and Push
View web-identity.md

Light-weight Identity for the Web using Browser Sync and Push

(This is just a rough idea intended to trigger a discussion. Details need to be fleshed out for a proper evaluation.)

Signing up and logging into website continues to be painful. For a while, there were attempts like the Mozilla Persona project but those were discontinued and that has led to social logins gaining prominence, with all the concerns of centralization, privacy, surveillance etc as valid as ever. In fact, more and more websites are now adopting Google's One Tap login because the convenient UX is giving them better conversions.

However, some things have changed in recent years:

  • Push notifications are now widely supported in browsers. This is relevant because one of the main reasons why websites insist on user registration is so that they can communicate with the users via email or phone when they are offline.
@nileshtrivedi
nileshtrivedi / worldview.md
Created May 12, 2020
WorldView : A personal beliefs management system
View worldview.md

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:

@nileshtrivedi
nileshtrivedi / home-server.md
Last active Feb 18, 2021
Home Server setup: Raspberry PI on Internet via reverse SSH tunnel
View home-server.md

Raspberry Pi on Internet via reverse SSH tunnel

HackerNews discussed this with many alternative solutions: https://news.ycombinator.com/item?id=24893615

I already have my own domain name: mydomain.com. I wanted to be able to run some webapps on my Raspberry Pi 4B running perpetually at home in headless mode (just needs 5W power and wireless internet). I wanted to be able to access these apps from public Internet. Dynamic DNS wasn't an option because my ISP blocks all incoming traffic. ngrok would work but the free plan is too restrictive.

I bought a cheap 2GB RAM, 20GB disk VM + a 25GB volume on Hetzner for about 4 EUR/month. Hetzner gave me a static IP for it. I haven't purchased a floating IP yet.

View india_twitter_muted_words_list.md

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 / composable_web.md
Last active Sep 27, 2020
My thoughts on making the Web more composable like UNIX
View composable_web.md

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 / MainActivity.java
Last active Jan 8, 2019
HyperTrack Onboarding
View MainActivity.java
HyperTrackCore.requestLocationPermissions(this, new LocationPermissionCallback() {
@Override
public void onLocationPermissionGranted() {
// Handle location permission granted
}
@Override
public void onLocationPermissionDenied() {
// Handle location permission denied
}
@nileshtrivedi
nileshtrivedi / sample.py
Created Jun 18, 2018
Cloud Firestore - Python example
View sample.py
# This script can be used standalone
# So it doesn't depend on filesystem or Google Cloud for the credentials
from google.cloud import firestore
from google.oauth2 import service_account
key = 'your_credentials_json_string'
info = json.loads(key)
creds = service_account.Credentials.from_service_account_info(info)