Skip to content

Instantly share code, notes, and snippets.

@Qata
Created March 17, 2016 04:01
Show Gist options
  • Save Qata/bb25ba7db64a6ba978e4 to your computer and use it in GitHub Desktop.
Save Qata/bb25ba7db64a6ba978e4 to your computer and use it in GitHub Desktop.
//
// CTMaybe.c
// Firestarter
//
// Created by Charlotte Tortorella on 17/03/2016.
// Copyright © 2016 DALI Lighting. All rights reserved.
//
#include "CTMaybe.h"
CTMaybe * CTMaybeCreate(CTAllocator * alloc, CTObject * object) {
CTMaybe * maybe = CTAllocatorAllocate(alloc, sizeof(CTMaybe));
maybe->alloc = alloc;
maybe->object = object;
return maybe;
}
uint8_t CTMaybeIsJust(CTMaybe * maybe) {
return maybe->object != NULL;
}
uint8_t CTMaybeIsNothing(CTMaybe * maybe) {
return maybe->object == NULL;
}
CTObject * CTMaybeWithDefault(CTMaybe * maybe, CTObject * default_object) {
if (CTMaybeIsJust(maybe)) {
return maybe->object;
}
return default_object;
}
CTMaybe * CTMaybeMap(CTAllocator * alloc, CTMaybe * maybe, CTObject * (^mapFn)(const CTObject * object)) {
if (CTMaybeIsJust(maybe)) {
return CTMaybeCreate(alloc, mapFn(maybe->object));
}
return CTMaybeCreate(alloc, NULL);
}
CTMaybe * CTMaybeMap2(CTAllocator * alloc, CTMaybe * maybe1, CTMaybe * maybe2, CTObject * (^mapFn)(const CTObject * object1, const CTObject * object2)) {
if (CTMaybeIsJust(maybe1) && CTMaybeIsJust(maybe2)) {
return CTMaybeCreate(alloc, mapFn(maybe1->object, maybe2->object));
}
return CTMaybeCreate(alloc, NULL);
}
CTMaybe * CTMaybeMap3(CTAllocator * alloc, CTMaybe * maybe1, CTMaybe * maybe2, CTMaybe * maybe3, CTObject * (^mapFn)(const CTObject * object1, const CTObject * object2, const CTObject * object3)) {
if (CTMaybeIsJust(maybe1) && CTMaybeIsJust(maybe2) && CTMaybeIsJust(maybe3)) {
return CTMaybeCreate(alloc, mapFn(maybe1->object, maybe2->object, maybe3->object));
}
return CTMaybeCreate(alloc, NULL);
}
CTMaybe * CTMaybeMap4(CTAllocator * alloc, CTMaybe * maybe1, CTMaybe * maybe2, CTMaybe * maybe3, CTMaybe * maybe4, CTObject * (^mapFn)(const CTObject * object1, const CTObject * object2, const CTObject * object3, const CTObject * object4)) {
if (CTMaybeIsJust(maybe1) && CTMaybeIsJust(maybe2) && CTMaybeIsJust(maybe3) && CTMaybeIsJust(maybe4)) {
return CTMaybeCreate(alloc, mapFn(maybe1->object, maybe2->object, maybe3->object, maybe4->object));
}
return CTMaybeCreate(alloc, NULL);
}
CTMaybe * CTMaybeMap5(CTAllocator * alloc, CTMaybe * maybe1, CTMaybe * maybe2, CTMaybe * maybe3, CTMaybe * maybe4, CTMaybe * maybe5, CTObject * (^mapFn)(const CTObject * object1, const CTObject * object2, const CTObject * object3, const CTObject * object4, const CTObject * object5)) {
if (CTMaybeIsJust(maybe1) && CTMaybeIsJust(maybe2) && CTMaybeIsJust(maybe3) && CTMaybeIsJust(maybe4) && CTMaybeIsJust(maybe5)) {
return CTMaybeCreate(alloc, mapFn(maybe1->object, maybe2->object, maybe3->object, maybe4->object, maybe5->object));
}
return CTMaybeCreate(alloc, NULL);
}
CTMaybe * CTMaybeOneOf(CTAllocator * alloc, CTArray * array) {
CTObject * return_value = CTArrayFirst(array, ^uint8_t(const CTObject *object) {
return CTMaybeIsJust(object->ptr);
});
if (return_value) {
return return_value->ptr;
}
return CTMaybeCreate(alloc, NULL);
}
void example(void) {
CTAllocator * alloc = CTAllocatorCreate();
CTMaybe * maybe = CTMaybeCreate(alloc, CTObjectCreate(alloc, CTStringCreate(alloc, ""), CTOBJECT_TYPE_STRING));
CTMaybe * new_value = CTMaybeMap(alloc, maybe, ^CTObject *(const CTObject *object) {
if (CTObjectType(object) == CTOBJECT_TYPE_STRING) {
return CTObjectCreate(alloc, CTArrayCreate(alloc), CTOBJECT_TYPE_ARRAY);
}
return CTObjectCreate(alloc, CTDictionaryCreate(alloc), CTOBJECT_TYPE_DICTIONARY);
});
if (CTMaybeIsJust(new_value)) {
puts("Success I suppose");
}
CTAllocatorRelease(alloc);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment