Skip to content

Instantly share code, notes, and snippets.

View sohang3112's full-sized avatar
:octocat:

Sohang Chopra sohang3112

:octocat:
View GitHub Profile
@sohang3112
sohang3112 / recursive-map.clj
Created June 29, 2022 06:16
Like `map`, but works recursively
(defn recursive-map [f coll]
;; is seq? right check??
;; it may not work for vectors
;; is there a coll? function?
(if (seq? coll)
(map #(recursive-map f %) coll)
coll)))
@sohang3112
sohang3112 / my-doto.clj
Created June 29, 2022 06:18
(Incomplete) re-implementation of `doto` macro
(defmacro my-doto [obj-expr & exprs]
(let [obj-sym (gensym "object")
actions (for [[method & args] exprs]
`(~method ~obj-sym ~@args))]
`(let [~obj-sym ~obj-expr]
~@actions
~obj-sym)))
;; Example
(my-doto (java.util.HashMap.)

Exercise 2

(defmacro def-vars [sym & exprs]
  `(do
     ~@(map-indexed (fn [i exp] `(def ~(symbol (str sym i)) ~exp)) 
                    exprs)))

Example Usage: (def-vars x (+ 1 2) 3 (* 2 5)) defines global variables x0, x1, x2.

Exercise 1

(defn has-number 
  "Does s expression have any number?"
  [sexp]
  (some number? (flatten sexp)))

;; Usage Examples
(has-number 'a)                          ;; => nil
(has-number '(a (b (c d) ((3)))))        ;; => true
@sohang3112
sohang3112 / print-inverted-triangle.clj
Last active June 29, 2022 06:44
Print inverted triangle of given size
(defn print-inverted-triangle
"Print inverted triangle of given size, i.e.,
the top right triangular half of a square of given size
Example: `(print-inverted-triangle 5)` prints:
```
*****
****
***
**
@sohang3112
sohang3112 / forth-notes.md
Last active April 8, 2025 20:51
Notes on the programming language Forth
@sohang3112
sohang3112 / aws-notes.md
Last active November 4, 2024 14:33
Notes on AWS CLI usage

AWS Notes

All config, credentials are stored by aws in ~/.aws.

Note: In any command, a variable written in all capital (example: SSO-PROFILE-NAME) has to be replaced by the appropriate value.

Installing AWS CLI v2

Official install without root

The official docs only show how to install aws-cli as root user. If you don't have root access, you can follow these steps:

@sohang3112
sohang3112 / dummy_object.py
Created July 8, 2022 05:26
Dummy object - any attribute / item access will always return the object itself. Calling as a function has the same effect. Useful for mocks in unit tests.
from __future__ import annotations
class Dummy:
"""
Class for a dummy object, where we can do arbitary attribute / item / method chaining - all of them always return the object itself.
Can also be called as a function, with any arguments or keyword arguments - again, the object itself is always returned.
>>> obj = Dummy()
>>> obj.x.y.z['key']('surprise', my='hurrah!') is obj
@sohang3112
sohang3112 / aws_sso_local_testing.md
Created July 11, 2022 12:01
Local testing of an AWS Lambda, where AWS SSO Login is required

AWS Lambda (Local Testing)

How to Run

  1. Create a .env file in the same directory as this script like this, and fill in the Lambda Name and SSO Profile Name (left blank here):
AWS_LAMBDA_FUNCTION_NAME=
AWS_SSO_PROFILE_NAME=
DEBUG=True
  1. Install all dependencies: pip install boto3 python-dotenv
@sohang3112
sohang3112 / zip-extract-recursive.ps1
Created July 14, 2022 04:48
Recursively extract all zip files in current folder
# Source: https://stackoverflow.com/a/71542036/12947681
Get-ChildItem '.' -R -Filter *.zip | ForEach-Object {
Expand-Archive $_.FullName "$($_.DirectoryName)/$($_.Basename)" -Force
Remove-Item $_.FullName
}