Skip to content

Instantly share code, notes, and snippets.

@ningsuhen
ningsuhen / Regex.swift
Last active April 27, 2019 18:39
Swift extension for Native String class to support Regex match and Regex replace. Credit - http://www.swift-studies.com/blog/2014/6/12/regex-matching-and-template-replacement-operators-in-swift
import Foundation
struct Regex {
var pattern: String {
didSet {
updateRegex()
}
}
var expressionOptions: NSRegularExpressionOptions {
didSet {
@radex
radex / lexer.swift
Last active February 15, 2017 13:08
Wrote a little lexer/tokenizer for fun. (Warning: I have no idea what I'm doing)
import Foundation
struct Stream {
let string: NSString
var position: Int
var matchingRange: NSRange {
return NSRange(location: position, length: string.length - position)
}
}
@mbrandonw
mbrandonw / 1-Functor-and-Monad.md
Last active June 4, 2022 02:12
Swift Functor and Monad

Copy and paste the swift code below into a playground to experiment.

This is a very close emulation of Functor and Monad typeclasses in swift. However, it is very fragile (i.e. easy to crash the compiler).

For example, instance methods of fmap will run fine, but attempting to use a globally defined fmap that acts on Functor types will cause a crash. Similarly for bind. Unfortunately this means we cannot define the nice infix operator versions of these functions.

@sebmarkbage
sebmarkbage / react_legacyfactory.md
Last active March 15, 2020 00:32
Use a factory or JSX

React Element Factories and JSX

You probably came here because your code is calling your component as a plain function call. This is now deprecated:

var MyComponent = require('MyComponent');

function render() {
 return MyComponent({ foo: 'bar' }); // WARNING
@mattpodwysocki
mattpodwysocki / eventsource.js
Last active September 23, 2019 15:34
Adding support for server-sent events for RxJS
if (!!root.EventSource) {
/**
* This method wraps an EventSource as an observable sequence.
* @param {String} url The url of the server-side script.
* @param {Observer} [openObserver] An optional observer for the 'open' event for the server side event.
* @returns {Observable} An observable sequence which represents the data from a server-side event.
*/
dom.fromEventSource = function (url, openObserver) {
return new AnonymousObservable(function (observer) {
@irace
irace / gist:927f843cb0f683a7f2c2
Created August 13, 2014 20:03
New iOS 8 APIs don't allow you to use a custom presentation controller for compact devices, in conjunction with a popover on "normal" size class devices
//
// ViewController.m
// PopoverPresentationControllerExample
//
// Created by Bryan Irace on 8/8/14.
// Copyright (c) 2014 Bryan Irace. All rights reserved.
//
#import "CustomCompactPresentationController.h"
#import "ViewController.h"
@infix func == <T:ScalarArithmetic, U:ScalarArithmetic> (lhs:T,rhs:U) -> Bool {
return (lhs.toDouble == rhs.toDouble)
}
@infix func != <T:ScalarArithmetic, U:ScalarArithmetic> (lhs:T,rhs:U) -> Bool {
return (lhs == rhs) == false
}
@infix func <= <T:ScalarArithmetic, U:ScalarArithmetic> (lhs:T,rhs:U) -> Bool {
return (lhs.toDouble <= rhs.toDouble)
}
@infix func < <T:ScalarArithmetic, U:ScalarArithmetic> (lhs:T,rhs:U) -> Bool {
@prendio2
prendio2 / SUPTableViewController.m
Created March 26, 2014 15:05
Custom viewWillApear to restore selected row when transition is cancelled
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSIndexPath *selectedRowIndexPath = [self.tableView indexPathForSelectedRow];
if (selectedRowIndexPath) {
[self.tableView deselectRowAtIndexPath:selectedRowIndexPath animated:YES];
[[self transitionCoordinator] notifyWhenInteractionEndsUsingBlock:^(id<UIViewControllerTransitionCoordinatorContext> context) {
if ([context isCancelled]) {
[self.tableView selectRowAtIndexPath:selectedRowIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];

The most obvious example is linking of dependent libraries. When you add a framework to your project, that framework already has encoded the libraries it depends on; simply drop the framework in, and no further changes are required to the list of libraries your project itself links to.

People/companies do this with static libraries as well, whereas it should be “the application developer's responsibility to add all required libraries in addition to the one they actually want to use”.

Of course, if ‘the one [library] they actually want to use’ is the only library they care about, then having a single ‘drop-in’ solution is preferable. However, once you depend on multiple libraries that might have common dependencies, there’s no way around having to have a dependency (versions) manager. Because while you can pull-off various symbol tricks with C libraries, you cannot with Objective-C. As in, there can only ever be one class/method registered for a given name in the runtime.

I feel like this was

@michaelochurch
michaelochurch / hackernews-banned-post
Created January 9, 2014 18:43
This is the reply I can't write on Hacker News, because PG is a fucking child and I get the "submitting too fast" error after ~3 posts per day.
In reply to: https://news.ycombinator.com/item?id=7031552
*People, as a group, really aren't interested in learning any more than the bare minimum they need to
get what they want. That's what MOOCs are really up against.*
True, and the people who are motivated tend to rely on each other, if not for support, at least to have
peers who are similarly interested. It's like the fact that people whose friends are obese are more likely
to gain weight.
If your motivation/work ethic/freedom-from-external-distraction level is 9-10, then you're fine working