Skip to content

Instantly share code, notes, and snippets.

View phlippieb's full-sized avatar
🦆

Phlippie phlippieb

🦆
View GitHub Profile
@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', ...);
@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 / 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?
/*
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
@phlippieb
phlippieb / ResizingTableViewCellDelegate.swift
Created March 15, 2019 06:57
Even when using AutoLayout and UITableView.automaticDimension, it's not obvious how to make your table view resize a cell when its content changes without reloading the entire cell. I've had success with calling beginUpdates() and endUpdates() on the table view after the content changes. This gist simply extracts that into a delegate.
/*
This code demonstrates a way to get UITableViewCells to resize
after their content size has changed, without needing to reload
the cells.
Prerequisites:
- The table view must be set up to calculate its cell heights dynamically; i.e.
tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 100 // or some good estimate
- The cell must be laid out using auto layout. Remember to add the subviews to
/// 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: () -> () = {}