Skip to content

Instantly share code, notes, and snippets.

@staticVoidMan
staticVoidMan / singleton.swift
Created August 1, 2023 07:55
Singleton with configurable values
class Singleton {
private(set) static var shared = Singleton(name: "")
let name: String
private init(name: String) {
self.name = name
}
static func configure(name: String) {
@staticVoidMan
staticVoidMan / bubbleSort.swift
Last active July 31, 2023 16:05
Bubble Sort
// Ref: https://www.youtube.com/watch?v=o4bAoo_gFBU
extension Array where Element: Comparable {
func bubbleSorted(by compare: ((Element, Element) -> Bool) = (<)) -> [Element] {
var array = self
array.bubbleSort(by: compare)
return array
}
@staticVoidMan
staticVoidMan / server.js
Created April 6, 2023 09:57
Simple NodeJS CRUD API with in-memory persistence
const express = require('express');
const { v4: uuidv4 } = require('uuid');
const app = express();
app.use(express.json());
const orders = [];
app.get('/order/all', (req, res) => {
res.send(orders);
@staticVoidMan
staticVoidMan / ConditionalView.swift
Last active April 4, 2023 06:15
A wrapper for if-else styled views in SwiftUI
import SwiftUI
struct ConditionalView<T: View, U: View>: View {
@Binding var condition: Bool
@ViewBuilder var then: T
@ViewBuilder var `else`: U
init(
@staticVoidMan
staticVoidMan / ftree.sh
Created December 7, 2022 18:36
Flat Tree representation of specified folder
#!/bin/bash
function usage() {
echo "Usage: ${0} [PATH]"
exit 1
}
FOLDER_PATH=${1}
if [[ -z ${FOLDER_PATH} ]]; then
usage
@staticVoidMan
staticVoidMan / fbinupload.sh
Last active December 7, 2022 20:49
Shell script to bulk upload files from a folder to filebin.net
#!/bin/bash
function help() {
# Display Help
echo "Bulk upload files to filebin.net"
echo
echo "Usage : ./fbinupload.sh [arguments]"
echo "or : ./fbinupload.sh upload from current folder to a random bin"
echo "or : ./fbinupload.sh -b abcd upload from current folder to bin id 'abcd'"
echo "or : ./fbinupload.sh -p test upload from 'test' folder"

How to download your Udemy course videos using youtube-dl

$ youtube-dl --list-extractors | grep udemy

Steps

  1. Get link to the course to download. e.g. https://www.udemy.com/course-name/
  2. Login into udemy website, save the cookie from chrome using Chrome (Cookie.txt)[1] export extension. Save it to file udemy-cookies.txt
  3. Get the link of the video that you want to download. usually in format. Use the command provided below where you have to replace the {course_link} and {path_to_cookies_file} with respective paths.
$ youtube-dl {course_link} --cookies {path_to_cookies_file}
@staticVoidMan
staticVoidMan / stack.swift
Last active September 12, 2023 09:00
Data Structure - Stack
struct StackFullError<T>: Error {
let value: T
}
struct Stack<T> {
private var elements: [T] = []
private var capacity: Int?
var isEmpty: Bool { elements.isEmpty }
//Queue is concurrent
let queue = OperationQueue()
queue.maxConcurrentOperationCount = 2
queue.qualityOfService = .utility
let aOperation = BlockOperation {
Thread.sleep(forTimeInterval: 1)
print(1.1)
}
//Execution blocks are concurrent
@staticVoidMan
staticVoidMan / DispatchGroupExample.swift
Created March 11, 2022 06:55
DispatchGroup vs Function chaining
/*
Objective is to perform N tasks and get notified only when all the tasks have completed.
In this scenario, the order of execution does not matter.
*/
func doSomethingSlow(id: Double, completion: @escaping ()->Void) {
DispatchQueue.global(qos: .utility).asyncAfter(deadline: .now() + 2) {
print("\(id) Completed")
completion()
}