Skip to content

Instantly share code, notes, and snippets.

View blixt's full-sized avatar
🚧
???

Blixt blixt

🚧
???
View GitHub Profile
@blixt
blixt / logger_middleware.go
Last active July 5, 2024 22:02
Logger middleware for Go HTTP servers which logs every request with response status code in the Apache format.
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
"time"
)
@discardableResult
public func then<U>(_ onFulfilled: @escaping (T) throws -> U) -> Promise<U> {
return self.thenImpl(onFulfilled, { throw $0 })
}
@discardableResult
public func then<U>(_ onFulfilled: @escaping (T) throws -> U, _ onRejected: @escaping (Error) throws -> U) -> Promise<U> {
return self.thenImpl(onFulfilled, onRejected)
}
@discardableResult
@blixt
blixt / nonblocking.go
Last active April 25, 2016 03:58
Non-blocking reader for Go. Probably a bad idea.
package nonblocking
import (
"io"
"time"
)
type NonBlockingReader struct {
ch chan []byte
rd io.Reader
@blixt
blixt / multipart.go
Created April 24, 2016 03:49
The multipart reader hangs instead of returning the available parts.
package main
import (
"fmt"
"io"
"math"
"math/rand"
"mime/multipart"
"time"
)
@blixt
blixt / LocalizeLists.swift
Created April 7, 2016 22:40
Localized lists in Swift
extension SequenceType where Generator.Element == String {
func localizedJoin() -> String {
var g = self.generate()
guard let first = g.next() else {
return ""
}
guard let second = g.next() else {
return first
}
guard var last = g.next() else {
@blixt
blixt / Segfault.swift
Last active February 2, 2016 23:32
Swift segfaulting
struct Participant {
let identifiers: [(label: String?, value: String)]
}
// This segfaults. Combining the two maps or changing "" to nil works.
let participants = ["a", "b", "c"]
.map { ("", $0) }
.map { Participant(identifiers: [$0]) }
// Update: Here's an error from a later build of Swift which explains the error.
@blixt
blixt / flatMap.swift
Last active December 16, 2015 21:26
Using flatMap to convert a list of mixed nils and values into a list of non-optional values
let numbers = [
"one",
"2",
"0x3",
"42",
]
// This will run the function on all values and only keep the ones that are not nil:
let parsedNumbers = numbers.flatMap { Int($0, radix: 10) }
// [2, 42]
@blixt
blixt / xml.swift
Last active May 19, 2018 10:42
Using indirect enums to create a simplified XML generator in just a few lines of code
indirect enum Node {
case tag(String, [Node])
case text(String)
}
extension Node: CustomStringConvertible {
var description: String {
switch self {
case let .tag(name, children):
if children.count == 1, case let .some(.text(text)) = children.first {
@blixt
blixt / NSURL_Extensions.swift
Created December 1, 2015 17:56
Parse query string from NSURL
// Example:
let url = NSURL(string: "https://example.com/hello?bla=one&bla=two&foo&bar=1")!
let values = url.parseQueryString()
// Result: ["bla": ["one", "two"], "bar": ["1"], "foo": []]
extension NSURL {
func parseQueryString() -> [String: [String]]? {
guard let items = NSURLComponents(URL: self, resolvingAgainstBaseURL: false)?.queryItems else {
return nil
}
@blixt
blixt / point_distance.py
Created December 1, 2015 17:03
Distance between lat/lon geo point
def point_distance(a, b):
degrees_to_radians = math.pi / 180
phi_a = (90 - a.lat) * degrees_to_radians
phi_b = (90 - b.lat) * degrees_to_radians
theta_a = a.lon * degrees_to_radians
theta_b = b.lon * degrees_to_radians
cos = (math.sin(phi_a) * math.sin(phi_b) * math.cos(theta_a - theta_b) +
math.cos(phi_a) * math.cos(phi_b))
arc = math.acos(cos)
return arc