Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Functional Programming in C/C++

Name: Anonymous 2012-10-01 20:15

I present you with a simple BASH script which adds lambdas to C/C++.

Now you can finally run your:

qsort(array, nelts, sizeof(int), lambda (int) (int *x, int *y) () return *x<*y; endLambda)


It expands code like

#include <stdio.h>

int main(int argc, char **argv) {
  int a = 1;
  int b = 2;
  printf("%d\n",
   (lambda (int) (int x) (int a, int b) return x+a+b; endLambda)(4));
  return 0;
}


into:

static int *_la_1337_a;
static int *_la_1337_b;
static int _lf_1337(int x);
#include <stdio.h>

int main(int argc, char **argv) {
  int a = 1;
  int b = 2;
  printf("%d\n",
   ((_la_1337_a=&a,_la_1337_b=&b,_lf_1337))(4));
  return 0;
}


int _lf_1337(int x) {
#define a (*_la_1337_a)
#define b (*_la_1337_b)
 return x+a+b;
#undef a
#undef b
}


So you worst!



#!/bin/bash

File=$(cat "$1" | tr '\n' '$')
Out="$2"
CODE=1337
Lambs=$(echo "$File" | grep -o "lambda.*endLambda")
trim() { sed 's/^ *\(.*\) *$/\1/'; }
part() { sed "s/lambda.*(\(.*\)).*(\(.*\)).*(\(.*\))\(.*\)endLambda/\\$1/"; }
RetType="$(echo "$Lambs" | part 1)"
Args="$(echo "$Lambs" | part 2)"
Vars="$(echo "$Lambs" | part 3)"
Body="$(echo "$Lambs" | part 4)"


Result="$(echo "$Vars" | tr ',' '\n' | trim | sed "s/^\(.*\) \([^ ]*\)\$/_la_${CODE}_\2=\\\\\&\2/" | tr '\n' ',')"
Result="(${Result}_lf_${CODE})"

echo "$Result"
echo "$Vars" | tr ',' '\n' | trim | sed "s/^\(.*\) \([^ ]*\)\$/static \1 *_la_${
CODE}_\2;/" > "$Out"
echo "static ${RetType} _lf_${CODE}($Args);" >> "$Out"

echo "$File" | sed "s/lambda.*endLambda/$Result/" | tr '$' '\n' >> "$Out"

echo "${RetType} _lf_${CODE}($Args) {" >> "$Out"
echo "$Vars" | tr ',' '\n' | trim | sed "s/^\(.*\) \([^ ]*\)\$/#define \2 (*_la_${CODE}_\2)/" >> "$Out"
echo "$Body" >> "$Out"
echo "$Vars" | tr ',' '\n' | trim | sed "s/^\(.*\) \([^ ]*\)\$/#undef \2/" >> "$Out"
echo "}" >> "$Out"

Name: Anonymous 2012-10-01 21:11

cool

Name: Anonymous 2012-10-01 21:30

You're still a bad programmer.

Name: Poop 2012-10-01 21:53

I love 1337 stuff.

Name: Anonymous 2012-10-01 22:20

7/10 would consider using

Name: Anonymous 2012-10-01 22:28

A somewhat fixed version, which provides shorter syntax:

#include <stdio.h>

int main(int argc, char **argv) {
  int a=1;
  int b=2;
  printf("%d\n", <:int; int x; int a, int b => return a+b+x:>(4));
  return 0;
}




#!/bin/bash

File=$(cat "$1" | tr '\n' '$')
Out="$2"
CODE=1337
Lambs=$(echo "$File" | grep -o "<:.*:>")
trim() { sed 's/ *$//' | sed 's/^ *//'; }
part() { sed "s/<:.*\(.*\);\(.*\);\(.*\)=>\(.*\):>/\\$1/"; }
RetType="$(echo "$Lambs" | part 1)"
Args="$(echo "$Lambs" | part 2 | trim)"
Vars="$(echo "$Lambs" | part 3 | tr ',' '\n' | trim)"
Body="$(echo "$Lambs" | part 4)"

Result="$(echo "$Vars" | sed "s/^\(.*\) \([^ ]*\)\$/_la_${CODE}_\2=\\\\\&\2/" | tr '\n' ',')"
if [ "$Result" == "," ]; then Result=""; fi
Result="(0,${Result}_lf_${CODE})"

echo "$Vars" | tr ',' '\n' | trim | sed "s/^\(.*\) \([^ ]*\)\$/static \1 *_la_${CODE}_\2;/" > "$Out"
echo "static ${RetType} _lf_${CODE}($Args);" >> "$Out"
echo "$File" | sed "s/<:.*:>/$Result/" | tr '$' '\n' >> "$Out"
echo "${RetType} _lf_${CODE}($Args) {" >> "$Out"
echo "$Vars" | sed "s/^\(.*\) \([^ ]*\)\$/#define \2 (*_la_${CODE}_\2)/" >> "$Out"
echo "$Body;" | tr '$' '\n' >> "$Out"echo "$Vars" | sed "s/^\(.*\) \([^ ]*\)\$/#undef \2/" >> "$Out"
echo "}" >> "$Out"

Name: Anonymous 2012-10-01 22:40

GCC supports this out of box:


#include <stdio.h>

#define lambda(l_ret_type, l_arguments, l_body)         \
  ({                                                    \
    l_ret_type l_anonymous_functions_name l_arguments   \
      l_body                                            \
    &l_anonymous_functions_name;                        \
  })

int main(int argc, char **argv) {
  int a=1;
  int b=2;
  printf("%d\n", lambda(int, (int x), {return a+b+x;})(4));
  return 0;
}

Name: Anonymous 2012-10-01 22:59

Scheme supports this out of box:

(lambda (w)
  (lambda (f)
    (f ((w w) f))))

Name: Anonymous 2012-10-01 23:02

Sure, Alonzo Church invented this out of the box, but did he Abelson invent it out of the box?

Name: Anonymous 2012-10-01 23:06

>>8
C/C++ is faster and they even pay you for using it.

Name: Anonymous 2012-10-01 23:20

>>8
Okay, what the fuck does that do?

Name: Anonymous 2012-10-01 23:41

>>11
it carries out and is Y

Name: Anonymous 2012-10-01 23:50

C/C++.
C++ already has lambdas.

Name: Anonymous 2012-10-01 23:54

>>13
And has had it for years with Boost.Lambda.

Name: Anonymous 2012-10-02 0:25

>>14
How are they different from C++0x lambdas?

Name: Anonymous 2012-10-02 0:33

>>15
Boost is already implemented, Sepplesox isn't.

Name: Anonymous 2012-10-02 0:44

>>16
what is the point implementing something that is already present?

Name: Anonymous 2012-10-02 1:20

>>17
Boost is anti-standard.

Name: Anonymous 2012-10-02 7:22

There are no jews. Please continue with this programming-related discussion.

Don't change these.
Name: Email:
Entire Thread Thread List