Skip to content

Instantly share code, notes, and snippets.

View harsh183's full-sized avatar
😺
People pleasing users

Harsh Deep harsh183

😺
People pleasing users
View GitHub Profile
@harsh183
harsh183 / typeInf125.md
Last active December 25, 2020 05:29
Blurb I wrote on CS 125 Forum about explaining Type Inference

I've gotten many questions about this from both students, and current existing course staff since type inference is a reasonably new age concept that is getting picked up in the programming languages of the 2010s and 2020s.

The lesson gives a pretty good starting point, especially the 7-minute walkthrough here on the basics of type inference.


Talk is cheap, show me the code

I redid a few of the older problems using var type inference across a few of the past homework problems to see this in action. I highly suggest checking these out.

@harsh183
harsh183 / hackyTestFramework.java
Last active June 8, 2024 01:42
Java hacky test framework
interface TestCase {
boolean test();
}
// print title if case fails
void checkCase(String title, TestCase t) {
if (t.test() == false) {
System.out.println("Failed: " + title);
}
}
int add(int a, int b) {
@harsh183
harsh183 / simple-linked-list-data-class.kt
Last active April 17, 2021 11:03
Simple singly linked list using data classes in Kotlin using one line. Featuring data classes and null safety
// Linked list
data class Node<T>(var value: T, var next: Node<T>?);
fun main() {
val head = Node(1, null)
val second = Node(2, Node(3, null)) // two more (init multiple)
head.next = second
println(head.value) // 1
println(head.next?.value) // 2
#!/usr/bin/env bash
set -ex
kotlinc $1 -d $1.jar
java -jar $1.jar
@harsh183
harsh183 / collection_declare_vs_initializer_functions.kt
Last active April 23, 2021 12:53
Comparing directly declaring lists vs initializer functions (using lambdas). The syntax is pretty decent except for maps but to be fair if you're doing this type of thing you should not be using maps.
fun main() {
// Both are the same
val array1: Array<Int> = arrayOf(0, 1, 4, 9, 16)
val list1: List<Int> = listOf(0, 1, 4, 9, 16)
val map1: Map<Int, Int> = mapOf(0 to 0, 1 to 1, 2 to 4, 3 to 9, 4 to 16)
val array2: Array<Int> = Array(5) { it*it }
val list2: List<Int> = List(5) {it*it}
val map2: Map<Int, Int> = (Array(5) {it}).associate {it to it*it}
}
@harsh183
harsh183 / ruby_times_in_kotlin.kt
Last active April 23, 2021 12:53
Recreating ruby's n.times operator on Kotlin. This is for fun, in all seriousness use the repeat(n) operator
fun main() {
// normal function
repeat(3) { print("Yay $it ") }
// => Yay 0 Yay 1 Yay 2
2.times { print("Looop ") }
// => Looop Looop
10.times { print("$it ") }
// => 0 1 2 3 4 5 6 7 8 9
}
@harsh183
harsh183 / functional_c++.cpp
Last active April 23, 2021 12:50
Basic and cool functional programmig concepts in C++14 onwards. Lots of auto abuse too for interesting type inference and results. Useful as a quick reference too. (Formatting is a bit wonky bc gist is weird sorry)
#include <iostream>
#include <string>
#include <vector>
#include <functional> // std::function, std::bind
#include <algorithm> // std::transform, std::remove_if
#include <numeric> // std::accumulate
using namespace std;
require 'pry'
require 'rqrcode'
qrcode = RQRCode::QRCode.new('https://cs125.cs.illinois.edu')
png = qrcode.as_png(
bit_depth: 1,
border_modules: 4,
color_mode: ChunkyPNG::COLOR_GRAYSCALE,
color: 'black',
@harsh183
harsh183 / mypy-example.py
Last active July 1, 2021 17:26
A small exploration with mypy and python type annotations.
# Simple script I put together showing mypy in action
# standard type annotations - we can give arguments a type and expect a return type
def add(x: int, y: int) -> int:
return x + y
print(add(1, 2))
# we can let it pick within a list of types
from typing import Union
#!bin/bash
# This script is for me creating my brain dumps that I usually do jumping right into it in vim.
# I have a template file also defined that I can setup if I want.
# This script also brings me back to the same context that I was in earlier
set -ex
cd ~/brain-dumps/