Skip to content

Instantly share code, notes, and snippets.

View PeteGabriel's full-sized avatar
🏇
chances are im on a horse

Pedro Gabriel PeteGabriel

🏇
chances are im on a horse
View GitHub Profile
@PeteGabriel
PeteGabriel / never.ts
Created November 26, 2019 13:00
How the type "Never" works
import * as fs from "fs";
function raise(msg: string): never {
console.error(`Error "${msg}" raised at ${new Date()}`)
throw new Error(msg)
}
function readConfig(configFile: string): string {
if (!fs.existsSync(configFile))
raise(`Configuration file ${configFile} missing.`)
@PeteGabriel
PeteGabriel / settings.json
Created September 17, 2019 10:28
VS Code JS settings
{
"workbench.activityBar.visible": true,
"editor.lineHeight": 0,
"files.autoSave": "onFocusChange",
"editor.fontLigatures": true,
"editor.smoothScrolling": true,
"editor.mouseWheelZoom": true,
"workbench.sideBar.location": "right",
"editor.autoClosingBrackets": "always",
"mocha.files.ignore": [
@PeteGabriel
PeteGabriel / Parenthesis.cs
Created December 4, 2018 14:46
Get closing parenthesis for opening position
public class Parenthesis
{
public static int GetClosingParenthesis(string input, int openingPos)
{
var counter = 0;
var positionFound = -1;
for (int i = 0; i < input.Length; i++)
{
if (input[i] == '(' && openingPos == i)
{
@PeteGabriel
PeteGabriel / method_calls.go
Last active August 15, 2018 17:02
Example of method calls in GO
package main
import (
"fmt"
)
func main() {
//Declare a pointer of type Human
peter := new(Human)
//The compile will dereference the pointer to make the call
@PeteGabriel
PeteGabriel / Extensions.swift
Created June 17, 2018 14:05
Create an UIColor from HEX values
extension UIColor {
convenience init(rgb:UInt, alpha:CGFloat = 1.0) {
self.init(
red: CGFloat((rgb & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgb & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgb & 0x0000FF) / 255.0,
alpha: CGFloat(alpha)
)
}
}
@PeteGabriel
PeteGabriel / remove.scala
Created June 10, 2018 22:08
Remove operation
def deleteNode(node: FrequencyNode): Unit = {
node.prev.next = node.next
node.next.prev = node.prev
node.next= null; node.prev = null
}
@PeteGabriel
PeteGabriel / access.scala
Created June 10, 2018 22:07
Access operation
def access(key: UUID): Int = {
val tmp = this.byKey(key)
if (tmp == null) {
throw new Exception("No such key")
}
val freq = tmp.parent
var nextFreq = freq.next
if (nextFreq == this.frequencyHead || nextFreq.value != (freq.value + 1)) {
@PeteGabriel
PeteGabriel / cache_insert.scala
Created June 10, 2018 22:04
Insert operation
def insert(key: UUID, value : Int) = {
if (this.byKey.contains(key)){
throw new Exception("Key already exists")
}
val freqNode = this.frequencyHead.next
if (freqNode.value != 1){
this.frequencyHead.next = LfuCache.getNewNode(this.frequencyHead, freqNode)
this.byKey(key) = LfuItem(this.frequencyHead.next, value, key)
this.frequencyHead.next.items += this.byKey(key)
@PeteGabriel
PeteGabriel / time.scala
Created May 20, 2018 14:16
Measure time in Scala
object Time {
def time[A](a: => A) = {
val now = System.nanoTime
val result = a
val micros = (System.nanoTime - now) / 1000
println("%d microseconds".format(micros))
result
}
@PeteGabriel
PeteGabriel / guid.pyw
Created April 13, 2018 20:05
Generate a guid and copy it to clipboard (no console open)
# Install the following modules:
# pip install pyperclip
# pip install uuid
import uuid
import pyperclip
pyperclip.copy(str(uuid.uuid1()))
# On Windows systems, there is no notion of an “executable mode”.