Skip to content

Instantly share code, notes, and snippets.

View drodriguez's full-sized avatar

Daniel Rodríguez Troitiño drodriguez

View GitHub Profile
import Foundation
enum Either<A, B> {
case Left(@auto_closure () -> A)
case Right(@auto_closure () -> B)
func fold<X>(fa: A -> X, fb: B -> X) -> X {
switch self {
case let .Left(a): return fa(a())
case let .Right(b): return fb(b())
@drodriguez
drodriguez / boolean_properties.swift
Created June 22, 2014 23:50
Abusing optional syntax for better boolean properties
class MyClass {
var enabled: Bool? {
didSet {
if let v = enabled? {
self.enabled = v ? true : nil
}
}
}
init(enabled: Bool) {
@drodriguez
drodriguez / ycombinator.swift
Created June 23, 2014 22:43
Y combinator in Swift
// You can have a recursive function
func fact(n: Int) -> Int {
if n < 2 { return 1 }
return n * fact(n - 1)
}
// But you cannot have a recursive closure
// let fact2: Int -> Int = { n in
// if n < 2 { return 1 }
// return n * fact2(n - 1) // ERROR: Variable used within its own initial value
@drodriguez
drodriguez / Spec.m
Created August 14, 2014 23:47
Better Kiwi?
#import "Kiwi.h"
#define KWPasteX(x, y) x ## y
#define KWPaste(x, y) KWPasteX(x, y)
#define KWMetaMacro(...) \
if (0) \
KWPaste(finished, __LINE__): ; \
else \
for (KWVoidBlock KWPaste(block, __LINE__) = nil;;) \
@drodriguez
drodriguez / gist:64c470cf59eae85767b1
Created December 6, 2014 00:10
Computed variable to return type conforming protocol.
protocol TestProtocol {
}
class TestClass : TestProtocol {
}
class TestClass2 {
}
struct Test {
@drodriguez
drodriguez / test-array-signature.c
Created December 24, 2014 11:34
TIL about arrays in signatures in C
#include <stdlib.h>
int test1(int32_t (*array)[10]) {
return 1;
}
int test2(int32_t (*array)[5]) {
return 2;
}
This script and build target will take the revision number from a Subversion repository (or a Git
repository using git-svn) and substitute the last dotted component of the CFBundleVersion in your
Info.plist file (you can put something like "1.0.0.xx" for your first run).
Instructions:
- Save update_build_number.rb into ${PROJECT_DIR}/Scripts (or wherever you want, but remember to
change the paths accordigly in the build phase).
- Add a new target to your project (I named mines as "Update FooBar Build Number"). In new target
dialog choose from "Other" category "Shell Script Target".
def month_names_between
# No hay necesidad de convertir en string para comparar
return [I18n.l(fecini, :format => '%B')] if fecini.month == fecfin.month && fecini.year == fecfin.year
# De nuevo, no hay necesidad de convertir en string y luego en entero.
# Además utilizo nombres más descriptivos.
anio_inicio = fecini.year
anio_final = fecfin.year
mes_inicio = fecini.month
mes_final = fecfin.month
diff -urN -x 'Makefile*' -x '*comp.sh' -x libtool -x x-to-1 -x stamp-h1 -x POTFILES -x gettext.sh -x '*config*' gettext-0.17/gettext-runtime/gnulib-lib/setenv.c gettext-0.17-mine/gettext-runtime/gnulib-lib/setenv.c
--- gettext-0.17/gettext-runtime/gnulib-lib/setenv.c 2007-10-07 22:28:16.000000000 +0200
+++ gettext-0.17-mine/gettext-runtime/gnulib-lib/setenv.c 2009-09-13 20:11:21.000000000 +0200
@@ -39,7 +39,8 @@
#if !_LIBC
# define __environ environ
# ifndef HAVE_ENVIRON_DECL
-extern char **environ;
+# include <crt_externs.h>
+# define environ (*_NSGetEnviron())
// Reachability Objective-C minimal version
// I'm new so memory leaks are possible on this code
// Extracted from:
// http://www.idevapps.com/forum/archive/index.php/t-3329.html
// (look for an imported_kelvin's post)
//
+ (BOOL)networkAvailable{
SCNetworkReachabilityRef netreach;
SCNetworkConnectionFlags flags;