Skip to content

Instantly share code, notes, and snippets.

View HenningBrandt's full-sized avatar
🤓

Henning Brandt HenningBrandt

🤓
View GitHub Profile
@HenningBrandt
HenningBrandt / nsarray_group.m
Last active December 9, 2015 21:44
Neat snippet to group objects in an NSArray based on equal values under a given keypath.
@implementation NSArray (GroupBy)
- (NSArray *)groupByKeyPath:(NSString *)keyPath {
NSArray *distinctValues = [self valueForKeyPath:[@"@distinctUnionOfObjects." stringByAppendingString:keyPath]];
NSMutableArray *result = [NSMutableArray arrayWithCapacity:[distinctValues count]];
for (id groupValue in distinctValues) {
NSString *predicateFormat = [NSString stringWithFormat:@"(%@ == %%@)", keyPath];
NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateFormat, groupValue];
NSArray *group = [self filteredArrayUsingPredicate:predicate];
func foldl1<A>(list: Array<A>, f: (A, A) -> A) -> A {
return foldl(list.head!, list: list.tail!, f: f)
}
extension Array {
var initial: Array<Element>? {
get {
if self.empty() { return nil }
return Array(dropLast(self))
}
@HenningBrandt
HenningBrandt / fold.swift
Last active December 9, 2015 20:34
An implementation of fold in Swift with a few usage examples. Blog article corresponding to it: http://thepurecoder.com/functional-swift-fold-it-baby/
extension Array {
func empty() -> Bool {
return self.count == 0
}
var head: Element? {
get {
return self.first
}
}
@HenningBrandt
HenningBrandt / tree.swift
Created February 11, 2015 04:44
Recursive, generic enums in Swift
protocol TreeLike {}
enum Tree<T> : TreeLike {
case Leaf(@autoclosure () -> T)
case Node(TreeLike, TreeLike)
}
func node<T>(left: Tree<T>, right: Tree<T>) -> Tree<T> {
return Tree.Node(left, right)
}
@HenningBrandt
HenningBrandt / ascii-art.zsh-theme
Last active April 16, 2016 05:23
Very simple prompt with: User, Git information, current working directory. Special feature is a random prompt character for each new session in the form of a short one line ASCII-Art
# Very simple prompt with:
# User, Git information, current working directory
# Special feature (because I love useless stuff) is a random prompt character for each new session
# in the form of a short one line ASCII-Art (Yeah! ASCII-Art)
#
# Some ASCII-Art might be NSFW, so be aware if you you use this prompt in public ;)
#
# Source of ASCII-Art: http://1lineart.kulaone.com
#
# Author: Henning Brandt
@HenningBrandt
HenningBrandt / 1lineart_scraper.rb
Last active August 29, 2015 14:12
Simple script to scrape for one line ASCII-Art and filter them by character-type and length. It than constructs a shell-script array and ouputs it to the file argument.
#!/usr/bin/env ruby
# Simple script to scrape for one line ASCII-Art and filter them by character-type and length.
# It than constructs a shell-script array and ouputs it to the file argument.
#
# Author: Henning Brandt
# Created: 04.01.15
# Licence: MIT
#