Skip to content

Instantly share code, notes, and snippets.

View seanlilmateus's full-sized avatar

Mateus seanlilmateus

View GitHub Profile
@landonf
landonf / 1-README.md
Last active June 2, 2016 06:49
Swift Result Type

A result type (based on swiftz) that can represent either an error or success:

enum Result<X, T> {
  case Err(() -> X)
  case Ok(() -> T)
}

Now we need a way to chain multiple results together without lots of nesting of if statements -- or exceptions. To do so, we can define a new bind (result, next) operator (implementation borrowed from swiftz) that operates on Result types (a.k.a flatMap or >>=):

  • If the result is Err, the result is immediately returned.
import Darwin
let isLittleEndian = Int(OSHostByteOrder()) == OSLittleEndian
let htons = isLittleEndian ? _OSSwapInt16 : { $0 }
let htonl = isLittleEndian ? _OSSwapInt32 : { $0 }
let htonll = isLittleEndian ? _OSSwapInt64 : { $0 }
let ntohs = isLittleEndian ? _OSSwapInt16 : { $0 }
let ntohl = isLittleEndian ? _OSSwapInt32 : { $0 }
let ntohll = isLittleEndian ? _OSSwapInt64 : { $0 }
@andyshep
andyshep / PASHideDetailAnimator.m
Created June 3, 2014 22:03
A slide-and-zoom animation using collections views
#import <Foundation/Foundation.h>
#import "PASConstants.h"
#import "PASViewController.h"
@interface PASHideDetailAnimator : NSObject <UIViewControllerAnimatedTransitioning>
@end
@implementation PASHideDetailAnimator
enum Either<A, B> {
case Left(A)
case Right(B)
}
func isLeft<A,B>(it : Either<A,B>) -> Bool {
switch it { case .Left: return true; case .Right: return false }
}
func isRight<A,B>(it : Either<A,B>) -> Bool {
@intrepidmatt
intrepidmatt / gist:d8209e4fa1f7727568ac
Created June 3, 2014 14:58
Swift language headers
operator infix ^= {
}
operator infix *= {
}
operator infix * {
}
operator infix % {

How to use swift in your terminal

setup

First, install Xcode 6 beta.

And run it.

sudo xcode-select -switch /Applications/Xcode6-Beta.app/Contents/Developer
//! Monad chain macro in Rust
//!
//! A typical usage is to avoid nested match expressions.
//!
//! Supports:
//! - Pattern matching
//! - Or expressions (A | B => ...)
//! - Guarded statements (x if <cond> => ...)
#[feature(macro_rules)];
@bvssvni
bvssvni / gist:9674632
Last active December 23, 2023 22:56
A Rust Chain Macro
//! Chain macro in Rust
//!
//! A typical usage is to avoid nested match expressions.
//!
//! Supports:
//! - Pattern matching
//! - Or expressions (A | B => ...)
//! - Guarded statements (x if <cond> => ...)
//! - Implicit else (requires all arms to return same type)
//! Leveraging tuples to make a statically typed, concatenative EDSL in Rust.
//!
//! I'm not sure how practical it really is – Rust's syntax can make it a little
//! hard to read, but it's fun to play around with it. The neat thing is how
//! little copying occurs. Everything is moved in and out by-value in a pipeline.
//!
//! Thanks goes to [Tekmo on reddit](http://www.reddit.com/r/programming/
//! comments/1zzom4/using_functionlength_to_implement_a_stack_language/cfyibsr)
//! for the idea.
//!
@seanlilmateus
seanlilmateus / async_await.rb
Last active December 27, 2020 08:34
whenever Rubymotion returns a symbol of the def-expr, async and await like c# will be possible
module AsyncAwait
class Task < NSOperation
def self.new(&block)
alloc.initWithBlock(block)
end
def initWithBlock(block)
init.tap do