Skip to content

Instantly share code, notes, and snippets.

@Nadohs
Nadohs / gist:304b0da0548752dd4230
Created October 25, 2015 00:19
arr&[-1] array accessor that checks if index is out of range, and returns an optional....
let arr = [1,2,3,4]
func & <T>(lhs:Array<T>,rhs:[Int]) -> T?{
guard let index = rhs.first else{
print("no index!")
return nil
}
if index < 0{
print("negative index")

###Subject: isEqual to replace == Equatable Requirement

Hi all,

I would like to propose changing the Equatable protocol to use isEqual(to:Self) -> Bool, defined inside of a type to replace the currently used operator == function, which is defined outside of the type.

###Reasoning:

  1. Having the conforming function defined inside of the type is more intuitive, since in general functions required for conformance are defined within the type. It feels like an unnecesary detail for learners of Swift to have to stumble through.
@Nadohs
Nadohs / gist:efe3b136b3e59765c667
Last active December 30, 2015 07:09
curried function with generics...
private func comparableAttributes<T, U:Equatable>(first f:T, second s:T)->(T->U) -> U?{
    return { trans in
        let x = trans(f)
        let y = trans(s)
        return (x == y) ? nil : x
    }
}

If i do this it doesnt work...

@Nadohs
Nadohs / if let quickER.md
Last active December 30, 2015 07:09
if-let operator syntax using flatmap
infix operator ?= {  precedence 90 }

func ?= <T> (inout f: T, optional:T?){
    let _  = optional.flatMap{f = $0}
}

Usage looks like:

store.name ?= json["name"]?.string
@Nadohs
Nadohs / EnumConvertible.md
Last active December 30, 2015 07:08
EnumConvertible

Example Enums:

enum SessionChannel: Int {
    case NeedsAuthentication = 0
    case VerifiedUser = 1
}


enum SessionEvent {

Try writing this with both an option field and a hanging animation closure, with no completion...

        UIView.animateWithDuration(0.2, delay: 0, options: .CurveEaseOut, animations: { 
          ///DO ANIMATION STUFF
        }, completion: nil)
        

I should beable to do this

#todo sudoku ui
from sudoku_engine import Board
from guizero import App, PushButton
class SudView:
board_code = None
board_solution = None
app_ref = None
// Single tab item view
struct TabItemView: View {
// @State private var isSelected: Bool
let imgName: String
let title: String
let isSelected: Bool
let action:()->Void
init(img:String, title:String, isSelected:Bool=false, action: @escaping ()->Void={}) {
self.imgName = img
@Nadohs
Nadohs / gist:ba7f427205d930c5e58e500b753a684c
Last active April 18, 2024 16:03
constaints_helper.swift
import Foundation
import UIKit
extension UIButton {
func setImage(named: String) {
if let image = UIImage(named: named) {
self.setImage(image, for: .normal)
}
}
}