Skip to content

Instantly share code, notes, and snippets.

@moreindirection
moreindirection / BranchingWithCombine.swift
Created September 3, 2020 20:26
Branching with Combine
URLSession.shared.dataTaskPublisher(for: URL(string: BASE_URL + filename)!)
.flatMap { data, response in
let httpURLResponse = response as! HTTPURLResponse
switch httpURLResponse.statusCode {
case 404:
return Just("Unable to find file").eraseToAnyPublisher()
case (300..<399):
// this is a function that returns a publisher - it can make network requests, whatever
return processRedirect(data)
@moreindirection
moreindirection / Combine Version.swift
Last active May 15, 2020 15:29
Async / Await in Swift
func loadWebResource(_ path: String) -> AnyPublisher<Resource, Error>
func decodeImage(_ r1: Resource, _ r2: Resource) -> AnyPublisher<Image, Error>
func dewarpAndCleanupImage(_ i : Image) -> AnyPublisher<Image, Error>
func processImageData1() -> AnyPublisher<Image, Error> {
loadWebResource("dataprofile.txt").zip(loadWebResource("imagedata.txt")).flatMap { (dataResource, imageResource) in
decodeImage(dataResource, imageResource)
}
.flatMap { imageTmp in
dewarpAndCleanup(imageTmp)
@moreindirection
moreindirection / AppDelegate.m
Last active December 31, 2015 12:59
Tab Bar issue in iOS 7
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
UITabBarController* tabController = [[UITabBarController alloc] init];
@moreindirection
moreindirection / gist:977598
Created May 17, 2011 22:52
Dataflow Futures
import actors.Futures
class DataflowFuture[A] private(f : => A) {
val future = Futures.future(f)
}
object DataflowFuture {
def apply[A](f : => A) = new DataflowFuture[A](f)
implicit def dataflowFutureToVal[A](df : DataflowFuture[A]) : A = df.future.apply()
@moreindirection
moreindirection / gist:977255
Created May 17, 2011 19:56
Poor Man's Dataflow
import actors.Futures.future
object PoorMansDataflow {
def main(args : Array[String]) {
// ....
val pastSales = future(db.getTotalSales)
val todaysSales = future(input.map(parseReport(_)).sumBy(_.totalSold))
function rm () {
local path
for path in "$@"; do
# ignore any arguments
if [[ "$path" = -* ]]; then :
else
local dst=${path##*/}
# append the time if necessary
while [ -e ~/.Trash/"$dst" ]; do
http.createServer(function (request, response) {
var uri = url.parse(request.url).pathname;
if(uri=='/pong') {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('PONG');
} else if ((match = uri.match(/^\/echo\/(.*)$/)) != null) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end(match[1]);
import Data.Char (isSpace)
trim :: String -> String
trim = trimAndReverse . trimAndReverse
where trimAndReverse = reverse . dropWhile isSpace
reverseBreak :: (a -> Bool) -> [a] -> ([a], [a])
reverseBreak f xs = (reverse before, reverse after)
where (after, before) = break f $ reverse xs