Skip to content

Instantly share code, notes, and snippets.

View hallski's full-sized avatar

Mikael Hallendal hallski

View GitHub Profile
@hallski
hallski / gcf-async.js
Last active November 15, 2017 23:06
Google Cloud Function using es6-async
const makeAsync = require('es6-async')
const timeout = (milliseconds) => new Promise(resolve => setTimeout(resolve, milliseconds))
const random = () => Promise.resolve(Math.random())
const asyncFunc = makeAsync(function* () {
yield timeout(1000)
return yield random()
})
@hallski
hallski / zip.js
Last active November 13, 2017 20:08
Javascript implementation of `zip` that works with Iterables.
/**
* Uses the `produce` function to produce values until the `predicate` returns false.
* If supplied, each value is transformed by `transform` before being yielded.
*
* @param {Function} produce produces the next value
* @param {Function} predicate return true if the production should continue
* @param {Function} transform transforms the produced value, defaults to identity
*/
function* produceWhile(produce, predicate, transform = value => value) {
while(true) {
extension Optional {
func both_then<T, U>(_ other: Optional<U>, f: (Wrapped, U) -> T) -> Optional<T> {
guard let a = self, let b = other else { return nil }
return f(a, b)
}
func neither<T>(_ other: Optional<T>) -> Bool {
return self == nil && other == nil
}
init() {
view = {
frame = CGRect()
return UIView(frame: frame)
}()
}
func spaces(n: Int) -> String {
return reduce(0..<max(0, n), "") { r, _ in r + " " }
}
@hallski
hallski / IfElseExpression.swift
Last active August 29, 2015 14:02
Simple ifElse-expression in Swift to try out @auto_closure
func ifElse<T> (f: @auto_closure() -> Bool, ifValue: T, elseValue: T) -> T {
if f() {
return ifValue;
} else {
return elseValue;
}
}
let foo = ifElse(12 + 25 > 20, "Yep", "No") // -> foo = "Yep"
@hallski
hallski / Partial.swift
Last active August 29, 2015 14:02
Simple implementation of a partial function in Swift
func partial<T, S, A>(f: ((T, S) -> A), t : T) -> (S -> A) {
return { (s:S) in
return f(t, s)
}
}
// Bind the first argument
let addTwenty = partial(+, 20)
// Call the new function
@hallski
hallski / gist:4240823
Created December 8, 2012 15:56
Toggle terminal
set appName to "Terminal" -- openTerminalWindow requires Terminal
set newWindowScript to "tmux -2" -- set to "" to just run the shell
if not isRunning(appName) then tell application appName to activate
if numberOfWindows(appName) is 0 then
openTerminalWindow(appName, newWindowScript)
foregroundApp(appName)
else
if isCurrentApp(appName) then
- (id<RACSignal>)startWithTrigger:(id<RACSignal>)signalTrigger
{
return [RACSignal createSignal:^(id<RACSubscriber> subscriber) {
__block RACDisposable *selfDisposable = nil;
__block RACDisposable *triggerDisposable = [signalTrigger subscribe:[RACSubscriber subscriberWithNext:^(id x) {
selfDisposable = [self subscribeNext:^(id x2) {
[subscriber sendNext:x2];
} error:^(NSError *error) {
[subscriber sendError:error];
} completed:^{
@hallski
hallski / TCRACSocket.m
Created May 6, 2012 22:46
TCRACSocket with subjects
#import "TCRACSocket.h"
#import <AsyncSocket.h>
@interface TCRACSocket ()
@property(strong,readwrite) AsyncSocket *sock;
@property(strong) RACSubject *connectSubject;
@property(strong) RACSubject *linesSubject;
@end
@implementation TCRACSocket