Skip to content

Instantly share code, notes, and snippets.

View alemar11's full-sized avatar

Alessandro alemar11

View GitHub Profile
@alemar11
alemar11 / Random.example.swift
Created March 23, 2017 11:51 — forked from pyrtsa/Random.example.swift
Sampling random numbers in Swift
import Random
random(0 ..< 10) // Int
random(1 ... 1000_000) // Int
random(-1000_000 ... 1000_000) // Int
random(Int.min ... Int.max) // Int
randomMax(UInt64.max) // UInt64
random(0 ... UInt64.max) // UInt64
random(1 ... UInt(10)) // UInt
@alemar11
alemar11 / PhonecallReceiver.java
Created March 24, 2017 09:12 — forked from ftvs/PhonecallReceiver.java
Detecting an incoming call coming to an Android device. Remember to set the appropriate permissions in AndroidManifest.xml as suggested in the Stackoverflow link. Usage example in comments. Source: http://stackoverflow.com/a/15564021/264619 Explanation: http://gabesechansoftware.com/is-the-phone-ringing/#more-8
package com.gabesechan.android.reusable.receivers;
import java.util.Date;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
public abstract class PhonecallReceiver extends BroadcastReceiver {
@alemar11
alemar11 / RandomNumbers.swift
Created April 20, 2017 20:08 — forked from jstn/RandomNumbers.swift
generate random numbers for 64-bit types while mitigating modulo bias
/*
`arc4random_uniform` is very useful but limited to `UInt32`.
This defines a generic version of `arc4random` for any type
expressible by an integer literal, and extends some numeric
types with a `random` method that mitigates for modulo bias
in the same manner as `arc4random`.
`lower` is inclusive and `upper` is exclusive, thus:
#!/usr/bin/ruby
# Create display override file to force Mac OS X to use RGB mode for Display
# see http://embdev.net/topic/284710
require 'base64'
data=`ioreg -l -d0 -w 0 -r -c AppleDisplay`
edids=data.scan(/IODisplayEDID.*?<([a-z0-9]+)>/i).flatten
vendorids=data.scan(/DisplayVendorID.*?([0-9]+)/i).flatten
@alemar11
alemar11 / lenses-and-prisms-in-swift-a-pragmatic-approach.swift
Created May 17, 2017 07:30 — forked from broomburgo/lenses-and-prisms-in-swift-a-pragmatic-approach.swift
code for the article "Lenses and Prisms in Swift: a pragmatic approach"
/// source: https://broomburgo.github.io/fun-ios/post/lenses-and-prisms-in-swift-a-pragmatic-approach/
protocol LensType {
associatedtype WholeType
associatedtype PartType
var get: (WholeType) -> PartType { get }
var set: (PartType,WholeType) -> WholeType { get }
init(get: @escaping (WholeType) -> PartType, set: @escaping (PartType,WholeType) -> WholeType)
@alemar11
alemar11 / MultiDirectionAdjudicatingScrollView.swift
Created July 15, 2017 08:33 — forked from andymatuschak/MultiDirectionAdjudicatingScrollView.swift
Source for the Khan Academy app's unusual scrolling interactions
//
// MultiDirectionAdjudicatingScrollView.swift
// Khan Academy
//
// Created by Andy Matuschak on 12/16/14.
// Copyright (c) 2014 Khan Academy. All rights reserved.
//
import UIKit
import UIKit.UIGestureRecognizerSubclass
public func weakify <T: AnyObject>(_ object: T) -> T? {
return { [weak object] in return object }()
}
@alemar11
alemar11 / async-operation.swift
Created August 4, 2017 15:25 — forked from pgherveou/async-operation.swift
Async operationQueue
import Foundation
/// List of states that can be used to update an asynchronous operation state.
public enum AsyncOperationState {
case ready
case executing
case finished
fileprivate var keyPath: String {
switch self {
@alemar11
alemar11 / Array+Equatable.swift
Created September 1, 2017 11:00
Array+Equatable
public func ==<T: Equatable>(lhs: [T]?, rhs: [T]?) -> Bool {
switch (lhs,rhs) {
case (.some(let lhs), .some(let rhs)):
return lhs == rhs
case (.none, .none):
return true
default:
return false
}
}
@alemar11
alemar11 / CustomFontMetrics.swift
Created September 5, 2017 13:32 — forked from zwaldowski/CustomFontMetrics.swift
[WIP] UIFontMetrics Backport from iOS 11
//
// CustomFontMetrics.swift
//
// Created by Zachary Waldowski on 6/6/17.
// Licensed under MIT.
//
import UIKit
private extension UITraitCollection {