Skip to content

Instantly share code, notes, and snippets.

View axelarge's full-sized avatar

Miķelis Vindavs axelarge

View GitHub Profile
<?php
/**
* piegriež tekstu līdz noteiktam garumam
*
* @param string $str - Piegriežamais teksts
* @param int $maxLen - Maksimālais garums
* @param array $options - papildus opcijas
* - string omission => teksts, ko pieliek, ja nogriež (default: '...')
* - char separator => pēc kā atdalīt tekstu (default: null)
* - bool abbreviate => vai piegrieztais teksts jāievieto iekš <abbr> taga (default: false)
p = Product.find(546)
# Product Load (0.4ms) ...
p.price
# => #<BigDecimal:be36588,'0.0',9(18)>
p.changed
# => []
p.price = '0.0'
# => "0.0"
p.price
# => #<BigDecimal:be30a34,'0.0',9(18)>
class Array
def pairs_for(k)
seen = Hash.new(0)
each_with_object([]) do |i, result|
complement = k - i
seen_count = seen[complement]
if seen_count > 0
result << [complement, i]
seen[complement] -= 1
else
@axelarge
axelarge / gist:5990587
Last active December 19, 2015 17:19
Objective-C macro for switching on objects
@implementation NSObject (Extra)
- (BOOL)isEqualToAnyOf:(id)object, ...
{
va_list args;
va_start(args, object);
for (id other = object; other != nil; other = va_arg(args, id)) {
if ([self isEqual:other]) return YES;
}
va_end(args);
// Shortest version
document.write("1,2,3,5,8,13,21,34,55,89,144,233")
// Shortest version without cheating
for(a=[],x=y=1;y<301;t=x,x=y,y+=t)a.push(y);document.write(a)
// If it's ok to start with 1,1, this can be shortened by another char:
for(a=[x=y=1];y<301;t=x,x=y,y+=t)a.push(y);document.write(a)
// Shortest version that doesn't introduce global variables
@axelarge
axelarge / gist:7386644
Created November 9, 2013 15:36
Objective-C NSComparisonResult for primitives macro
#define COMPARE(A, B) ({ \
__typeof__(A) __a = (A); \
__typeof__(B) __b = (B); \
__a < __b ? NSOrderedAscending : __a == __b ? NSOrderedSame : NSOrderedDescending; \
})
typedef void(^LogBlock)(NSString *, ...);
#define LogBlock LogBlock NS_FORMAT_FUNCTION(1,2)
- (BOOL)loggingErrorsDo:(void (^)(LogBlock error))block
{
__block BOOL success = YES;
block(^(NSString *messageFormat, ...) {
va_list args;
va_start(args, messageFormat);
NSString *message = [[NSString alloc] initWithFormat:messageFormat arguments:args];
@axelarge
axelarge / gist:8026201
Created December 18, 2013 17:17
Python-style generators in Objective-C
@interface CDYieldEnumerator : NSEnumerator
@end
typedef BOOL(^YieldBlock)(id);
#define YIELD_BLOCK ^(YieldBlock yield)
#define YIELD(OBJECT) do { if (!yield(OBJECT)) return; } while (0)
@implementation CDYieldEnumerator {
dispatch_queue_t _queue;
dispatch_semaphore_t _queueLock;
from itertools import groupby, izip
from operator import itemgetter
DATA = [
{"service_date": "2013-10-28", "odometer": "115379"},
{"service_date": "2013-10-11", "odometer": "115050"},
{"service_date": "2013-09-27", "odometer": "114410"},
{"service_date": "2013-05-15", "odometer": "107936"},
{"service_date": "2013-05-09", "odometer": "107534"},
{"service_date": "2012-10-16", "odometer": "101089"},
// Gandrīz vienmēr
if ([self someThingOrOther]) {
return;
}
// Retos gadījumos (return, break, continue)
if ([self somethingOrOther]) return;
// Pretējā gadījumā vieglāk var notikt šis
if ([self somethingOrOther])