Skip to content

Instantly share code, notes, and snippets.

View phlippieb's full-sized avatar
🦆

Phlippie phlippieb

🦆
View GitHub Profile
@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

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 / 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,
@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', ...);
/*
For use in an XCode Playground
*/
import Foundation
import XCPlayground
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
// Method 1: Works
# To get this:
# curl -Lo $HOME/.screenrc 'https://goo.gl/bZv3x2'
# Enable mouse scrolling:
termcapinfo xterm* ti@:te@
# Clear screen when vim/less/etc quits:
altscreen on
# Status line that shows windows:
@phlippieb
phlippieb / LightRibbonView.swift
Last active April 16, 2018 07:15
A UIView subclass that draws an animated light ribbon.
//
// LightRibbon2.swift
// Waveforms
//
// Created by Phlippie Bosman on 2018/04/12.
// Copyright © 2018 Phlippie Bosman. All rights reserved.
//
import UIKit
/// Synched operations will only run one at a time.
/// The `operation` method is responsible for calling `complete` to notify the object when it is done.
final class SyncedOp {
// Use a GCD Semaphore to synchronize flag access.
private let semaphore = DispatchSemaphore(value: 1)
private var isBusy = false
private var isRetriggerQueued = false
internal var operation: () -> () = {}
//
// TableviewDatasource.swift
//
// Created by Phlippie Bosman on 2019/01/30.
// Copyright © 2019 Kalido. All rights reserved.
//
import Reusable
import SwiftLCS
// Say you have a generic class with a couple of generic types,
// that also takes a couple of parameters in its initializer:
class Foo<T, U, V> {
init(label: String, other: Any) {
// ...
}
}
// Instantiating objects of this class in the standard way, while not unusual,