Skip to content

Instantly share code, notes, and snippets.

View NSStudent's full-sized avatar
🏠
Working from home

Omar NSStudent

🏠
Working from home
View GitHub Profile
@bballentine
bballentine / Middleware
Created April 26, 2017 18:06
Vapor - Example middleware that checks user's role before giving access to protected resources.
import Vapor
import HTTP
public class AdminAuthMiddleware: Middleware {
public let error: Error
public let authLevel: UserRole
public init(error: Error, authLevel: UserRole) {
self.error = error
self.authLevel = authLevel
}
@GemmaDelOlmo
GemmaDelOlmo / Optional+Unwrapping.swift
Last active December 30, 2018 10:59
Optional extension for easily unwrapping one or more variables.
extension Optional {
//Executes the closure if there is some value
func then(_ handler: (Wrapped) -> Void) {
switch self {
case .some(let wrapped): return handler(wrapped)
case .none: break
}
}
exec > /tmp/${PROJECT_NAME}_archive.log 2>&1
UNIVERSAL_OUTPUTFOLDER=${BUILD_DIR}/${CONFIGURATION}-universal
if [ "true" == ${ALREADYINVOKED:-false} ]
then
echo "RECURSION: Detected, stopping"
else
export ALREADYINVOKED="true"
@andymatuschak
andymatuschak / States-v3.md
Last active April 14, 2025 22:47
A composable pattern for pure state machines with effects (draft v3)

A composable pattern for pure state machines with effects

State machines are everywhere in interactive systems, but they're rarely defined clearly and explicitly. Given some big blob of code including implicit state machines, which transitions are possible and under what conditions? What effects take place on what transitions?

There are existing design patterns for state machines, but all the patterns I've seen complect side effects with the structure of the state machine itself. Instances of these patterns are difficult to test without mocking, and they end up with more dependencies. Worse, the classic patterns compose poorly: hierarchical state machines are typically not straightforward extensions. The functional programming world has solutions, but they don't transpose neatly enough to be broadly usable in mainstream languages.

Here I present a composable pattern for pure state machiness with effects,

@guarani
guarani / playground.swift
Last active December 29, 2021 09:22
Dining Philosophers Problem in Swift with NSOperationQueue
//
// A Swift implementation of the Dining Philosophers Problem:
// https://en.wikipedia.org/wiki/Dining_philosophers_problem
//
//
// P0
// f3 f0
// P3 P1
// f2 f1
// P2
@ldong
ldong / install_opencv_on_mac.md
Created May 16, 2016 01:02
install opencv on mac
  1. Create a virtualenv, mkvirtualenv lookup

  2. install opencv on mac via homebrew

brew tap homebrew/science
brew install opencv
cd ~/.virtualenvs/lookup/lib/python2.7/site-packages
ln -s /usr/local/Cellar/opencv/{VERSION_NUMBER}/lib/python2.7/site-packages/cv.py cv.py
ln -s /usr/local/Cellar/opencv/{VERSION_NUMBER}/lib/python2.7/site-packages/cv2.so cv2.so
@AliSoftware
AliSoftware / Coordinator.swift
Last active July 10, 2022 14:32
Coordinators & StateMachine - Concept
struct Coordinator {
let window: UIWindow
let navCtrl: UINavigationController?
func start() {
presentWelcomeScreen()
}
private func presentWelcomeScreen() {
let vc = WelcomeScreenViewController() // Instanciate from code, XIB, Storyboard, whatever your jam is
@gonzalezreal
gonzalezreal / ios-cell-registration-swift.md
Last active March 5, 2025 02:07
iOS Cell Registration & Reusing with Swift Protocol Extensions and Generics

iOS Cell Registration & Reusing with Swift Protocol Extensions and Generics

A common task when developing iOS apps is to register custom cell subclasses for both UITableView and UICollectionView. Well, that is if you don’t use Storyboards, of course.

Both UITableView and UICollectionView offer a similar API to register custom cell classes:

public func registerClass(cellClass: AnyClass?, forCellWithReuseIdentifier identifier: String)
public func registerNib(nib: UINib?, forCellWithReuseIdentifier identifier: String)
@andyyhope
andyyhope / SwiftEvolution_EnumCaseCountFunctionality_AndyyHope.md
Last active March 12, 2020 13:05
Swift Evolution - Enum Case Count Functionality

Swift Evolution Proposal

Author: Andyy Hope

Enum Values() Functionality (Update)

It has been brought to my attention that there was more use for the unintended values() functionality that I had outline in my "Other Languages" Java example below.

On the Swift Evolution mailing list, one developer outlined their requirement to loop through an array of enum case values to add different states to objects.

Another example where a values array would be useful if the developer wants to do something different for each different case, such as setting an image on a UIButton subclass for each different UIControlState