Skip to content

Instantly share code, notes, and snippets.

@neoneye
neoneye / squareroot.rb
Created March 19, 2020 20:34
Alternative way of computing the squareroot using acos+sin
# Mesolabe Compass and Square Roots - Numberphile
# https://www.youtube.com/watch?v=9VVPBS_flOI
v=4; Math.sin(Math.acos(2.0 / (v + 1) - 1.0)) * (v + 1) / 2.0
# 2.0000000000000004
v=9; Math.sin(Math.acos(2.0 / (v + 1) - 1.0)) * (v + 1) / 2.0
# 3.0
v=16; Math.sin(Math.acos(2.0 / (v + 1) - 1.0)) * (v + 1) / 2.0
@neoneye
neoneye / Logging.swift
Created March 29, 2019 16:16
SwiftyBeaver using Apple's unified logging system (OSLog)
// Copyright © 2019 Simon Strandgaard. All rights reserved.
import SwiftyBeaver
public let log = SwiftyBeaver.self
extension BaseDestination.LevelColor {
mutating func applyDefaultStyle() {
debug = "🏐 "
info = "🏐 "
verbose = "🏐 "
import Foundation
import PlaygroundSupport
/// A thread-safe array.
public class SynchronizedArray<Element> {
fileprivate let queue = DispatchQueue(label: "io.zamzam.ZamzamKit.SynchronizedArray", attributes: .concurrent)
fileprivate var array = [Element]()
}
// MARK: - Properties
@neoneye
neoneye / routes.swift
Last active June 6, 2018 22:17
self documenting api using Perfect
import PerfectHTTP
class HandlerContext {
let request: HTTPRequest
let response: HTTPResponse
init(request: HTTPRequest, response: HTTPResponse) {
self.request = request
self.response = response
}
@neoneye
neoneye / UIViewController+Child.swift
Created June 6, 2018 08:40
Install child view controller
import UIKit
extension UIViewController {
/// Taken from this great StackOverflow post by Phani Sai: https://stackoverflow.com/a/35473300/78336
func configureChildViewController(_ childController: UIViewController, onView: UIView?) {
var holderView = self.view
if let onView = onView {
holderView = onView
}
addChildViewController(childController)
@neoneye
neoneye / Example.swift
Created March 18, 2018 20:23
Tweakable Values in swift
let value0 = _tv(100)
let value1 = _tv(200)
print("the values are: \(value0) \(value1)")
if _tv(1) > 0.5 {
print("if-case")
} else {
print("else-case")
}
@neoneye
neoneye / backup_database.rb
Created March 11, 2018 11:05
Backup PostgreSQL Database to AWS S3 Storage
#!/usr/bin/env ruby
require 'fileutils'
PATH_PG_DUMP = "/usr/lib/postgresql/9.6/bin/pg_dump"
POSTGRES_HOST = "demoscene.t1qwy5zz3sn3.eu-southwest-3.rds.amazonaws.com"
POSTGRES_PORT = 1234
POSTGRES_USERNAME = "admin"
POSTGRES_PASSWORD = "doesnt look like anything to me"
POSTGRES_DBNAME = "main"
AWS_S3_BUCKET = "there.is.no.spoon"
import XCTest
class URLRelativePathFromTests: XCTestCase {
func process(_ p1: String, _ p2: String) -> String? {
let u1 = URL(fileURLWithPath: p1)
let u2 = URL(fileURLWithPath: p2)
return u1.relativePath(from: u2)
}
func testNormal() {
@neoneye
neoneye / guide.md
Last active November 20, 2022 14:26
Install GraphicsMagick on AWS EC2 running Ubuntu Linux
@neoneye
neoneye / PostgresStORM+DoWithTransaction.swift
Created October 21, 2017 12:08
Unsuccessful experiments getting transactions working with StORM
import PerfectPostgreSQL
import PostgresStORM
import StORM
extension PostgresStORM {
static func doWithTransaction<Result>(closure: () throws -> Result) throws -> Result {
let thisConnection = PostgresConnect(
host: PostgresConnector.host,
username: PostgresConnector.username,
password: PostgresConnector.password,