View swift.rb
# -*- coding: utf-8 -*- #
module Rouge
module Lexers
class Swift < RegexLexer
tag 'swift'
filenames '*.swift'
title "Swift"
View mash.swift
enum Enum {
case foo(Int)
case bar(String)
case snog(Int)
}
let items: [Enum] = [.foo(1), .bar("hi"), .foo(2)]
let filtered = items.filter({ switch $0 { case .foo: return true; default: return false } })
View AssociativeArray.swift
import Foundation
/// A "collection" of paired items
public struct AssociativeArray<PairedItem: Hashable>: CustomStringConvertible, ExpressibleByDictionaryLiteral {
/// Constructable outside module
public init() {}
public typealias DictForm = Dictionary<PairedItem, PairedItem>
View Approach1.swift
// Returns `URL?`
public enum SocketEndPoint: String {
case eventSocket = "http://nowhere.com/events"
case liveSocket = "http://nowhere.com/live"
public var url: URL? {
return URL(string: self.rawValue)
}
}
View after.swift
fileprivate func chargeState(for battery: BatteryService)
-> ChargeState
{
switch (battery, isCalculating(battery)) {
case (.isACPowered, true):
return .calculating(isDischarging: false)
case (.isACPowered, _) where battery.isCharging:
return .charging
case (.isACPowered, _):
return .power //bypasses error
View gist:52a0d386682b595db102c60c6c4bcb3e
@UIApplicationMain class AppDelegate: UIResponder,
UIApplicationDelegate, UINavigationControllerDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
if let window = window {
let vc = MyViewController(nibName: nil, bundle: nil)
let nav = UINavigationController(rootViewController: vc)
window.rootViewController = nav
window.makeKeyAndVisible()
View sideeffect.swift
import Foundation
public final class Singleton {
private static let _shared = Singleton()
private init() { }
public static func shared() -> Singleton {
print("Accessed")
return _shared
View gist:ba8d60ec26c5459b9d3b4330da926c07
import Foundation
/// Prints the elapsed time to execute a block under whatever optimization
/// conditions are currently in use by the compiler
public func timetest(_ note: String, block: () -> Void) {
print("Starting Test:", note)
let now = ProcessInfo().systemUptime
block()
let timeInterval = ProcessInfo().systemUptime - now
print("Ending Test:", note); print("Elapsed time: \(timeInterval)")
View CozmoTriggers.py
from Cozmo import *
triggerActions = {"AcknowledgeFaceInitPause" : cozmo.anim.Triggers.AcknowledgeFaceInitPause,
"AcknowledgeFaceNamed" : cozmo.anim.Triggers.AcknowledgeFaceNamed,
"AcknowledgeFaceUnnamed" : cozmo.anim.Triggers.AcknowledgeFaceUnnamed,
"AcknowledgeObject" : cozmo.anim.Triggers.AcknowledgeObject,
"AskToBeRightedLeft" : cozmo.anim.Triggers.AskToBeRightedLeft,
"AskToBeRightedRight" : cozmo.anim.Triggers.AskToBeRightedRight,
"BlockReact" : cozmo.anim.Triggers.BlockReact,
"BuildPyramidReactToBase" : cozmo.anim.Triggers.BuildPyramidReactToBase,
View 1_Cozmo.py
import sys
from enum import *
import cozmo
from cozmo.util import degrees
from Triggers import *
# Log Error
def error(message):
print("*********************ERROR:", message)