Skip to content

Instantly share code, notes, and snippets.

View harlanhaskins's full-sized avatar
🦅
Swifting

Harlan Haskins harlanhaskins

🦅
Swifting
View GitHub Profile
var str = "Hello, playground"
public extension String {
public func replacing(range: CountableClosedRange<Int>, with replacementString: String) -> String {
let start = characters.index(characters.startIndex, offsetBy: range.lowerBound)
let end = characters.index(start, offsetBy: (range.upperBound + 1) - range.lowerBound)
return self.replacingCharacters(in: start ..< end, with: replacementString)
}
}

Consider this Swift:

let addr: String? = person?.job?.address

In this case, if person is nil, it returns nil, otherwise it binds the job, and repeats.

This same thing is expressed in Haskell as:

// note the signature => (T?, T -> U?) -> U?
func flatMap<T, U>(value: T?, transform: T -> U?) -> U? {
switch value {
case .None: return .None
case .Some(let contained): return transform(contained)
}
}
// different from this => (T?, T -> U) -> U?
func map<T, U>(value: T?, transform: T -> U) -> U? {
; ModuleID = 'fact.ltr'
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-apple-darwin15.0.0"
; Function Attrs: nounwind readnone
define i64 @main() #0 {
entry:
ret i64 0
}
; ModuleID = '/Users/harlanhaskins/fact.ltr'
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-apple-darwin15.0.0"
@printformat = private constant [3 x i8] c"%d\00", align 1
@printlnformat = private constant [4 x i8] c"%d\0A\00", align 1
declare i64 @printf(i8*, ...)
define private i64 @print(i64 %arg) {
package cunn not found!
package cutorch not found!
Falling back on CPU mode
creating an rnn...
missing seed text, using uniform probability over first character
--------------------------
ghedilite.
Wertce tirkly and sprinkle to the mixture and stir cold water, purnith for atili ex13 in is remaining whipped mixing bowl, combine brown sugar
1 teaspoon ground chilled, warm boiling water (or a skillet thinly fresh fish in 1/8 inch pieces cream cheese, soy cake stuf cheese, karnish with thick)s, about 40 minutes to rice.
Place a thinly add to stor lightly tf with chicken parted to sauce
//
// main.c
// randomseed
//
// Created by Harlan Haskins on 1/10/16.
// Copyright © 2016 Harlan. All rights reserved.
//
#include <stdlib.h>
#include <stdio.h>
@harlanhaskins
harlanhaskins / Goto.swift
Created January 8, 2016 07:35
An Extensible Goto framework for Swift
struct Goto {
typealias Closure = () -> ()
var closures = [String: Closure]()
func to(label: String) {
guard let closure = closures[label] else {
fatalError("Unknown label: \(label)")
}
closure()
}
mutating func set(label: String, closure: () -> ()) {
@harlanhaskins
harlanhaskins / loops.swift
Created January 8, 2016 07:11
Loops implemented terribly, in Swift
func while_(@autoclosure(escaping) cond: () -> Bool, body: () -> ()) {
func goto(label: String) {
switch label {
case "cond": goto(cond() ? "body" : "end")
case "body":
body()
goto("cond")
case "end":
fallthrough
default: break
import Darwin
func printFile(fileName: String) {
var line: UnsafeMutablePointer<Int8> = nil
var len = 0
var read = 0
let file = fopen(fileName, "r")
guard file != nil else { return }
func goto(label: String) {
switch label {