Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View lilyball's full-sized avatar

Lily Ballard lilyball

View GitHub Profile
@lilyball
lilyball / 4button-pico-bp.yaml
Last active March 21, 2023 05:10 — forked from crkochan/4button-pico-bp.yaml
Home Assistant Blueprint
blueprint:
name: Lutron Caseta Four-Button Zone Pico Actions
description: Short and long press automations for the Pico four-button remote.
domain: automation
input:
pico_remote:
name: Pico Four-Button Zone Remote
description: Select the Pico four-button zone remote to configure.
selector:
device:
public enum TruncateMode {
case Head, Middle, Tail
}
public extension String {
/// Truncates a string to a maximum length in grapheme clusters using the
/// specified mode. If `addEllipsis` is `true`, the resulting string
/// including the ellipsis will be no longer than `maxLength`.
func truncateToLength(maxLength: Int, mode: TruncateMode = .Tail, addEllipsis: Bool = false) -> String {
if maxLength <= 0 {
extension Optional {
func and<U>(x:U?) -> (T, U)? {
switch (self, x) {
case let (.Some(a), .Some(b)):
return (a, b)
default:
return nil
}
}
}

Challenge #2

Implement a take_while method on Array

take_while runs the block with each element of the Array in turn until it returns false, then it stops and returns an Array of the prior elements.

Example

  Input >> [1,2,3,4].take_while { $0 < 3 }
 Output &gt;&gt; R8: Array = 2 values {
@lilyball
lilyball / left-view.swift
Last active August 29, 2015 14:04 — forked from tel/left-view
extension String {
static let empty: String = String()
func uncons() -> (Character, String)? {
if self.isEmpty {
return nil
}
return (self[startIndex], dropFirst(self))
}
protocol Delegate {
typealias GenerateType
func generate() -> GenerateType
}
class Controller<D: Delegate> {
var delegate: D
}
@lilyball
lilyball / foo.swift
Created July 28, 2014 02:24 — forked from bryanl/foo.m
func viewController(viewController: GTMOAuth2ViewControllerTouch!, finishedWithAuth auth: GTMOAuth2Authentication!, error: NSError!) {
if error {
// authentication failed
} else {
// authentication succeeded
}
}
// From documentation...
- (NSUInteger)numberOfBarsInBarChartView:(JBBarChartView *)barChartView
{
return ...; // number of bars in chart
}
// my attempt
func numberOfBarsInBarChartView(_ barChartView: AnyObject!) -> UInt {
return 72
import Cocoa
class Test : NSWindowController {
var foo: String
init(window:NSWindow!) {
self.foo = "foo"
super.init(window:window)
}
struct OddNumbersTimesFiveGenerator<S : Sequence where S.GeneratorType.Element == Int> : Generator {
var elements : S.GeneratorType
mutating func next() -> Int? {
while true {
switch elements.next() {
case let .Some(x) where x % 2 == 0: continue
case let .Some(x): return x * 5
case .None: return nil
}