Skip to content

haifenghuang/linq4c

Repository files navigation

Linq for C

IMG1 IMG2

Features

  • Most of the LINQ methods(60+) are supported.
  • Chaining of LINQ methods are supported.
  • Lambda is supported(slow).

Examples

For more examples, please see the main.c file.

Example using callbacks

#include <gc.h>
#include "ArrayList.h"
#include "linq.h"

bool WhereCallback(void *item) {
    char *str= (char *)item;
    return str[0] == 'h';
}

void *SelectCallback(void *item) {
    return newStr("%s_1", (char *)item);
}

ArrayList testLinq(ArrayList array) {
    Linq *lq = From(array);

    ArrayList result =
        lq
        ->Where(lq, WhereCallback)
        ->Select(lq, SelectCallback)
        ->ToArray(lq);

    return result;
}

int main() {
    GC_INIT();

    char *str1 = "huang", *str2 = "hai", *str3 = "feng";

    ArrayList array = arrlist_new();
    arrlist_append(array, str1);
    arrlist_append(array, str2);
    arrlist_append(array, str3);

    ArrayList result = testLinq(array);
    for(int i = 0; i < arrlist_size(result); i++) {
        printf("%s\n", arrlist_get(result, i));
    }

    return 0;
}

Example with lambdas

ArrayList testLinq(ArrayList array) {
    Linq *lq = From(array);

    ArrayList result =
        lq
        ->Where(lq,
                lambda(bool, (void *item) {
                    char *str= (char *)item;
                    return str[0] == 'h';
                }))
        ->Select(lq,
                 lambda(void *, (void *item) {
                     return newStr("%s_1", (char *)item);
                 }))
        ->ToArray(lq);

    return result;
}

int main() {
    GC_INIT();

    char *str1 = "huang", *str2 = "hai", *str3 = "feng";

    ArrayList array = arrlist_new();
    arrlist_append(array, str1);
    arrlist_append(array, str2);
    arrlist_append(array, str3);

    ArrayList result = testLinq(array);
    for(int i = 0; i < arrlist_size(result); i++) {
        printf("%s\n", arrlist_get(result, i));
    }

    return 0;
}

Example with macros

#define LINQ_PTR lq

bool WhereCallback(void *item) {
    char *str= (char *)item;
    return str[0] == 'h';
}

void *SelectCallback(void *item) {
    return newStr("%s_1", (char *)item);
}

ArrayList testLinq(ArrayList array) {
    Linq *lq = From(array);

    ArrayList result =
        lq
        ->WHERE(WhereCallback)
        ->SELECT(SelectCallback)
        ->TO_ARRAY();

    return result;
}

int main(int argc, char **argv) {
    GC_INIT();

    char *str1 = "huang", *str2 = "hai", *str3 = "feng";

    ArrayList array = arrlist_new();
    arrlist_append(array, str1);
    arrlist_append(array, str2);
    arrlist_append(array, str3);

    ArrayList result = testLinq(array);
    for(int i = 0; i < arrlist_size(result); i++) {
        printf("%s\n", arrlist_get(result, i));
    }

    return 0;
}

Requirement

As for the garbage collect, you need to install Boehm GC.

# yum install gc-devel      # RedHat family
# apt-get install libgc-dev # Debian family

What's implemented


  • Where
  • WhereIndexed
  • Select
  • SelectIndexed
  • SelectMany
  • SelectManyIndexed
  • SelectManyBy
  • FirstOrDefault
  • LastOrDefault
  • Take
  • TakeEvery
  • TakeWhile
  • TakeWhileIndexed
  • TakeExceptLast
  • TakeFromLast
  • Skip
  • SkipWhile
  • SkipWhileIndexed
  • Any
  • All
  • Reverse
  • IndexOf
  • LastIndexOf
  • Concat
  • Except
  • Union
  • Intersect
  • SumInts
  • SumFloats
  • MaxInts
  • MaxFloats
  • MinInts
  • MinFloats
  • AverageInts
  • AverageFloats
  • Max
  • Min
  • OrderBy
  • OrderByDesc
  • GroupBy
  • Zip
  • Single
  • SingleWith
  • Contains
  • Count
  • CountWith
  • JoinStr
  • Join
  • GroupJoin
  • Aggregate
  • AggregateWithSeed
  • AggregateWithSeedBy
  • SequenceEqual
  • Prepend
  • Append
  • ForEach
  • AlternateBefore
  • AlternateAfter
  • Shuffle
  • Slice
  • Pad
  • PadBy
  • Print
  • Println

License

MIT

About

LINQ for C(GroupBy, GroupJoin, Join, Take, Where, Select, etc)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published