Skip to content

Instantly share code, notes, and snippets.

View dnedrow's full-sized avatar

David Nedrow dnedrow

View GitHub Profile
@dnedrow
dnedrow / IntExtensions.cs
Last active September 21, 2020 00:20
C# extensions for converting between UInt32 and UInt16
/// <summary>
/// Joins two 16bit integers to a single 32bit value.
/// </summary>
/// <param name="int1">The integer to use for the high bits.</param>
/// <param name="int2">The integer to use for the low bits.</param>
/// <returns>An unsigned 32bit integer.</returns>
public static UInt32 Join(this UInt16 int1, UInt16 int2)
{
return (uint) ((int1 << 16) | int2);
}
@dnedrow
dnedrow / BindablePropertyLiveTemplate.cs
Created July 27, 2020 02:11
Rider live template for BindableProperty
// This is a live template for the JetBrains Rider IDE. It creates the boilerplat BindableProperty code.
// In the C# section of Live Templates, add a new template. I called it "bindProp", but you can give it whatever
// name you'll remember. Edit the variables and ensure that the order is 1) propertyName, 2) propertType, and 3) className.
// Set the className variable to "Not editable" and change its macro to "Containing type name".
// Now, typing bindProp will offer the Live Template you created as a completion option.
public static readonly BindableProperty $propertyName$Property = BindableProperty.Create(nameof($propertyName$), typeof($propertyType$), typeof($className$), $END$); public $propertyType$ $propertyName$
{
get => ($propertyType$)GetValue($className$.$propertyName$Property);
set => SetValue($className$.$propertyName$Property, value);
@dnedrow
dnedrow / Bool+Integer.swift
Last active June 3, 2020 15:03
A swift extension on Bool that enables the use of Ints for boolean operations.
// Bool+Integer.swift
// Attribution 4.0 International (CC 4.0), see bottom for license details.
// https://gist.github.com/dnedrow/de0eb8869e3909df595590a7e2404b47
import Foundation
/// This extension enables the following behavior:
/// let x = Bool(5) // true
/// let y = Bool(0) // false
/// let z = Bool(-1) // false
@dnedrow
dnedrow / Queue.swift
Last active May 27, 2020 15:24
Boilerplate queue implementation.
//
// Queue.swift
// StackQueueVisualDemo
//
// Created by Alexey Danilov on 23.05.2018.
// Copyright © 2018 danilovdev. All rights reserved.
//
// FIFO - First In First Out
struct Queue<T> {
@dnedrow
dnedrow / Stack.swift
Last active May 27, 2020 15:25
Standard stack implementation in Swift
//
// Stack.swift
// StackQueueVisualDemo
//
// Created by Alexey Danilov on 23.05.2018.
// Copyright © 2018 danilovdev. All rights reserved.
//
// LIFO - Last In First Out
struct Stack<T> {
@dnedrow
dnedrow / target.c
Last active April 2, 2020 14:49
Example for checking target platform in C
#ifdef _WIN64
//define something for Windows (64-bit)
#elif _WIN32
//define something for Windows (32-bit)
#elif __APPLE__
#include "TargetConditionals.h"
#if TARGET_OS_IPHONE && TARGET_IPHONE_SIMULATOR
// define something for simulator
#elif TARGET_OS_IPHONE
// define something for iphone
@dnedrow
dnedrow / FailableInit.swift
Last active March 16, 2020 12:31
Example of a Swift optional (failable) init
struct ProductFitInfo: SimpleViewModel, Hashable {
let message: String?
let imageName: String?
public init?(attribute: FitAttribute, imageName: String? = nil) {
let suffix = String(describing: attribute)
if suffix.isEmpty {
return nil
}
self.message = Constants.FitRecommendation.Strings.predicate + " " + suffix
@dnedrow
dnedrow / VarWithBlock.swift
Last active March 16, 2020 12:25
Example of using a block to set a variable in Swift.
self.fitLabel.isHidden = { (message: String?) -> Bool in
guard let thisMessage = message else {
return true
}
self.fitLabel.text = thisMessage
self.fitLabel.textAlignment = self.traitCollection.horizontalSizeClass == .regular ? .right : .left
return false
}(vm.fitInfo?.message)
// Comparable+Extensions.swift
// Attribution 4.0 International (CC 4.0), see bottom for license details.
import Foundation
extension Comparable {
func clamp<T: Comparable>(lower: T, _ upper: T) -> T {
assert(lower <= upper)
return min(max(self as! T, lower), upper)
@dnedrow
dnedrow / Array+Utility.swift
Last active March 3, 2020 13:23
Array utility routines.
// Array+Extensions.swift
// Attribution 4.0 International (CC 4.0), see bottom for license details.
import Foundation
extension Array where Element == String? {
// converts an array of optional accessibility labels to a single accessibility label that can be set on an accessibility element
var convertedToAccessibilityLabel: String {
return self.compactMap { $0 }.joined(separator: ", ")