Skip to content

Instantly share code, notes, and snippets.

@lorentey
lorentey / shadow.swift
Last active July 8, 2018 12:20
The most annoying thing about Swift 3's naming conventions is that some renamed members now shadow frequently used global functions:
extension Array where Element: Comparable {
func clamp(from lowerBound: Element, to upperBound: Element) -> Array {
return self.map { min(upperBound, max(lowerBound, $0)) }
}
}
let clamped = [0, 1, 2, 3, 4, 5, 6].clamp(from: 2, to: 4))
@cbess
cbess / AppNotify.swift
Last active August 10, 2019 15:58
Simple NSNotificationCenter wrapper for Swift 4.x
//
// AppNotify.swift
//
// Created by Christopher Bess on 6/30/15.
// MIT License
//
import Foundation
/// Represents the app notification.
@oleganza
oleganza / async_swift_proposal.md
Last active May 12, 2023 10:06
Concrete proposal for async semantics in Swift

Async semantics proposal for Swift

Modern Cocoa development involves a lot of asynchronous programming using blocks and NSOperations. A lot of APIs are exposing blocks and they are more natural to write a lot of logic, so we'll only focus on block-based APIs.

Block-based APIs are hard to use when number of operations grows and dependencies between them become more complicated. In this paper I introduce asynchronous semantics and Promise type to Swift language (borrowing ideas from design of throw-try-catch and optionals). Functions can opt-in to become async, programmer can compose complex logic involving asynchronous operations while compiler produces necessary closures to implement that logic. This proposal does not propose new runtime model, nor "actors" or "coroutines".

Table of contents

@staltz
staltz / introrx.md
Last active April 20, 2024 14:15
The introduction to Reactive Programming you've been missing
@depth42
depth42 / gist:7355346
Created November 7, 2013 14:23
Xcode Server has the problem that one can only access the build log files once the build or test process has finished. But as it can occur that tests hang, it is hard to analyze why and where the hang actually happened. This little shell script helps debugging by showing the log output of the currently running bot.
#!/bin/bash
# Shell script to be executed on a Mavericks Server runing Xcode Server using sudo
BOTRUN_DATA=/Library/Server/Xcode/Data/BotRuns
LATEST_RUN=`ls -tr $BOTRUN_DATA | grep BotRun- | tail -n1`
tail -f ${BOTRUN_DATA}/${LATEST_RUN}/output/build.log
@DiegoSalazar
DiegoSalazar / validate_credit_card.js
Last active February 12, 2024 05:09
Luhn algorithm in Javascript. Check valid credit card numbers
// Takes a credit card string value and returns true on valid number
function valid_credit_card(value) {
// Accept only digits, dashes or spaces
if (/[^0-9-\s]+/.test(value)) return false;
// The Luhn Algorithm. It's so pretty.
let nCheck = 0, bEven = false;
value = value.replace(/\D/g, "");
for (var n = value.length - 1; n >= 0; n--) {