Skip to content

Instantly share code, notes, and snippets.

View masters3d's full-sized avatar

masters3d masters3d

  • Pacific North West
View GitHub Profile
@dstaley
dstaley / fizzbuzz.swift
Last active February 5, 2019 21:19
FizzBuzz in Swift using pattern matching
func fizzbuzz(i: Int) -> String {
let result = (i % 3, i % 5)
switch result {
case (0, _):
return "Fizz"
case (_, 0):
return "Buzz"
case (0, 0):
return "FizzBuzz"
default:
@mchambers
mchambers / reflect.swift
Last active March 5, 2021 09:20
Basic Reflection in Swift.
// Let's define a basic Swift class.
class Fruit {
var type=1
var name="Apple"
var delicious=true
}
// We can get at some info about an instance of an object using reflect(), which returns a Mirror.
reflect(Fruit()).count
reflect(Fruit())[1].0
func add(a: Int, b: Int) -> Int {
return a + b
}
add(1, 2)
func add(a: Int, #b: Int) -> Int {
return a + b
}
@wanghailei
wanghailei / csallseeingeye.py
Last active July 16, 2024 12:27
Code snippets of All Seeing Eye
# The following code and the code generated art works are the intellectrual properities of Hailei Wang.
# © 2009 - 2014, Hailei Wang. All rights reserved.
from nodebox import geo
colors = ximport("colors")
# Define Brush
def composeimage( x, y, colr, radius, points, diminish ) :
nofill()
stroke()
@wanghailei
wanghailei / bbideolabs.py
Created June 6, 2014 03:51
Code snippets of BB IDEO LABS
# The following code and the code generated art works are the intellectrual properities of Hailei Wang.
# © 2010 - 2014, Hailei Wang. All rights reserved.
colors = ximport( "colors" )
font( "Courier", 200 )
align( CENTER )
text_path_line_1 = textpath( "IDEO", 0, 200, width = WIDTH)
text_path_line_2 = textpath( "LABS", 0, 350, width = WIDTH)
@tomekc
tomekc / Optional+getOrElse.swift
Last active October 28, 2016 17:29
Add .getOrElse() method to Swift's Optional<T>, to unwrap value with fallback to default value. Idea borrowed from Scala language.
// An extension to Optional type:
// getOrElse() will return optional's value if exists, otherwise the default value provided as argument will be returned.
//
// Note: Since Xcode6 beta 5, you can use '??' operator.
//
// (c) Tomek Cejner 2014
// @tomekcejner
extension Optional {
func getOrElse(val:T) -> T {
if self != nil {
@staltz
staltz / introrx.md
Last active August 15, 2025 20:30
The introduction to Reactive Programming you've been missing
@ariok
ariok / [swift]ArrayExtension.swift
Created June 27, 2014 14:56
[swift]Adding support for "indexOf:" and "contains:" to Array
extension Array {
func contains(object:AnyObject) -> Bool {
return self.bridgeToObjectiveC().containsObject(object)
}
func indexOf(object:AnyObject) -> Int {
return self.bridgeToObjectiveC().indexOfObject(object)
}
}
import Darwin
import CoreGraphics
protocol ScalarFunctions {
var acos:Double {get}
var asin:Double {get}
var atan:Double {get}
func atan2(x:Double) -> Double
var cos:Double {get}
var sin:Double {get}
@kareman
kareman / Queue.swift
Last active July 31, 2020 19:16
A standard queue (FIFO - First In First Out) implemented in Swift. Supports simultaneous adding and removing, but only one item can be added at a time, and only one item can be removed at a time. Using the "Two-Lock Concurrent Queue Algorithm" from http://www.cs.rochester.edu/research/synchronization/pseudocode/queues.html#tlq, without the locks.
//
// Queue.swift
// NTBSwift
//
// Created by Kåre Morstøl on 11/07/14.
//
// Using the "Two-Lock Concurrent Queue Algorithm" from http://www.cs.rochester.edu/research/synchronization/pseudocode/queues.html#tlq, without the locks.
// should be an inner class of Queue, but inner classes and generics crash the compiler, SourceKit (repeatedly) and occasionally XCode.