Skip to content

Instantly share code, notes, and snippets.

View callionica's full-sized avatar
🏠
Working from home

Callionica callionica

🏠
Working from home
View GitHub Profile
// C# equivalent of buckets.swift
using System;
using System.Collections.Generic;
interface IFeed {
String url { get; }
}
class LocalFeed : IFeed {
public String url { get; set; }
// Protocol
protocol Feed {
var url : String { get }
}
// Concrete classes
class LocalFeed : Feed {
var url : String = "file://Local"
}
// Types work but are not optimal - no error handling
// Demo in response to https://gist.github.com/sketchytech/029c00a74a4217a89797
extension CollectionType {
func reorder<T>(sorter: [(index: Self.Index, element: T)]) -> Array<Self.Generator.Element> {
return sorter.map({self[$0.index]})
}
}
func sortColumns<T0, T1>(_ col0: Array<T0>, _ col1: Array<T1>, _ cmp: (T0,T0)->Bool) -> (Array<T0>, Array<T1>) {
var sizes = [
// [ 640, 960, "phone-35"],
[ 640, 1136, "phone-40"],
[ 750, 1334, "phone-47"],
[1242, 2208, "phone-55"],
// [ 768, 1024, "pad-x1"],
[1536, 2048, "pad-x2"],
[2048, 2732, "pad-pro"],
];
// This is just some noodling based on Brent's interest in Swift responder chains: http://inessential.com/2016/05/15/a_hypothetical_responder_chain_written_i
// Just showing that responder chain can easily fit with the level of dynamism in Swift today
infix operator >>> { associativity left }
func >>> <In, Middle, Out>(first: In -> Middle, second: Middle -> Out) -> In -> Out {
return { x in second(first(x)) }
}
func >>> <In, Out>(deduce: In.Type, fn: In -> Out) -> In -> Out {
// Code generation!
let props = ["name", "value", "thing"]
for prop in props {
print([
"if local.\(prop) != remote.\(prop) {",
" local.\(prop) = remote.\(prop)",
" changes[\(prop)Key] = remote.\(prop)",
"}",
// Dynamic cast without bridging
func dynamicCast<T>(_ type: T.Type, _ v: Any)->T? {
guard let result = v as? T where v.dynamicType is T.Type else {
return nil;
}
return result
}
// Return a 2uple if both objects are convertible to type T, otherwise nil
func matchCast<T>(_ type: T.Type, _ l: Any, _ r: Any)->(T, T)? {
template <class T>
inline T* as(NSObject* o) {
if ([o isKindOfClass: [T class]]) {
return (T*)o;
}
return nil;
}
template <class T>
inline bool is(NSObject* o) {
@callionica
callionica / optional-call-avoidance.swift
Created December 13, 2016 19:18
let result = fn<-?(a, b)
// Swift Ver. 3.0.1 (Release)
// Platform: Linux (x86_64)
// Noodling with syntax for call avoidance with optional arguments
// Just to get a sense of what it could look like
// Given a function "fn" that takes 2 non-optional arguments
// We can enable this syntax:
// let result = fn<-?(a, b)
// where a and b are optionals
@callionica
callionica / cpp.json
Last active February 14, 2017 19:29
VSCode Snippets for C++ from Callionica --- Visual Studio Code - VS Code - VSCode
{
"include": {
"prefix": "#inc",
"body": [
"#include \"$1\"",
"$0"
],
"description": "#include \"\""
}
,"include-system": {