Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,153,473 members, 7,819,721 topics. Date: Monday, 06 May 2024 at 09:39 PM

Anonymous Functions/ Lambda Functions - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Anonymous Functions/ Lambda Functions (1277 Views)

Most Used Mysql Database Functions / Missing Nairaland Functions / 7 Useful Functions To Tighten The Security In Php (2) (3) (4)

(1) (Reply) (Go Down)

Anonymous Functions/ Lambda Functions by ektbear: 3:46am On Sep 21, 2012
Make life so pleasant.

I wish I'd been aware of this programming concept as a kid.
Re: Anonymous Functions/ Lambda Functions by Javanian: 8:33am On Sep 21, 2012
Could you throw more light
Re: Anonymous Functions/ Lambda Functions by ektbear: 8:48am On Sep 21, 2012
Sure.

I want to call some function f(n,k).

I want to examine it under two regimes:

1. k fixed at 1, and n varying over 100, 1000, 10000
2. k=sqrt(n), n varying over 100, 1000, 10000
3. k=0.5*n, n varying over 100, 1000, 10000

One very simple way for me to do this is to wrap f with a new function g that takes n as a parameter, as well as a function h_1, h_2, or h_3.

This allows me to avoid modifying the internals of the function f, or have some sort of awkward implementation of f which takes say a code corresponding to which function I want it to select.

So g looks like the following:
Re: Anonymous Functions/ Lambda Functions by Javanian: 9:12am On Sep 21, 2012
Thanks, but can i see a java version of the g function?
Re: Anonymous Functions/ Lambda Functions by ektbear: 9:15am On Sep 21, 2012
It looks like you guys don't have lambdas in java...but they are available in Java 8?

You have to do something a bit kludgy to get the same behavior:
http://stackoverflow.com/questions/929988/equivalent-for-pythons-lambda-functions-in-java
Re: Anonymous Functions/ Lambda Functions by Javanian: 9:19am On Sep 21, 2012
^ Thanks a million
Re: Anonymous Functions/ Lambda Functions by ektbear: 9:20am On Sep 21, 2012
np
Re: Anonymous Functions/ Lambda Functions by Nobody: 4:00pm On Sep 21, 2012
thats one of the many reasons python developers are so passionate about it

i like the ability to pass objects around in python without python making copies
so you get to work on same object anywhere in your app.

Actionscript 3 has one feature i wish could be replicated in python, or maybe it's there but am not aware of it.
in as3, a dictionary key can be any object,
so when you modify the key anywhere else or even on the dictionary itself, it becomes updated throughout your app
regardless of if it's in use.
Re: Anonymous Functions/ Lambda Functions by lordZOUGA(m): 4:33pm On Sep 21, 2012
Lambda functions exist in C++. Its offered by boost libraries (#include <boost/lambda/lambda.hpp>wink.
It also exists in much recent versions of C++ (though am yet to use the native C++ version).

@webdezzi, passing objects around without making copies of them exists in C/C++. Its done by referencing them like func(&arg);

in as3, a dictionary key can be any object,
so when you modify the key anywhere else or
even on the dictionary itself, it becomes updated throughout your app
regardless of if it's in use.

are you talking about static or global instances?
Re: Anonymous Functions/ Lambda Functions by Nobody: 8:01am On Sep 22, 2012
also possible in javaScript

[2,4,5,6,7,8].each(function(item){
item=item*2 ;
});

var x=function(a){alert(a)} ;
x( "Okeowo" ) ;
//even allows me to do currying
var x=function(a){return function(b){console.log( a+b ) ; } }
x("Okeowo Love" ) ( "JavaScript" ) ;


That shit cray
Re: Anonymous Functions/ Lambda Functions by ektbear: 8:04am On Sep 22, 2012
Yeah, Javascript is actually a functional programming language.

So for a JS guy, what I said is nothing new.
Re: Anonymous Functions/ Lambda Functions by Nobody: 8:06am On Sep 22, 2012
^ yeah true, which is why i love it, and you know the funny thing PHP has anonymous functions but it feels weird using it, Functional languages are fun i'd like to learn python soon enough. its the space stuff and no curly braces i might ave to get used to.
Re: Anonymous Functions/ Lambda Functions by Nobody: 4:58pm On Sep 23, 2012
lordZOUGA: Lambda functions exist in C++. Its offered by boost libraries (#include <boost/lambda/lambda.hpp>wink.
It also exists in much recent versions of C++ (though am yet to use the native C++ version).

@webdezzi, passing objects around without making copies of them exists in C/C++. Its done by referencing them like func(&arg);

thanks for that reminder, maybe when i start serious work with C, i'll enjoy that beauty


in as3, a dictionary key can be any object,
so when you modify the key anywhere else or
even on the dictionary itself, it becomes updated throughout your app
regardless of if it's in use.

are you talking about static or global instances?
both.

e.g.



var theObject:myClass=new myClass(); //create an object with myClass
var anotherObject:yourClass=new yourClass();

var myDict:Dictionary = new Dictionary();
myDict[theObject] = anotherObject;

lets say you are using theObject above to hold mp3 binary data
and somewhere else, where theObject is accessible, you decide to chop of part of the mp3 data and append another song data to it
you will assume anotherObject will be lost simply because the key to access it has changed, right?
nope!
you can still use theObject as a key to anotherObject.
this can very useful instead of passing objects around as function parameters.
if your dictionary is public, you can simply borrow it's key.
Re: Anonymous Functions/ Lambda Functions by Nobody: 9:09pm On Sep 23, 2012
for some reason i never liked ActionScript the syntax was weird,
Re: Anonymous Functions/ Lambda Functions by Nobody: 9:45pm On Sep 23, 2012
Re: Anonymous Functions/ Lambda Functions by Nobody: 8:26pm On Sep 24, 2012
^thanks a lot omo_dun but mehn NodeJS is the main shit peeps should be trying now, so many crafty ideas you can come up with,

(1) (Reply)

Help Me With C++ Assisgnment / How Can I Create Blackberry App For My Blog? / Cicada 3301 - Today Is January 5th

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 21
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.