Skip to content

Instantly share code, notes, and snippets.

View k0nserv's full-sized avatar

Hugo Tunius k0nserv

View GitHub Profile
@k0nserv
k0nserv / open-source-draft.md
Last active August 29, 2015 14:06
Open Source Draft

On July the 7th I went to the CocoaHeads Stockholm meetup as I always try to do when they host one. For this particular meetup the brilliant, but unknown to me at the time, Orta Therox held a talk about open source, CocoaPods and how they work at Artsy. For very long time beforehand I had felt that I was not contributing back enough to all the awesome projects that I used daily, CocoaPods being one of them. Orta really inspired me to take the first steps to contribute back. I promptly went home and started working on a issue I found among the CocoaPods issues on GitHub. After finishing that one I jumped on to another one which was related. After a landing a few PRs Orta invited me to the CocoaPods slack team. It was during a discussion there that the questions for this blog post came up.

The question

In the mentioned the discussion the original questions was:

Why do Github issues

// The DoUntil struct holds the action that is
// the loop body
struct DoUntil {
let action: () -> ()
init(action: () -> ()) {
self.action = action
}
// Using a method like this allows replacing what would have
func onMainThread(action: () -> ()) {
dispatch_async(dispatch_get_main_queue(), action)
}
onMainThread {
myView.hidden = true
}
func inBackground(action: () -> ()) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), action)
// Perform some action 50% of the time
func maybe(action: () -> ()) {
if arc4random_uniform(2) == 0 {
action()
}
}
maybe {
println("Hello World?")
}
// Define the ++= operator
infix operator ++= {}
func ++= (action: @autoclosure () -> (), condition: BooleanType){
unless(condition, action)
}
var x = 1
// This is equal to ruby's
// x = 2 unless 1 > 2
func unless(condition: BooleanType, action: () -> ()) {
if condition.boolValue == false {
action()
}
}
var opt: Int? = 10
unless(opt == nil) {
println("opt was non nil")
}
func _while_without_while(condition: @autoclosure () -> BooleanType,
action: () -> ()) {
var loop: () -> () = { $0 }
loop = {
// The condition is called each time to
// see if the loop should continue
if condition() {
// Then the acutal action is called
action()
(function () {
"use strict";
// Code..
}());
func _while(condition: @autoclosure () -> BooleanType, action: () -> ()) {
while condition() {
action()
}
}
var i = 0
_while(i < 10) {
println("\(i)")
i += 1
@k0nserv
k0nserv / if-without-if.swift
Last active August 29, 2015 14:03
Reimplementing the swift if statement without using the if keyword
func _if_without_if(condition: BooleanType, action: () -> ()) {
// We fake the false case by using an empty closure, e.g a NOP
let actions = [{}, action]
// boolValue returns a Bool and we can abuse the fact that
// hashValue of true is 1 and then hashValue of false is 0 to
// get the correct closure to run
actions[condition.boolValue.hashValue]()
}