Skip to content

Instantly share code, notes, and snippets.

View AndrewBarba's full-sized avatar

Andrew Barba AndrewBarba

View GitHub Profile
---@param input string
---@return table<string>
local function parse_raw(input)
local res = {}
local is_key = true
local is_value = false
local key_t = {}
local value_t = {}
@AndrewBarba
AndrewBarba / OrderedMap.lua
Last active May 30, 2023 16:40
OrderedMap.lua
---@class OrderedMap
local OrderedMap = {}
OrderedMap.__name = 'Map'
local keys_key = setmetatable({}, {
__tostring = function() return "<___keys___>" end
})
local vals_key = setmetatable({}, {
__tostring = function() return "<___vals___>" end
@AndrewBarba
AndrewBarba / vercel_edge_config.py
Created May 11, 2023 21:20
Vercel EdgeConfig for Python Runtime
import os
import json
from urllib.parse import urlparse
edge_config_id_prefix = "ecfg_"
class EdgeConfigError(Exception):
pass
@AndrewBarba
AndrewBarba / AsyncMap.swift
Created June 20, 2021 03:18
Concurrent async version of map on any Sequence
extension Sequence {
func mapAsync<T>(_ handler: @escaping (Element) async -> T) async -> [T] {
return await withTaskGroup(of: (Int, T).self) { group in
var results: [Int: T] = [:]
for (index, item) in self.enumerated() {
group.async { (index, await handler(item)) }
}
for await (index, transformed) in group {
results[index] = transformed
@AndrewBarba
AndrewBarba / EpsonDeviceNameToSeries.swift
Created May 25, 2017 15:00
Convert Epson printer device name to series enum
func printerSeries(for name: String) -> Int32 {
switch name.uppercased() {
case "TM-M10":
return EPOS2_TM_M10.rawValue
case "TM-M30":
return EPOS2_TM_M30.rawValue
case "TM-P20":
return EPOS2_TM_P20.rawValue
case "TM-P60":
return EPOS2_TM_P60.rawValue
@AndrewBarba
AndrewBarba / cs6740-space-boilerplate.js
Last active February 11, 2016 20:19
A boilerplate for the cs6740 space colonization problem written in Javascript. Abstracts away parsing and organizing the data so you can focus on the algorithm needed to solve the challenge.
'use strict';
/**
* Returns the cheapest way to colonize the given species
* The first argument is an Array of species, and the second argument
* is an array of discounts.
*
* A single species in the array, accesed like species[0], has 2 properties:
* - cost: Int - the cost to bring 1 of these species without a discount
* - needToBring: Int - the number of species I need to bring
//
// Dispatch.swift
//
// Created by Andrew Barba on 8/25/15.
//
import Foundation
public struct Dispatch {
@AndrewBarba
AndrewBarba / make-xcode-fast
Created April 14, 2015 15:17
Make Xcode (with Cocoapods) faster by cleaning Pods, workspaces, and derived data folders
xcode-fast:
rm -rf Pods
rm -rf *.lock
rm -rf *.xcworkspace
rm -rf ~/Library/Caches/com.apple.dt.Xcode/*
rm -rf ~/Library/Developer/Xcode/DerivedData/*
pod install
@AndrewBarba
AndrewBarba / UIColorMake
Last active August 29, 2015 14:06
Objective-C macro for easily creating UIColor instances with RGB values between 0 and 255
/**
* Example: UIColor *teal = UIColorMake(9.0, 171.0, 161.0, 1.0)
*/
CG_INLINE UIColor*
__UIColorMake(CGFloat r, CGFloat g, CGFloat b, CGFloat a)
{
return [UIColor colorWithRed:(r/255.0) green:(g/255.0) blue:(b/255.0) alpha:a];
}
#define UIColorMake __UIColorMake
/**
* @updated: 2014-07-29
* @source: http://stackoverflow.com/questions/6052966/phone-number-formatting
*/
-(NSString*)phoneNumberString
{
static NSCharacterSet* set = nil;
TL_DISPATCH_ONCE(^{
set = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
});