Skip to content

Instantly share code, notes, and snippets.

View phlippieb's full-sized avatar
🦆

Phlippie phlippieb

🦆
View GitHub Profile
/*
For use in an XCode Playground
*/
import Foundation
import XCPlayground
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
// Method 1: Works
@phlippieb
phlippieb / MaxHeightTextView.swift
Created January 25, 2017 05:35
`MaxHeightTextView` overrides `UITextView`'s `intrinsicContentSize` and `layoutSubviews` to allow it to grow to fit its content size, but only up to a specified max height. Once the text view reaches this max height, its content will start to scroll. Use with AutoLayout. Specifically, set up a fixed width, and two height constraints with greater…
//
// File.swift
// Kalido
//
// Created by Phlippie Bosman on 2016/11/17.
// Copyright © 2016 Kalido. All rights reserved.
//
class MaxHeightTextView: UITextView {
open var maxHeight: CGFloat?
@phlippieb
phlippieb / RangeTappableTextView.swift
Last active November 23, 2016 14:00
A UITextView subclass that allows clients to register ranges of characters as "tappable", with keys to identify those ranges. A delegate can be assigned to receive callbacks when tappable ranges are tapped. When a user taps on a character in the text view which is in a registered range (with a registered key), the delegate's callback is called w…
//
// RangeTappableTextView.swift
// Kalido
//
// Created by Phlippie Bosman on 2016/11/23.
// Copyright © 2016 Kalido. All rights reserved.
//
protocol RangeTappableTextViewDelegate: class {
func rangeTappableTextView(_ rangeTappableTextView: RangeTappableTextView,

This naive algorithm compares two strings, needle and haystack. It returns true if haystack "fuzzily" contains needle, which is when (almost) every character in needle is found in (almost) the same order, but not necessarily contiguously, in haystack. Up to allowed_errors characters in needle are allowed to not be found in haystack. Characters may also be swapped.

The following is a python implementation:

def stringFuzzilyContains(needle, haystack):
    # allow this many characters to not be found
    allowed_errors = 0
    
    # the last index where a character was found;
@phlippieb
phlippieb / AutoCompleteTextView.swift
Last active August 24, 2016 06:58
A swift autocomplete solution. I needed this to subclass a specific textview class (from the Material pod, if you're interested), but it should work fine with UITextView. I also used RxSwift to capture text changes, which can probably be done with the text view's delegate methods, though it might be harder.
//
// AutoCompleteTextField.swift
//
// Created by Phlippie Bosman on 2016/08/23.
// Copyright © 2016 Kalido. All rights reserved.
//
import Material
import RxSwift
@phlippieb
phlippieb / bsr-scripts.R
Last active December 9, 2016 13:11
R scripts for linear approximations on data points and data frames, based on a solution from this thread: http://r.789695.n4.nabble.com/piecewise-linear-approximation-td833371.html
plot.full <- function (datafile, ...)
{
data <- read.table( datafile, quote="\"" );
dataframe <- data.frame(x=1:length(rowMeans(data)), y=rowMeans(data));
plot(dataframe, type='l');
b <- bsr2 (dataframe$y, dataframe$x, 100);
slope <- diff( b$fit$model[,2] ) / diff ( predict(b$fit) );
plot.2pwla(dataframe$x, dataframe$y, cont=FALSE, add=TRUE, type='l', col='red', ...);